/ / (stored procedure) Come ordinare una sequenza speciale - sql-server, tsql, stored procedure

(stored procedure) Come ordinare una sequenza speciale - sql-server, tsql, stored-procedures

Ho una domanda su come effettuare un ordine per sequenza

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

fine)

  ) t
order by (case when t.description =  "Amanah" then 1
when t.description = "Mah" then 2
when t.description = "JK" then 3
END) ASC

Questo output è simile a:

inserisci la descrizione dell'immagine qui

è necessario seguire la sequenza dell'ordine come:

inserisci la descrizione dell'immagine qui

Ottengo questo errore:

non è valido nella clausola ORDER BY perché non è contenuto in una funzione aggregata o la clausola GROUP BY.

Ringrazia tutti.

risposte:

-1 per risposta № 1

utilizzare invece la query seguente:

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