SQL计数来自2个表

时间:2012-06-19 15:12:17

标签: sql

背景:有多个与POG.ID关联的DBPArentProductKE。

我需要计算发生的Pog.DBKEY的数量。现在,结果计算了值为4的总数据库中的总POG.ID,但我想要与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

1 个答案:

答案 0 :(得分:1)

pog.DBKey中取出GROUP BY

另外,我认为你错过了一个连接条件。您对表pro没有加入条件。

最后,不需要DISTINCT