我的方法比较笨,先用case when 语句来逐个的求出各个字符中on的个数。然后。再插入到一个表中,最后用max函数
with a as( select 'BXZS'col_name,sum(case when bxzs='on' then 1 end) num from biao union all select 'XTJS',sum(case when xtjs='on' then 1 end) num from biao)--获得结果集 select col_name from a where num=(select max(num) from a)
select case when c1 > c2 then 'col1' else 'col2' end column_name from (select sum(case when col1 = 'on' then 1 else 0 end) c1, sum(case when col2 = 'on' then 1 else 0 end) c2 from table_name)
select 'BXZS'col_name,sum(case when bxzs='on' then 1 end) num from biao
union all
select 'XTJS',sum(case when xtjs='on' then 1 end) num from biao)--获得结果集
select col_name from a where num=(select max(num) from a)
select case
when c1 > c2 then
'col1'
else
'col2'
end column_name
from (select sum(case
when col1 = 'on' then
1
else
0
end) c1,
sum(case
when col2 = 'on' then
1
else
0
end) c2
from table_name)