客户 时间 金额
A 2006-10-1 200
A 2007-5-5 300
B 2006-1-1 400
实现如下结果:
客户 3个月以内 3-6个月 ... 合计
A 200 300 500
B 400 400
下面就是SQL实现:
select 客户,
(select sum(金额) from TableA where convert(bigint, 时间) + 90 >= convert(bigint, getdate()) and TableA.客户 = a.客户) as [3个月以内],
(select sum(金额) from TableA where convert(bigint, 时间) + 180 >= convert(bigint, getdate()) and convert(bigint, 时间) + 90 < convert(bigint, getdate()) and TableA.客户 = a.客户) as [3-6个月]
from (select distinct(客户) as 客户 from TableA) a