您的位置:首页 > 数据库

一个SQL语句实现的统计功能

2007-12-10 16:30 555 查看
前几天,在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐