Mam pytanie, jak złożyć zamówienie według sekwencji
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
koniec)
) t
order by (case when t.description = "Amanah" then 1
when t.description = "Mah" then 2
when t.description = "JK" then 3
END) ASC
Ten wynik wygląda następująco:
należy postępować zgodnie z kolejnością zamówień, np .:
Otrzymuję ten błąd:
jest niepoprawny w klauzuli ORDER BY, ponieważ nie jest zawarty w albo funkcja agregująca, albo klauzula GROUP BY.
Dziękuje wszystkim.
Odpowiedzi:
-1 dla odpowiedzi nr 1zamiast tego użyj poniższego zapytania:
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