我在SQL中对Order By遇到了麻烦。
我想让我的结果(见下文)从以下sql按desc顺序按以下各列进行排序,但在整个sql末尾应用该顺序仅按第一列进行排序,而忽略其余部分
这些列是; FinPen,CWPPen,GAPPen,EGPen,WarPen和IPD。但是,如果您查看结果,则仅由FinPen进行排序。
如何更改我的sql以获取所需的结果?
SQL代码:
WITH tbl_All AS
(
SELECT dealername,
(CASE WHEN backsell = 0 AND isnonopportunity = 0 AND financeopportunity = 1 THEN units END) AS units,
(CASE WHEN backsell = 0 AND isnonopportunity = 0 AND financeopportunity = 1 AND financetype IS NOT NULL THEN units END) AS numadvance,
(CASE WHEN cwpcommission <> 0 THEN 1.0 ELSE 0.0 END) AS totalppi,
(CASE WHEN gapsell <> 0 THEN 1.0 END) AS totalgap,
(CASE WHEN supagardsell <> 0 THEN 1.0 END) AS totalsuper,
(CASE WHEN warrantysell <> 0 AND financeopportunity = 1 AND delivered = 1 THEN 1.0 END) AS totalwar,
(financecommission + warrantycommission + gapcommission + cwpcommission + supagardcommission + volumebonus - subsidy) AS totalipd
FROM tbl_synergy_controllogs
WHERE period >= '01/01/2018'
AND period <= '10/01/2018'
AND dealername NOT LIKE '%comm%'
AND newcar = '1'
), tbl_SUM AS
(
SELECT dealername AS udm,
sum(units) AS units,
dealername AS fdm,
cast(count(numadvance) AS decimal(18, 2)) AS numadvance,
dealername AS cdm,
cast(count(totalppi) AS decimal(18, 2)) AS totppi,
dealername AS gdm,
cast(count(totalgap) AS decimal(18, 2)) AS totgap,
dealername AS edm,
cast(count(totalsuper) AS decimal(18, 2)) AS totsup,
dealername AS wdm,
cast(count(totalwar) AS decimal(18, 2)) AS totwar,
dealername AS idm,
sum(totalipd) AS totalipd
FROM tbl_all
GROUP BY dealername
), tbl_CALC AS
(
SELECT udm,
units,
fdm,
numadvance / units AS finpen,
cdm,
totppi / units AS cwppen,
gdm,
totgap / units AS gappen,
edm,
totsup / units AS egpen,
wdm,
totwar / units AS warpen,
idm,
totalipd / units AS ipd
FROM tbl_sum
)
SELECT *
FROM tbl_calc
ORDER BY finpen DESC, cwppen DESC, gappen DESC, egpen DESC, warpen DESC, ipd DESC
结果:
UDM | Units | FDM | FinPen | CDM |CWPPen |GDM |GAPPen |EDM |EGPen |WDM |WarPen |IDM |IPD
which co | 237 | which co | 0.928270042 | which co |1.008438819 |which co |0.827004219 |which co |0.405063291 |which co |0.004219409 |which co |569.924303
that co | 117 | that co | 0.914529915 | that co |1.034188034 |that co |0.743589744 |that co |0.367521368 |that co |0.094017094 |that co |544.309743
what co | 208 | what co | 0.908653846 | what co |1.028846154 |what co |0.783653846 |what co |0.557692308 |what co |0.009615385 |what co |622.47375
zzz co | 161 | zzz co | 0.906832298 | zzz co |1.01863354 |zzz co |0.639751553 |zzz co |0.49068323 |zzz co |0.130434783 |zzz co |433.015714
def co | 177 | def co | 0.892655367 | def co |1.04519774 |def co |0.570621469 |def co |0.463276836 |def co |0 |def co |387.078305
abc co | 211 | abc co | 0.890995261 | abc co |1.08056872 |abc co |0.663507109 |abc co |0.777251185 |abc co |0 |abc co |466.44
请有人帮我,最好提供一些示例代码/示例?
谢谢
PS。抱歉,很长的帖子。