前几天,在QQ群上有Q友问到下面的问题:

客户    时间       金额  
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

相关文章:

  • 2021-12-04
  • 2022-03-06
  • 2021-11-13
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2021-09-15
  • 2021-08-21
  • 2022-12-23
相关资源
相似解决方案