-1

此查询返回与 POG.Dbkey 相关的产品数量。我需要找到所有重复项以比较哪些具有相同的产品,哪些具有相同数量的产品。

--每个货架图中的总产品

select pog.DBKey
,Count(pos.DBParentProductKey)
as Total
from ix_spc_planogram as pog with (nolock)
, ix_spc_position as pos with (nolock)
where pog.dbkey = pos.dbparentplanogramkey
and pog.Value4= 358
group by  pog.dbkey 
4

2 回答 2

1

如果我理解你的问题(prolly not),你可以像这样选择重复的数量:

select pog.DBKey
,Count(pos.DBParentProductKey) as 'Total'
,Count(pog.DBKey) - 1 as 'Number Duplicates'
as Total
from ix_spc_planogram as pog with (nolock)
, ix_spc_position as pos with (nolock)
where pog.dbkey = pos.dbparentplanogramkey
and pog.Value4= 358
group by  pog.dbkey

或像这样限制它HAVING

select pog.DBKey
,Count(pos.DBParentProductKey) as 'Total'
as Total
from ix_spc_planogram as pog with (nolock)
, ix_spc_position as pos with (nolock)
where pog.dbkey = pos.dbparentplanogramkey
and pog.Value4= 358
group by  pog.dbkey
having count(pog.dbkey) > 1
于 2012-06-19T16:52:27.620 回答
0

这会让你开始。

这是我在需要从表中选择/删除重复项时使用的模板:

Select  *
--DELETE
FROM    Table
WHERE   Table.TableID IN
(
    -- List 1 - all rows that have duplicates
    SELECT t1.TableID
    FROM Table t1
    WHERE Exists
    (
        SELECT UniqueField1, UniqueField2, Count(TableID)
        FROM Table
        WHERE Table.UniqueField1 = t1.UniqueField1
        AND UniqueField2 = t1.UniqueField2
        GROUP BY UniqueField1, UniqueField2
        HAVING Count(TableID) > 1
    )
)
AND TableID NOT IN
(
    -- List 2 - one row from each set of duplicate
    SELECT Max(TableID)
    FROM Table t2
    WHERE Exists
    (
        SELECT UniqueField1, UniqueField2, Count(TableID)
        FROM Table
        WHERE UniqueField1 = t2.UniqueField1
        AND UniqueField2 = t2.UniqueField2
        GROUP BY UniqueField1, UniqueField2
        HAVING Count(TableID) > 1
    )
    GROUP BY UniqueField1, UniqueField2
)
于 2012-06-19T18:09:52.940 回答