Create a UDF to do your counting.See code below.create table #ch( ch1 int, ch2 int, ch3 int, ch4 int)insert into #chselect 1, 3, 4, 3 union allselect 7, 7, 7, 3 union allselect 7, 8, 9, 0 create function dbo.count_no( @ch1 int, @ch2 int, @ch3 int, @ch4 int, @cnt_no int)returns intasbegin declare @count int select @count = count(*) from ( select @ch1 as ch union all select @ch2 union all select @ch3 union all select @ch4 ) as c where ch = @cnt_no return @countendselect ch1, ch2, ch3, ch4, dbo.count_no(ch1, ch2, ch3, ch4, 3) as NoOf3s, dbo.count_no(ch1, ch2, ch3, ch4, 7) as NoOf7sfrom #ch
----------------------------------'KH'