我有这个表:需要帮助设计一个合适的SQL查询
DebitDate | DebitTypeID | DebitPrice | DebitQuantity
----------------------------------------------------
40577 1 50 3
40577 1 100 1
40577 2 75 2
40578 1 50 2
40578 2 150 2
我想用一个单一的查询来获取(如果可能的话),这些细节: 日期,debit_id,total_sum_of_same_debit,how_many_debits_per_day
所以从上面的例子中我会得到:
40577, 1, (50*3)+(100*1), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40577, 2, (75*2), 2 (because 40577 has 1 and 2 so total of 2 debits per this day)
40578, 1, (50*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)
40578, 2, (150*2), 2 (because 40578 has 1 and 2 so total of 2 debits per this day)
所以我有这样的SQL查询:
SELECT DebitDate, DebitTypeID, SUM(DebitPrice*DebitQuantity) AS TotalSum
FROM DebitsList
GROUP BY DebitDate, DebitTypeID, DebitPrice, DebitQuantity
现在我有麻烦了,我不确定在哪里把我需要的最后信息的计数。
根据你想要的记录必须分组? - 我的意思是你需要将记录与Debitdate或DebitTypeID分组。你不能把他们两个。 – 2011-02-03 10:16:24