[英]SQL Comparison Query Error
I have a table with transaction history for 3 years, I need to compare the sum ( transaction) for 12 months with sum( transaction) for 4 weeks and display the customer list with the result set. 我有一张具有3年交易历史记录的表,我需要比较12个月的总和(交易)与4周的总和(交易),并显示结果集的客户列表。
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 only Customer_List with 1 because sum of 12 months transactions equals sum of 1 month transaction. 输出应仅包含带1的Customer_List,因为12个月的交易总和等于1个月的交易总和。
I am confused about how to find the sum for 12 months and then compare with same table sum for 4 weeks. 我对如何找到12个月的总和然后与同一表的总和比较4周感到困惑。
the query below will work, except your sample data doesnt make sense 下面的查询将起作用,除非您的示例数据没有意义
total for customer 1 for the last 12 months in your data set = 400 total for customer 1 for the last 4 weeks in your data set = 200 数据集中最近12个月的客户1总数= 400数据集中最近4周的客户1总数= 200
unless you want to exclude the last 4 weeks, and not be a part of the last 12 months? 除非您想排除最近4周,而不是最近12个月的一部分?
then you would change the "having clause" to: 那么您可以将“具有子句”更改为:
having
sum(case when Dt >= '01/01/2014' and dt <='12/31/2014' then (trans) end) - sum(case when Dt >= '10/01/2014' and dt <= '11/02/2014' then (trans) end) =
sum(case when Dt >= '10/01/2014' and dt <= '11/02/2014' then (trans) end)
of course doing this would mean your results would be customer 1 and 2 当然这样做意味着您的结果将是客户1和2
create table #trans_hist
(Customer_List int, Trans int, Dt Date)
insert into #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
from #trans_hist
group by
Customer_List
having
sum(case when Dt >= '01/01/2014' and dt <='12/31/2014' then (trans) end) =
sum(case when Dt >= '10/01/2014' and dt <= '11/02/2014' then (trans) end)
drop table #trans_hist
I suggest a self join. 我建议自己加入。
select yourfields
from yourtable twelvemonths join yourtable fourweeks on something
where fourweek.something is within a four week period
and twelvemonths.something is within a 12 month period
You should be able to work out the details. 您应该能够计算出细节。
If your transactions are always positive and you want customers whose 12-month totals equal the 4-week total, then you want customers who have transactions in the past four weeks but not in the preceding 12 months - 4 weeks. 如果您的交易始终是积极的,并且您希望12个月的总交易额等于4周的总交易额,那么您希望的客户是过去四周而不是前12个月-4周的交易。
You can get this more directly using aggregation and a having
clause. 您可以使用aggregation和
having
子句更直接地获得此信息。 The logic is to check for any transactions in the past year that occurred before the previous 4 weeks: 逻辑是检查过去4周之前发生的过去一年中的所有交易:
select Customer_List
from Transaction_History
where date >= dateadd(month, -12, getdate())
group by CustomerList
having min(date) >= dateadd(day, -4 * 7, getdate());
Look here for methods to aggregate by month, year, etc. 在此处查找按月,年等汇总的方法。
http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.