Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement