读书人

如何能实现多字段不重复项查询

发布时间: 2012-04-18 15:01:59 作者: rapoo

怎么能实现多字段不重复项查询
表示例:
id c1 c2 c3 c4 c5 c6
1 a c f a d d
2 a d f g c a
3 b e g a b r
4 b a g h j e
5 a e c c d q

查询所有c1 - c6 无重复项(a,b,c,d...)的总数目。以及每个项(a,b,c,d...)的出现次数。

[解决办法]

SQL code
 
--无重复项(a,b,c,d...)的总数目
select count(*)
from(select c1 from table
union
select c2 from table
union
select c3 from table
union
select c4 from table
union
select c5 from table
union
select c6 from table) as T


--每个项(a,b,c,d...)的出现次数
select c1,count(*)
from(select c1 from table
union all
select c2 from table
union all
select c3 from table
union all
select c4 from table
union all
select c5 from table
union all
select c6 from table) as T
group by c1

读书人网 >SQL Server

热点推荐