问题描述
|
有2列包含相同类型的数据:
col_a col_b ...
Bob Tim ..
John Frank .
Bob John
John Tim
Bob Bob
Frank John
现在我要执行的查询是这样的(计数出现次数:(Bob,3),(John,2),..):
SELECT col_a,count(*) AS total FROM table1 GROUP BY col_a
但是,我不想在col_a上同时运行它,而是想同时在col_a和col_b上运行它((Bob,4),(John,4),..)
任何帮助表示赞赏
编辑:
谢谢大家。
再次感谢
解决方法
Select Z.name,Count(*) As Total
From (
Select col_a As name
From total
Union All
Select col_b
From total
) As Z
Group By Z.name
,select Name,count(*) as Total
from (
select col_a as Name from MyTable
union all
select col_b from MyTable
) a
group by Name
,根据您的“ 4”列,我认为您需要对子查询进行分组:
select idx,sum(count)
from (
select col_a as idx,count(*) as count
from table
union all
select col_b as idx,count(*) as count
where col_a <> col_b -- avoid dups
) as counts
group by idx
要么:
select idx,count(*)
from (
select col_a as idx
from table
union all
select col_b as idx
where col_a <> col_b -- avoid dups
) as counts
group by idx