|
楼主 |
发表于 2012-10-4 11:09
|
显示全部楼层
amazeyeli 发表于 2012-10-3 12:59
a:两表合并
b:5,6,7月份分组合计,筛选出
请说明X,Y具体代表什么,有什么作用,数据在附件。谢谢- ;with a as( select * from ss union all select * from wm),b as( select MONTH([date]) as [month],[id],SUM([money]) as [money_sum] from a where MONTH([date]) in (5,6,7) group by MONTH([date]),[id] having SUM([money]) < 2000),c as( select [date],[id],[money], (select SUM(money) from a as x where x.[id] = y.[id] and MONTH([date]) = 8 and x.[date] <= y.[date]) as [money_sum] from a as y),d as( select c.[date],c.id,c.money_sum from b join c on b.id = c.id where c.money_sum >= 200)select MIN([date]) as [date],id,MIN(money_sum) as money_sumfrom dgroup by id/*date id money_sum----------------------- ---------------------- ----------------------2012-08-15 00:00:00.000 1 216.50112012-08-03 00:00:00.000 2 269.262012-08-04 00:00:00.000 3 268.58662012-08-15 00:00:00.000 4 343.7362012-08-03 00:00:00.000 5 205.142012-08-04 00:00:00.000 6 324.34862012-08-03 00:00:00.000 7 261.442012-08-03 00:00:00.000 9 3709.252012-08-05 00:00:00.000 10 322.142012-08-05 00:00:00.000 12 242.21752012-08-09 00:00:00.000 13 238.36612012-08-04 00:00:00.000 14 274.882012-08-03 00:00:00.000 16 309.412012-08-06 00:00:00.000 17 218.07832012-08-03 00:00:00.000 18 255.772012-08-13 00:00:00.000 19 247.752012-08-08 00:00:00.000 20 281.4823(17 行受影响)*/
复制代码 |
|