我有下表:
Name | Col1 | Col2 | Col3 John A B C John A D A Bill A A D Bill F A A Steve F A B Steve C C A
我想知道John,Bill和Steve有多少......
约翰:3,比尔:4,史蒂夫:2
我怎么能用T-SQL做到这一点?谢谢.
你可以使用这个UNPIVOT
功能:
select name, count(val) TotalAs
from yourtable
unpivot
(
val
for col in (col1, col2, col3)
) u
where val = 'A'
group by name
Run Code Online (Sandbox Code Playgroud)