0

我坚持了好几天。想按历史日期对 sql 结果集进行分组。因此,希望从指定日期范围内的每个日期一直按日期对结果进行分组。到目前为止,这是我的 sql,但它按日期而不是历史日期对结果进行分组。

请帮忙!

SELECT ledger.transdate,
sum(case when transcodes.dtcr = 'C' then ledger.amount else 0 end) Credit,
sum(case when transcodes.dtcr = 'D' then ledger.amount else 0 end) Debit,
sum(case when transcodes.dtcr = 'C' then ledger.amount else 0 end) -
sum(case when transcodes.dtcr = 'D' then ledger.amount else 0 end) Balance
FROM
  LEDGER
  INNER JOIN TRANSCODES ON (LEDGER.TRANSCODE = TRANSCODES.TRANSCODE)
 where ledger.transdate >= '2013-02-28' and ledger.transdate <= '2013-03-01'
 group by ledger.transdate
4

3 回答 3

0

如果我理解正确,并且您想按日期字段的仅日期部分进行分组,则可以使用 Date() 函数,即 ... group by Date(transdate)

于 2013-07-03T07:08:35.827 回答
0

也许考虑这个例子......

SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+

 SELECT x.i, SUM(y.i) running FROM ints x JOIN ints y ON y.i <= x.i GROUP BY i;
+---+---------+
| i | running |
+---+---------+
| 0 |       0 |
| 1 |       1 |
| 2 |       3 |
| 3 |       6 |
| 4 |      10 |
| 5 |      15 |
| 6 |      21 |
| 7 |      28 |
| 8 |      36 |
| 9 |      45 |
+---+---------+
于 2013-07-03T07:06:26.030 回答
0

您的答案可以在这里找到:http: //www.codeproject.com/Articles/300785/Calculating-simple-running-totals-in-SQL-Server

于 2013-07-11T07:56:57.477 回答