MAX不工作我有一個SQL查詢如預期甲骨文
SELECT spt.paymenttype,
MAX(nest.paytypetotal) total
FROM sportpaymenttype spt
INNER JOIN (SELECT spt.paymenttype,
SUM(sod.detailunitprice * sod.detailquantity) paytypetotal
FROM sportorderdetail sod
INNER JOIN sportorder so ON so.orderid = sod.orderid
INNER JOIN sportpaymenttype spt ON spt.paymenttype = so.paymenttype
GROUP BY spt.paymenttype) nest ON nest.paymenttype = spt.paymenttype
GROUP BY spt.paymenttype;
我希望它返回一行(因爲MAX功能),但是,它返回4行。我想出了一個很好的方法來正確地做到這一點,但我想知道爲什麼最大功能是這樣做的?
而且,這些都是結果,在這裏我只希望第一個
PAYMENTTYPE TOTAL
Loan 8640.95
Check 147.34
Credit Card 479.93
Cash 25.95
我想知道是如果有一個更好的方式來做到這一點...
SELECT spt.paymenttype,
nest.paytypetotal total
FROM sportpaymenttype spt
INNER JOIN (SELECT spt.paymenttype,
SUM(sod.detailunitprice * sod.detailquantity) paytypetotal
FROM sportorderdetail sod
INNER JOIN sportorder so ON so.orderid = sod.orderid
INNER JOIN sportpaymenttype spt ON spt.paymenttype = so.paymenttype
GROUP BY spt.paymenttype) nest ON nest.paymenttype = spt.paymenttype
WHERE nest.paytypetotal = (SELECT MAX(nest.paytypetotal)
FROM (SELECT spt.paymenttype,
SUM(sod.detailunitprice * sod.detailquantity) paytypetotal
FROM sportorderdetail sod
INNER JOIN sportorder so ON so.orderid = sod.orderid
INNER JOIN sportpaymenttype spt ON spt.paymenttype = so.paymenttype
GROUP BY spt.paymenttype) nest);
謝謝。
示例SQL的工作原理是:爲每個付款類型返回一行 –
擺脫'SELECT'子句中的'spt.paymenttype',擺脫整個'GROUP BY'子句。瞧。 – NullUserException