Question : Count number of times a value appears within rows returned

Hi,

I'd like to add a counter to the following table that counts the vCodealternative
 SELECT DISTINCT vSubCodeAlternative,vCodeAlternative
      FROM         dbo.tbProduct
      WHERE     (iProductGroupId = @iProductGroupid) AND (vSubCodeAlternative <> '' AND NOT (vSubCodeAlternative IS NULL))
      order by vCodeAlternative,vsubCodeAlternative

returns
vSubCodeAlternative,vCodeAlternative:
subcode1 code1
subcode2 code1
subcodeA code2

would like it to return:
vSubCodeAlternative,vCodeAlternative,counter
subcode1 code  2
subcode2 code1  2
subcodeA code2  1

Thanks

Answer : Count number of times a value appears within rows returned

SELECT x.vSubCodeAlternative,x.vCodeAlternative, y.Counter
FROM
(
SELECT DISTINCT vSubCodeAlternative,vCodeAlternative
      FROM         dbo.tbProduct
      WHERE     (iProductGroupId = @iProductGroupid) AND (vSubCodeAlternative <> '')
) X,
(
      SELECT vCodeAlternative, COUNT(distinct vSubCodeAlternative) as counter
      FROM         dbo.tbProduct
      WHERE     (iProductGroupId = @iProductGroupid) AND (vSubCodeAlternative <> '')
      GROUP BY vCodeAlternative
) Y
where x.vCodeAlternative=Y.vCodeAlternative
order by x.vCodeAlternative,x.vsubCodeAlternative
Random Solutions  
 
programming4us programming4us