FreeOZ论坛

标题: SQL 进行行间计算(累计余额) [打印本页]

作者: xblues    时间: 16-9-2010 21:07
提示: 作者被禁止或删除, 无法发言 标题: SQL 进行行间计算(累计余额)
这个也要我想出来啦,我好厉害啊!



原表
  1. SELECT ReportingDate, Complete
  2. FROM CMGDM.drs.CampaignsSummary
复制代码
  1. ReportingDate        Complete
  2. 2010-08-18 00:00:00.000        0
  3. 2010-08-19 00:00:00.000        122
  4. 2010-08-20 00:00:00.000        151
  5. 2010-08-21 00:00:00.000        27
  6. 2010-08-23 00:00:00.000        14
  7. 2010-08-24 00:00:00.000        81
  8. 2010-08-25 00:00:00.000        80
  9. 2010-08-26 00:00:00.000        59
  10. 2010-08-27 00:00:00.000        156
  11. 2010-08-28 00:00:00.000        5
  12. 2010-08-30 00:00:00.000        64
  13. 2010-08-31 00:00:00.000        28
  14. 2010-09-01 00:00:00.000        37
  15. 2010-09-02 00:00:00.000        31
复制代码
==================================================================

计算累计值
  1. SELECT ta.ReportingDate
  2. , SUM(tb.Complete) AS RemainingRecord
  3. FROM CMGDM.drs.CampaignsSummary AS ta
  4. , CMGDM.drs.CampaignsSummary AS tb
  5. WHERE ta.ReportingDate >= tb.ReportingDate
  6. GROUP BY ta.ReportingDate
复制代码
  1. ReportingDate        RemainingRecord
  2. 2010-08-18 00:00:00.000        0
  3. 2010-08-19 00:00:00.000        122
  4. 2010-08-20 00:00:00.000        273
  5. 2010-08-21 00:00:00.000        300
  6. 2010-08-23 00:00:00.000        314
  7. 2010-08-24 00:00:00.000        395
  8. 2010-08-25 00:00:00.000        475
  9. 2010-08-26 00:00:00.000        534
  10. 2010-08-27 00:00:00.000        690
  11. 2010-08-28 00:00:00.000        695
  12. 2010-08-30 00:00:00.000        759
  13. 2010-08-31 00:00:00.000        787
  14. 2010-09-01 00:00:00.000        824
  15. 2010-09-02 00:00:00.000        855
复制代码

作者: ubuntuhk    时间: 16-9-2010 23:07
这是MSSQL?
作者: xblues    时间: 17-9-2010 00:19
提示: 作者被禁止或删除, 无法发言 对啊,我只会ms sql




欢迎光临 FreeOZ论坛 (https://hioz.im/ibbs/) Powered by Discuz! X3.2