Please test this:-- create a sample table variabledeclare @Sample table( A char(1), B char(1))-- insert sample datainsert @Sampleselect 'Y', '1' union allselect 'Y', '1' union allselect 'Y', '2' union allselect 'Y', '3' union allselect 'Y', '4' union allselect 'Y', '4' union allselect 'Y', '4' union allselect 'N', '1' union allselect 'N', '1' union allselect 'N', '1' union allselect 'N', '2' union allselect 'N', '2'--I would like the output to be ------------------------------------------------------| Col A | Num of Col A | Col B | Num of Col B |------------------------------------------------------| Y | 7 | 1 | 2 | --| Y | 7 | 2 | 1 |--| Y | 7 | 3 | 1 |--| Y | 7 | 4 | 3 |--| N | 5 | 1 | 3 |--| N | 5 | 2 | 2 |------------------------------------------------------ try a solution using derived tablesselectdt1.A,dt1.NumOfColA,dt2.B,dt2.NumOfColBfrom(selectA,count(*) as NumOfColAfrom @Samplegroup by A)dt1left join(selectA,B,count(*) as NumOfColBfrom @Samplegroup by A,B)dt2on dt1.A = dt2.A-- resultA NumOfColA B NumOfColB---- ----------- ---- -----------N 5 1 3N 5 2 2Y 7 1 2Y 7 2 1Y 7 3 1Y 7 4 3(6 row(s) affected)
No, you're never too old to Yak'n'Roll if you're too young to die.