0
我有這些表在我的數據庫:如何在SQL Server中查詢獲取客戶帳戶狀態?
*custmrsTble: (custID: PK,Int) - (Name: varchar(20))
=============
custID Name
________________
1 Sam
2 Tom
ProductsTble: (prodID: PK,Int) - (prodName: varchar(20)) - (soldPrice: money)
===========
prodID prodName soldPrice
____________________________
1 Biscuits 20
2 Butter 30
3 Milk 10
OrdersTbl: (orderID: PK,Int) - (orderDate: smallDateTime) - (custID: FK,Int)
==========
orderID orderDate custID
____________________________
1 2013/4/2 1
2 2013/4/2 2
3 2013/4/3 1
OrderDetails: (orderDetailsID : PK,Int)- (orderID: FK,Int) - (prodID: FK,Int) - (qntty: int)
=============
orderDetailsID orderID prodID qntty
_______________________________________
1 1 1 2
2 1 2 1
3 1 3 2
4 2 1 5
5 3 1 1
CashMoventsTble: (cashID : PK,Int)- (orderID: FK,Int) - (cashDate : smallDateTime) - (cashValue money)
================
cashID orderID cashDate cashValue
_____________________________________
1 1 2013/4/2 30
2 2 2013/4/2 100
3 1 2013/4/5 20
4
所以,我想查詢將返回類似這樣的客戶狀態:
Name TotalPurchase TotalPayments
_______________________________________
Sam 110 50
Tom 100 100
TotalPurchase = SUM(qntty)* soldPrice ---- >所有購買的每個客戶
TotalPayments =總額(cashValue)----->每個客戶的所有付款
但不知道如何做到這一點。所以請幫助我實現這一點。 謝謝。
您需要爲集合函數使用「group by」子句。 – OldProgrammer 2013-05-02 12:55:17
OldProgrammer:當我這樣做時,它會向我展示每件產品,而我需要的是摘要而不是細節,正如我之前提到的 – 2013-05-02 13:01:31