简体   繁体   中英

Finding the column with highest value within one row

This is how the table looks like

Jan | Feb | Mar | ~
10  | 20  | 10  | ~

I can find the highest value using GREATEST function.

SELECT GREATEST(Jan,Feb,Mar,~) FROM table

Now, I want to find the month with the highest value. I can try to compare the result of GREATEST function with individual column values. However, I feel that there should be a more elegant and easy to read solution.

Here's another way that works for me:

SELECT month FROM t1
WHERE num =
(
  SELECT MAX(num)FROM t1
);

http://sqlfiddle.com/#!6/87ffd/12/0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM