sql计数3列中的值

时间:2016-03-18 12:47:15

标签: sql oracle count

当可能的值可以在0到3列的任何值时,如何计算所有值。我想把所有的a,b,s,c&s,d&#39>加起来

COL1     COL2     COL3
a
b
b         a
a         c        b
a         b
c         a   
d         a        c
c         d      

2 个答案:

答案 0 :(得分:0)

select col, count(*) 
from (
select col1 col from tablename
union all
select col2 from tablename
union all
select col3 from tablename) t 
group by col

答案 1 :(得分:0)

您可以使用UNPIVOT

SELECT t.Val, COUNT(*) AS cnt
FROM (
  SELECT Col1, Col2, Col3
  FROM mytable
  UNPIVOT (
     Val FOR Col IN ("Col1", "Col2", "Col3"))) t
GROU BY t.Val