目前我有9列
Brand, ID, M, T, W, Th, Fri, Sat, Sun
abc, 1, 2, 4, 6, 3, 7, 1, 5
我想找出以下哪些群体的个人数量最多。 m / t / w / th,fri,sat / sun。例如,在上表中,将选择星期五,因为它具有最高计数。
答案 0 :(得分:0)
假设值中没有关系,您可以使用greatest
和case
表达式执行此操作。如果存在关联,则会返回case
表达式中遇到的第一个值(在关联天数内)。
select case greatest(M, T, W, Th, Fri, Sat, Sun)
when M then 'Monday'
when T then 'Tuesday'
when W then 'Wednesday'
when Th then 'Thursday'
when Fri then 'Friday'
when Sat then 'Saturday'
when Sun then 'Sunday'
end
from tbl
答案 1 :(得分:0)
SELECT BRAND, ID, GREATEST (M, T, W, Th, Fri, Sat, Sun) Max_day
FROM yourtable;
答案 2 :(得分:0)
select brand, ID,
case
when M>T and M>W and M>Th and M>Fri and M>Sat and M>Sun then M
when T>M and T>W and T>Th and T>Fri and T>Sat and T>Sun then T
when W>T and W>M and W>Th and W>Fri and W>Sat and W>Sun then W
when TH>T and TH>W and TH>M and TH>Fri and TH>Sat and TH>Sun then TH
when FRI>T and FRI>W and FRI>Th and FRI>M and FRI>Sat and FRI>Sun then FRI
when SAT>T and SAT>W and SAT>Th and SAT>Fri and SAT>M and SAT>Sun then SAT
when SUN>T and SUN>W and SUN>Th and SUN>Fri and SUN>Sat and SUN>M then SUN
ELSE M END AS MAXDAY
FROM TBL1;