sql server - SQL Comparison Query Error -
sql server - SQL Comparison Query Error -
i have table transaction history 3 years, need compare sum ( transaction) 12 months sum( transaction) 4 weeks , display client list result set.
table transaction_history customer_list transaction date 1 200 01/01/2014 2 200 01/01/2014 1 100 10/24/2014 1 100 11/01/2014 2 200 11/01/2014
the output should have customer_list 1 because sum of 12 months transactions equals sum of 1 month transaction.
i confused how find sum 12 months , compare same table sum 4 weeks.
the query below work, except sample info doesnt create sense
total client 1 lastly 12 months in info set = 400 total client 1 lastly 4 weeks in info set = 200
unless want exclude lastly 4 weeks, , not part of lastly 12 months?
then alter "having clause" to:
having sum(case when dt >= '01/01/2014' , dt <='12/31/2014' (trans) end) - sum(case when dt >= '10/01/2014' , dt <= '11/02/2014' (trans) end) = sum(case when dt >= '10/01/2014' , dt <= '11/02/2014' (trans) end)
of course of study doing mean results client 1 , 2
create table #trans_hist (customer_list int, trans int, dt date) insert #trans_hist (customer_list, trans , dt ) values (1, 200, '01/01/2014'), (2, 200, '01/01/2014'), (1, 100, '10/24/2014'), (1, 100, '11/01/2014'), (2, 200, '11/01/2014') select customer_list #trans_hist grouping customer_list having sum(case when dt >= '01/01/2014' , dt <='12/31/2014' (trans) end) = sum(case when dt >= '10/01/2014' , dt <= '11/02/2014' (trans) end) drop table #trans_hist
sql sql-server
Comments
Post a Comment