我有三個表:SQL如何分組行正確
**Bookie**:
------------
Id : int (Primary Key)
Bookie : varchar
**Transactions**:
-------------------
Id : int (Primary Key)
Date : date
Bookie : int (Foreign Key)
Amount : decimal
Wagers:
Id : int (Primary Key)
Stake : decimal
Bookie : int (Foreign Key)
下注表不是全部,但對於SQL這些列就足夠了。
我想這SQL代碼至今:
SELECT Bookie.Bookie, SUM(Amount), Wagers.Stake FROM Transactions
JOIN Bookie on Transactions.Bookie = Bookie.Id
JOIN Wagers on Bookie.Id = Wagers.Bookie
GROUP BY Bookie.Bookie, Wagers.Stake
但我的輸出是這樣的:
Bookie | Amount | Stake
William Hill | 600.00 | 14.70
Bet at Home | 500.00 | 30.00
Bet at Home | 500.00 | 45.00
Bet at Home | 500.00 | 50.00
Bet365 | 10200.00 | 100.00
它應該是這樣的:
Bookie | Amount | Stake
William Hill | 600.00 | 14.70
Bet at Home | 500.00 | 125.00
Bet365 | 10200.00 | 100.00
有誰知道要實現這個問題?
感謝
,而不是'Wagers.Stake'做'總和(Wagers.Stake)作爲Stake'和Stake' –
@Gohan的數據庫使用的是刪除'組? –
我不明白,但我可以看到別人做的,所以我會把它留給他們。 – Strawberry