当前位置: 代码迷 >> Sql Server >> 如何能统计若干列所有数据各自的数量
  详细解决方案

如何能统计若干列所有数据各自的数量

热度:79   发布时间:2016-04-24 10:18:28.0
怎么能统计若干列所有数据各自的数量?
一列的话可以用count和group by ,多几列不懂了。。
比如数据库里有这几列:
a列  b列  c列
1线  3线  2线
1线  6线  3线
2线  4线  1线
3线  1线  8线
4线  1线  4线

怎么能统计若干列,比如3列所有线各自的数量?比如这3列,包含:
1线,5
2线,2
3线,3
4线,3
6线,1
8线,1

大榭~
------解决方案--------------------
用union
select col,count(*)
from (
select a as col
from tb
union all
select b
from tb
union all
select c
from tb) tmp
group by  col
------解决方案--------------------
slect a,count(1) from (select a from 表 
union all
select b from 表
union all
select c from 表)a
group by a
  相关解决方案