我有這個表:需要幫助設計一個合適的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