本质上我有一个有两列的表
One Two
-----------
A B
B C
C D
我想计算A到D的数量。
结果
Letter Count
---------------
A 1
B 2
C 2
D 1
我的代码现在是
Select one, count("") from table
group by one
union
Select two, count("*") from table
group by two
现在我正在
Letter Count
---------------
A 1
B 1
B 1
C 1
C 1
D 1
我该如何解决?
答案 0 :(得分:3)
试试这个
SELECT Count(a),
a
FROM (SELECT cola a
FROM table
UNION ALL
SELECT colb a
FROM table) c
GROUP BY a
答案 1 :(得分:1)
没有理由进行两次分组。
select letter, count(*) as total_cnt
from
(
Select one as letter from table
union all
Select two as letter from table
)
group by letter;
答案 2 :(得分:1)
select letter, sum(total) from
(
Select one as letter, count(1) as total from tablename
group by one
union all
Select two as letter, count(1) as total from tablename
group by two) as t1
group by t1.letter
order by t1.letter asc
答案 3 :(得分:0)
您可以执行此操作
SELECT LETTER,
count(*) AS _count
FROM (
SELECT One AS Letter
FROM Test
UNION ALL
SELECT Two AS Letter
FROM Test
) T1
GROUP BY LETTER