日期:2014-05-19  浏览次数:20657 次

求汇总SQL语句,急!急!
表A
--------
peono     |conmoney   |condate                                     |contype   |condept

0001       |1.00           |2007-07-28   11:45:29.000     |消费         |食堂A
0002       |2.00           |2007-07-28   23:45:29.000     |消费         |食堂A
0001       |1.00           |2007-07-29   13:45:29.000     |消费         |食堂A
0002       |1.50           |2007-07-30   01:45:29.000     |消费         |食堂B
0002       |1.00           |2007-07-30   23:45:29.000     |消费         |食堂A
0001       |2.00           |2007-07-31   10:45:29.000     |消费         |食堂B
0003       |1.50           |2007-07-31   11:45:29.000     |消费         |食堂A
0003       |1.00           |2007-07-31   15:45:29.000     |消费         |食堂A

如何通过查询语句得到结果:

食堂A
-------------
condate             |summoney
2007-07-28       |3.00
2007-07-29       |1.00
2007-07-30       |1.00
2007-07-31       |2.50

食堂B
------------
condate             |summoney
2007-07-28       |0.00
2007-07-29       |0.00
2007-07-30       |1.50
2007-07-31       |2.00

------解决方案--------------------
看看這個結果可以不?

Select
A.condept,
A.condate,
SUM(B.conmoney) As summoney
From
(
Select T1.*, T2.* From(
Select Distinct condept From A) T1
Cross Join (
Select Distinct Convert(Varchar(10), condate, 120) As condate From A) T2) A
Left Join
A B
On A.condept = B.condept And A.condate = Convert(Varchar(10), condate, 120)
Group By
A.condept,
A.condate
------解决方案--------------------
Select
condept,
Convert(varchar(10),Condate,120) as Condate,
Sum(conmoney) as SumMoney
From TableName
Group By Condept,
Convert(varchar(10),Condate,120)