对于您的示例 4 案例,这样的事情应该这样做:
select 'W' as [Value], count(*) as Count from Table1
where Col1='W' or Col2='W' or Col3='W' or Col4='W' union
select 'X' as [Value], count(*) as Count from Table1
where Col1='X' or Col2='X' or Col3='X' or Col4='X' union
select 'Y' as [Value], count(*) as Count from Table1
where Col1='Y' or Col2='Y' or Col3='Y' or Col4='Y' union
select 'Z' as [Value], count(*) as Count from Table1
where Col1='Z' or Col2='Z' or Col3='Z' or Col4='Z'
演示:http ://www.sqlfiddle.com/#!3/68aa3/11
编辑: 如果有很多可能的值,可以通过合并所有可能的值并动态计算它们来进一步简化:
select V.Value as [Value],
count(IIF(col1=[Value] or col2=[Value]
or col3=[Value] or col4=[Value],1,NULL)) as [Count]
from Table1, (
select distinct col1 as [Value] from table1 union
select distinct col2 from table1 union
select distinct col3 from table1 union
select distinct col4 from table1) V
where V.value is not null
group by V.value