SQL to JSON
Уговорил разработчиков не делать лишний сервис, а использовать фичу MSSQL – sql-to-json.
Solution:
with Nominations (NominationID, winnersFirstPlace, winnersSecondPlace, winnersThirdPlace) as ( select NominationID, [1], [2], [3] from ( select NominationID , IssuerID , Pos from Nominations where Pos in (1, 2, 3) group by NominationID , IssuerID , Pos ) s pivot ( max(IssuerID) for Pos in ([1], [2], [3]) ) p ) select an.CYear 'year' , case when an.OpenName like '%сектор%экономи%' then 3 else an.GroupID end 'statistics.nominationType' , an.OpenName 'statistics.nominationName' , (select ai.OpenName from Issuers ai where ai.IssuerID = n.winnersFirstPlace) 'statistics.winnersFirstPlace' , (select ai.OpenName from Issuers ai where ai.IssuerID = n.winnersSecondPlace) 'statistics.winnersSecondPlace' , (select ai.OpenName from Issuers ai where ai.IssuerID = n.winnersThirdPlace) 'statistics.winnersThirdPlace' , json_query(select ai.OpenName from Issuers ai where ai.IssuerID in (n.winnersFirstPlace, n.winnersSecondPlace, n.winnersThirdPlace) for json path) 'statistics.shortList' from Nominations n , ANominations an where an.NominationID = n.NominationID order by an.CYear desc for json path