Background: There are multiple DBPArentProductKEys associated with a POG.ID.
I need to count the number of Pog.DBKEYs that occurs. Right now the results count total POG.IDs in the total database for value4, but I want the count associated with the DBparentproductkey.
select distinct
Count(pog.DBKey) as Total,
pos.DBParentProductKey
from
ix_spc_planogram as pog with (nolock), ix_spc_position as pos with (nolock),
ix_spc_product as pro with (nolock)
where
pog.dbkey = pos.dbparentplanogramkey
and pog.Value4 = 358
group by
pog.DBKey, pos.DBParentProductKey
Take
pog.DBKeyout of theGROUP BY.Also, I think you are missing a join condition. You have no join condition against table
pro.Finally,
DISTINCTshouldn’t be needed.