Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab;
如果要计算这些计数值的总和,请将上述查询视为内部查询,并使用以下命令:
Select q.*,
q.count_col1 + q.count_col2 + q.count_col3 whole_sum
from
(
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab
) q
1条答案
按热度按时间vyu0f0g11#
可以使用条件聚合:
如果要计算这些计数值的总和,请将上述查询视为内部查询,并使用以下命令:
rextester演示