試試這個:
SELECT captain_name,
Sum(finaldiscount) AS tot,
payment_mode
FROM order_master
WHERE payment_mode IN('cash', 'card')
AND captain_name IN (SELECT captain_name
FROM order_master
WHERE payment_mode IN('cash', 'card')
GROUP BY captain_name
HAVING Count(DISTINCT payment_mode) = 2)
GROUP BY captain_name,payment_mode
子查詢將返回唯一的日其中有payment_mode
,即card
和cash
。然後選擇captain_name
和group by
那些行,按captain_name,payment_mode
。
,如果你不想單獨的行爲card
和cash
然後使用此:
SELECT captain_name,SUM(finaldiscount)
FROM order_master
WHERE payment_mode IN('cash', 'card')
GROUP BY captain_name
HAVING Count(DISTINCT payment_mode) = 2
注:我認爲two captain should be equal
意味着只有那些被兩個查詢返回的隊長,應當由合併返回查詢。
爲什麼要組合相同的查詢?我沒有看到查詢之間的區別。 – 2014-12-05 10:02:12
這兩個查詢看起來都是一樣的 – 2014-12-05 10:02:30
它被錯誤地編輯了,在第一個查詢中有'payment_mode ='card''。 – 2014-12-05 10:04:02