我得到的總存款金額錯誤。 我想輸出每個campaign_name 總存款的報告,並最終在日期範圍內輸出。爲左連接和GROUP BY獲取不正確的SUM
SELECT IFNULL(campaign_name,'DIRECT'),
IFNULL(TotalDeposit,0)
FROM trackings
LEFT JOIN
(SELECT deposit_amount,
sum(deposit_amount) AS TotalDeposit,
uuid
FROM conversions
LEFT JOIN transactions ON conversions.trader_id = transactions.trader_id
WHERE aff_id =3
AND TYPE='deposit'
GROUP BY transactions.trader_id) AS conversions ON trackings.uuid = conversions.uuid
WHERE aff_id=3
GROUP BY campaign_name
結果:trynow活動中缺少200個?
campaign_name,TotalDeposit
DIRECT,0.00
new_campaign_name,0.00
test march,500.00
testing,0.00
trynow,800.00
預計業績:
campaign_name,TotalDeposit
DIRECT,0.00
new_campaign_name,0.00
test march,500.00
testing,0.00
trynow,1000.00
將樣本數據填充到SqlFiddle(http://sqlfiddle.com/)中可能會有所幫助,以便人們可以隨意使用它。我知道我通常能夠更好地瞭解查看數據的內容,而不僅僅是查看查詢! – 2015-03-30 18:50:45
沒有樣本數據,您的預期結果是無用的。 – 2015-03-30 18:53:55
嵌套選擇看起來不一致。它僅由一個字段transactions.trader_id分組,同時SELECT子句中還有兩個字段。 – heximal 2015-03-30 18:55:13