日期:2014-05-18  浏览次数:20549 次

求一SQL语句,万分感谢
现有一张表名为T,共有四个字段(C1,C2,C3,C4),有如下数据
 
C1 C2 C3 C4

A A C B
C D B A
D A C B
C C B A
A D C A
D C A B
B A D A
C A D D
D C B A

要求用SQL语句进行统计,实现如下输出

Alphabet CountNum
A 12
B 7
C 9
D 8
 


------解决方案--------------------
amandag(高歌) ( )
是不能统计出来的

字段的值 是在不同的数据列中
------解决方案--------------------
楼上的还是测试一下吧,谢谢提醒,少了All

create table T
(
C1 varchar(10),
C2 varchar(10),
C3 varchar(10),
C4 varchar(10)
)

insert into T values( 'A ', 'A ', 'C ', 'B ')
insert into T values( 'C ', 'D ', 'B ', 'A ')
insert into T values( 'D ', 'A ', 'C ', 'B ')
insert into T values( 'C ', 'C ', 'B ', 'A ')
insert into T values( 'A ', 'D ', 'C ', 'A ')
insert into T values( 'D ', 'C ', 'A ', 'B ')
insert into T values( 'B ', 'A ', 'D ', 'A ')
insert into T values( 'C ', 'A ', 'D ', 'D ')
insert into T values( 'D ', 'C ', 'B ', 'A ')

select c1 as Alphabet , count(c1) AS CountNum
from
(select c1 from T
union all
select c2 from T
union all
select c3 from T
union all
select c4 from T) as A
group by c1