我有这张桌子......
TABLE [orders] (
[customer_ID] uniqueidentifier,
[salary] money,
[enter_into_force_date] date
)
我需要按“customer_ID”进行分组并在今天之前获得工资总额(enter_into_force_date< = getdate)和今天之后的工资金额(enter_into_force_date> getdate) - 也就是说,我需要了解每位客户,工资是多少总和我们到今天还有将来的工资。
所以结果看起来应该......
customer_ID before_today after_today
7FBF73B0-6F18-488B-8BEA-CB1968473BBE 20,100.00 10,211.00
679329F5-D7BB-44BE-9E76-F2F02DE5FD00 1,500.10 30.100,10
我如何制作这样的TSQL?
答案 0 :(得分:4)
您可以使用此查询
select customer_id,
sum(case when enter_into_force_date <= GETDATE() then salary else 0 end) before_today,
sum(case when enter_into_force_date <= GETDATE() then 0 else salary end) after_today
from orders
group by customer_id