Ich habe eine Frage, wie ich nach Reihenfolge bestellen kann
select * from (select @fYear as [Year],
(case when main.Description in ("Kecil", "Tanah") then "JK"
else main.Description
end) as description,
--CardType,
sum(case when MONTH(blue.AppliedDate) = 1 then 1 else 0 end) as Jan_Collection,
sum(case when MONTH(blue.AppliedDate) = 2 then 1 else 0 end) as Feb_Collection,
...
from tblR as main
left join tblP as b on main.requestorid = b.requestorid
left join tblB as blue on b.partyid = blue.partyid and YEAR(blue.AppliedDate) = @fYear
group by (case when description in ("Kecil", "Tanah") then "JK"
else main.Description
Ende)
) t
order by (case when t.description = "Amanah" then 1
when t.description = "Mah" then 2
when t.description = "JK" then 3
END) ASC
Diese Ausgabe sieht folgendermaßen aus:
müssen der Reihenfolge folgen wie:
Ich bekomme diesen Fehler:
ist in der ORDER BY-Klausel ungültig, da sie nicht in enthalten ist entweder eine Aggregatfunktion oder die GROUP BY-Klausel.
Danke allen.
Antworten:
-1 für die Antwort № 1Verwenden Sie stattdessen die folgende Abfrage:
select * from (select @fYear as [Year],
(case when main.Description in ("Kecil", "Tanah") then "JK"
else main.Description
end) as description,
--CardType,
sum(case when MONTH(blue.AppliedDate) = 1 then 1 else 0 end) as Jan_Collection,
sum(case when MONTH(blue.AppliedDate) = 2 then 1 else 0 end) as Feb_Collection,
...
from tblR as main
left join tblP as b on main.requestorid = b.requestorid
left join tblB as blue on b.partyid = blue.partyid and YEAR(blue.AppliedDate) = @fYear
group by (case when description in ("Kecil", "Tanah") then "JK"
else main.Description
end)
) t
order by (case when t.description = "Amanah" then 1
when t.description = "Mah" then 2
when t.description = "JK" then 3
END) ASC