我有一个带有属性
的表count
我想选择一个具有最大价值的列。我该怎么做?
计数表示页面命中,我想选择一个具有最大命中数的列并显示它。
答案 0 :(得分:5)
您可以使用双嵌套子查询
select
(select max(count1)
from (
select count1 union all
select count2 union all
select count3 union all
select count4 union all
select count5) X) as MaxCount
from tbl
答案 1 :(得分:0)
有几种方法,涉及PIVOT
或Temp Table
,但我认为最容易理解的是使用Case
。
SELECT
CASE
WHEN count1 >= count2 AND count2 >= count3 AND count1 >= count4 AND count1 >= count5 THEN count1
WHEN count2 >= count3 AND count2 >= count4 AND count2 >= count5 THEN count2
WHEN count3 >= count4 AND count3 >= count5 THEN count3
WHEN count4 >= count5 THEN count4
ELSE count5
END AS highestCount
答案 2 :(得分:0)
select MAX(max_count) FROM
(
select count1 as max_count from count
UNION
select count2 as max_count from count
UNION
select count3 as max_count from count
UNION
select count4 as max_count from count
UNION
select count4 as max_count from count
)
我不建议使用Select,因为生成相同需要花费大量时间。触发器会更好