Skip to content
Advertisement

Return column name of max value in a row SQL Server

Initial Table:

Output Table:

I need to get the Job Name (column name) of the row with the max value in SQL Server. Is there a way other than else if to do this because ‘greatest’ function is not available in SQL? (Coz I have 50 Jobs at least)

Advertisement

Answer

You can use apply and window functions:

That said, you should probably fix your data model. Having multiple columns with the same information is usually a sign of a deficient data model. Each name/job combination should be a separate row in another table.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement