如何显示DISTINCT重复SQL

时间:2018-04-05 22:34:56

标签: sql-server

所以当我输入SQL

时,我会显示一个表格
SELECT DISTINCT
     r.CustomerId, o.ItemNumber, ItemIdentifier, o.ItemProductNumber
FROM 
    Owner o
INNER JOIN 
    Report r
ON 
    o.ReportId = r.ReportId
ORDER BY r.CustomerId

所以显示的表是

CustomerId  Item Number ItemIdentifier ItemProductNumber
NULL        NULL        NULL           NULL
NULL        2144        NULL           NULL
1           8797        NULL           NULL
2           6816        NULL           NULL
3           3436        NULL           NULL
4           4436        NULL           NULL
5           3523        NULL           NULL
6           6346        NULL           NULL
7           7452        NULL           NULL
7           7453        NULL           NULL
8           5475        NULL           NULL
9           6816        NULL           NULL

我想要完成的事情,并且遇到麻烦的是获得具有不同客户ID的重复项目编号。在这种情况下,客户2和9.我只想要2和9来显示。我看到很多分组和计数,什么不是,但这似乎不是我正在寻找的。

任何帮助都会很棒。

4 个答案:

答案 0 :(得分:0)

这应该做的伎俩

DECLARE @Table TABLE (CustomerId int, ItemNumber int, ItemIdentifier int, ItemProductNumber int)
INSERT INTO @Table
values(NULL,NULL,NULL,NULL),(NULL,2144,NULL,NULL),(1,8797,NULL,NULL),
(2,6816,NULL,NULL),(3,3436,NULL,NULL),(4,4436,NULL,NULL),(5,3523,NULL,NULL),
(6,6346,NULL,NULL),(7,7452,NULL,NULL),(7,7453,NULL,NULL),(8,5475,NULL,NULL),
(9,6816,NULL,NULL)


SELECT *
FROM @Table
WHERE ItemNumber IN(SELECT ItemNumber
                    FROM @Table
                    GROUP BY ItemNumber
                    HAVING COUNT(ItemNumber) > 1)

如果您仍需要获取DISTINCT条记录,请将其放入CTE或子查询中:

;WITH CTE AS(
SELECT DISTINCT r.CustomerId, o.ItemNumber, ItemIdentifier, 
o.ItemProductNumber
FROM [Owner] o
INNER JOIN Report r ON o.ReportId = r.ReportId
)

SELECT *
FROM CTE
WHERE ItemNumber IN (SELECT ItemNumber
                     FROM CTE
                     GROUP BY ItemNumber
                     HAVING COUNT(ItemNumber) > 1)

答案 1 :(得分:0)

另一个答案是有道理的。

但我想我有时间通过​​修改原始查询来返回不同的ItemNumber来尝试不同的东西,并将其用作过滤器,以便匹配的那些从列表中排除:

select * from (

-- this is your original query

SELECT DISTINCT
     r.CustomerId, o.ItemNumber, ItemIdentifier, o.ItemProductNumber
FROM 
    Owner o
INNER JOIN 
    Report r
ON 
    o.ReportId = r.ReportId)
AS T

-- and we are looking for item number not in the list

WHERE T.ItemNumber not in (

-- this is your original query
-- but modified to return records that are unique

SELECT DISTINCT
     o.ItemNumber
FROM 
    Owner o
INNER JOIN 
    Report r
ON 
    o.ReportId = r.ReportId
group BY o.ItemNumber
having count(o.itemnumber) = 1)

将其视为ALL RECORDS - UNIQUE RECORDS = DUPLICATE RECORDS

SQL小提琴:http://www.sqlfiddle.com/#!9/5396f/34

答案 2 :(得分:0)

这应该有效:

;WITH CTE1 AS (SELECT o.ItemNumber, CountItemNumbers = COUNT(1)
                FROM [Owner] o
                INNER JOIN Report r
                    ON o.ReportId = r.ReportId
                GROUP BY o.ItemNumber
                )

SELECT r.CustomerId, o.ItemNumber, ItemIdentifier, o.ItemProductNumber, C1.CountItemNumbers
FROM CTE1 C1
INNER JOIN [Owner] o
    ON C1.ItemNumber = o.ItemNumber
INNER JOIN Report r
    ON o.ReportId = r.ReportId
WHERE C1.CountItemNumbers > 1

答案 3 :(得分:0)

这是另一种方法。我认为这稍微清晰一点,因为它不会将您的表重新加入到自身中,也不会使用与IN ()相比被证明效率低下的EXISTS ()

您可以使用窗口函数计算ItemNumber的出现次数。在这种情况下COUNT,我们包含OVER(PARTITION BY ItemNumber)来指定分组。

样品:

DECLARE @data TABLE (CusomerId INT, ItemNumber INT, ItemIdentifier INT, ItemProductNumber INT)
INSERT INTO @data
VALUES (NULL,NULL,NULL,NULL),
(NULL,2144,NULL,NULL),
(1,8797,NULL,NULL),
(2,6816,NULL,NULL),
(3,3436,NULL,NULL),
(4,4436,NULL,NULL),
(5,3523,NULL,NULL),
(6,6346,NULL,NULL),
(7,7452,NULL,NULL),
(7,7453,NULL,NULL),
(8,5475,NULL,NULL),
(9,6816,NULL,NULL)

;WITH Duplicates AS
(
    SELECT *, COUNT(2) OVER(PARTITION BY ItemNumber) AS CN
    FROM @data
)

SELECT * -- DISTINCT ItemNumber
FROM Duplicates
WHERE CN>1

要将此应用于您的方案,我们会这样做:

;WITH X AS
(
    SELECT DISTINCT r.CustomerId, o.ItemNumber, ItemIdentifier, o.ItemProductNumber
    FROM Owner o
    INNER JOIN Report r ON o.ReportId = r.ReportId
), Duplicates AS
(
    SELECT *, COUNT(2) OVER(PARTITION BY ItemNumber) AS CN
    FROM X
)

SELECT * -- DISTINCT ItemNumber
FROM Duplicates
WHERE CN>1