看来您正在使用固定值或“r”、“g”和“y”值。如果是这样,这应该适合你。内部“PQ”(预查询)正在获取每一行其他数据元素,然后将每一列与 1 相加,如果它的值分别是“r”、“g”或“y”,则分为 3 个计数列。
然后,我使用“Greatest”函数从提供的给定列表中返回最大值。如果您拥有所有 3 个具有相同颜色的 3 个,它们都将被标记为最高......或者即使 4/4/1 拆分,两个将显示为高计数。
我不知道你的表名,所以我就叫它“freq1”
select
PQ.r_id,
PQ.r_user,
PQ.module,
PQ.gCnt,
PQ.rCnt,
PQ.yCnt,
if( PQ.gCnt = greatest( PQ.gCnt, PQ.rCnt, PQ.yCnt ), 'yes', ' ' ) as HighG,
if( PQ.rCnt = greatest( PQ.gCnt, PQ.rCnt, PQ.yCnt ), 'yes', ' ' ) as HighR,
if( PQ.yCnt = greatest( PQ.gCnt, PQ.rCnt, PQ.yCnt ), 'yes', ' ' ) as HighY,
greatest( PQ.gCnt, PQ.rCnt, PQ.yCnt ) as HighCnt
from
( select
r_id, r_user, module,
if( q_1 = 'g', 1, 0 )
+ if( q_2 = 'g', 1, 0 )
+ if( q_3 = 'g', 1, 0 )
+ if( q_4 = 'g', 1, 0 )
+ if( q_5 = 'g', 1, 0 )
+ if( q_6 = 'g', 1, 0 )
+ if( q_7 = 'g', 1, 0 )
+ if( q_8 = 'g', 1, 0 )
+ if( q_9 = 'g', 1, 0 ) as gCnt,
if( q_1 = 'r', 1, 0 )
+ if( q_2 = 'r', 1, 0 )
+ if( q_3 = 'r', 1, 0 )
+ if( q_4 = 'r', 1, 0 )
+ if( q_5 = 'r', 1, 0 )
+ if( q_6 = 'r', 1, 0 )
+ if( q_7 = 'r', 1, 0 )
+ if( q_8 = 'r', 1, 0 )
+ if( q_9 = 'r', 1, 0 ) as rCnt,
if( q_1 = 'y', 1, 0 )
+ if( q_2 = 'y', 1, 0 )
+ if( q_3 = 'y', 1, 0 )
+ if( q_4 = 'y', 1, 0 )
+ if( q_5 = 'y', 1, 0 )
+ if( q_6 = 'y', 1, 0 )
+ if( q_7 = 'y', 1, 0 )
+ if( q_8 = 'y', 1, 0 )
+ if( q_9 = 'y', 1, 0 ) as yCnt
from freq1 ) PQ
创建的结果
r_id r_user module gCnt rCnt yCnt HighG HighR HighY HighCnt
1 test 1 4 3 2 yes 4
2 test-2 1 2 5 2 yes 5