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