I have a table called campaigns that shows the number of 'tiles' each category is displayed in in an email campaign, like below
campaign_id categoryA_tiles categoryB_tiles categoryC_tiles ... cateogory Z_tiles
1 5 7 4 ... 4
2 4 4 9 ... 1
I need to add 3 columns to this table that show the 3 most frequently occurring categories (i.e. the column names) for each email campaign. For example I would like the result to look like this
campaign_id ... category_1 category_2 category_3
1 ... categoryB categoryA categoryZ
2 ... categoryC categoryA categoryB
Note that there may be ties as in the second row, in which case I'd like them to be displayed from left to right.
This is essentially the same as this question but I'm using SSMS 2012 rather than MySQl. This answer is what I'm after but it is incompatible with SSMS. To be specific, the error I can't fix is how @rownum is defined and used.
Can anyone help?