[英]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. 我可以使用
GREATEST
函数找到最大值。
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. 我可以尝试将
GREATEST
函数的结果与各个列值进行比较。 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 http://sqlfiddle.com/#!6/87ffd/12/0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.