SQl Server 查询 问题
有一个表有三个字段A,B,C,插入三行数据 第一行1,2,3 第二行2,4,6第三行3,5,4
查询出这个表中各个数字的个数。
求大神
------解决方案--------------------with tb(A,B,C) as
(
select 1,2,3 union all
select 2,4,6 union all
select 3,5,4
)
, tb2 (result) as
(
select A from tb
union all
select B from tb
union all
select C from tb
)
select result, COUNT(result)
from tb2
group by result
------解决方案--------------------你还没下班吗。
这样吗:
create table tb(a int, b int,c int)
insert into tb
select 1,2,3 union all
select 2,4,6 union all
select 3,5,4
go
select x,COUNT(*) as '次数'
from
(
select a x from tb union all
select b from tb union all
select c from tb
)t
group by x
/*
x 次数
1 1
2 2
3 2
4 2
5 1
6 1
*/