按提供范围内的每个日期的历史日期分组

时间:2013-07-03 06:52:49

标签: mysql sql sql-server

我坚持了好几天。想按历史日期对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

3 个答案:

答案 0 :(得分: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 |
+---+---------+

答案 1 :(得分:0)

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

答案 2 :(得分:0)