Table1
MntID GroupID SoldDate Price UpdateMonth
1 1 2008/11/01 10 2008/11/01
2 1 2008/11/05 25 2008/11/01
3 2 2008/11/05 20 2008/11/01
4 1 2008/11/07 15 2008/11/01
5 2 2008/10/01 40 2008/10/01
6 3 2008/09/28 20 2008/09/01
7 1 2008/10/08 10 2008/10/01
8 2 2008/09/11 20 2008/09/01
求每个月的合计,并求出每个组的累加值:
Select
Q1.GroupID,Q1.UpdateMonth,SUM(Q1.Price) AS Price,SUM(Q2.Price) As TotalPrice
From
(
Select GroupID,UpdateMonth,SUM(Price) AS Price
FROM
Table1
GROUP BY GroupID,UpdateMonth
) AS Q1
Inner join
(
Select GroupID,UpdateMonth,SUM(Price) AS Price
FROM
Table1
GROUP BY GroupID,UpdateMonth
)
AS Q2
On
Q2.GroupID = Q1.GroupID
AND Q2.UpdateMonth < Q1.UpdateMonth
Group BY Q1.GroupID,Q1.UpdateMonth
Order BY Q1.GroupID,Q1.UpdateMonth