各位大师,我需要您的帮助。
有表:
DataCollection
==================
PK Code
smallint RestaurantCode
smallint Year
tinyint Month
money Amount
money AccumulativeMonthsAmount
...
我需要每家餐厅上个月的 AccumulateAmount。
首先,我得到了“当年”的每家餐厅的最后一个月(对于这种情况):
SELECT RestaurantCode, MAX(Month) as Month FROM DataCollection
WHERE (Year >= 2012 AND YEAR <= 2012) GROUP BY RestaurantCode
现在我想将其用作子查询,以获取 Last - AccumulativeMonthsAmount :
SELECT AccumulativeMonthsAmount FROM DataCollection
WHERE (RestaurantCode, Month)
IN (SELECT RestaurantCode, MAX(Month) as Month FROM DataCollection
WHERE (Year >= 2012 AND YEAR <= 2012) GROUP BY RestaurantCode)
但是运营商IN,不工作,我该怎么办?
按年份和月份排序的示例数据:
RestCode Amount Accumulative Year Month
123 343.3 345453.65 2012 12
123 124.7 345329.00 2012 11
...
122 312.2 764545.00 2012 12
122 123.4 764233.00 2012 11
...
999 500.98 2500.98 2012 6
999 100.59 2000.00 2012 5
...
我想获得每家餐厅最后一个月的累积:
RestCode Accumulative Month
123 345453.65 12
122 764545.00 12
99 2500.98 6
...