SELECT DISTINCT(code), connection, standing
FROM data
WHERE code IN (SELECT code
FROM data
GROUP BY code
HAVING COUNT(*)>1)
ORDER BY code
我的问题是它显示所有重复项。
基本上,我想显示重复项,如果它们在连接或站立时具有不同的值,但它也显示重复项(由于DISTINCT而仅在1行中),在连线或立场上具有相同的值。
我拥有和想要的东西:
Code Connection Standing
----------------------------
XXX YYY YYY
XXX ZZZ YYY
但是我也有:
Code Connection Standing
---------------------------
WWW YYY YYY
由于不同,该代码仅存在一次。
那么我可以过滤掉不重复的结果吗?
不确定它是否可以理解,但我希望如此。
编辑:添加更多关于我的问题的示例:
Code Connection Standing
---------------------------
XXX YYY YYY
XXX ZZZ YYY
YYY SSS YYY
YYY SSS ZZZ
TTT SSS YYY
我想摆脱TTT
行,因为它并不是我要找的重复项。我想查看在连接或站立状态下具有不同值的重复项。
答案 0 :(得分:0)
我相信您要寻找的东西涉及三个主要步骤:
实现这一目标的一种方法是借助Common Table Expressions和Subqueries:
WITH CTE AS (
SELECT DISTINCT Code, Connection,Standing FROM @T)
SELECT CTE.Code,Connection, Standing FROM CTE INNER JOIN
(SELECT Code FROM CTE GROUP BY Code HAVING COUNT(*) >1) AS tCode ON tCode.Code = CTE.Code
另一种方法可能是使用CTE和Window Functions:
WITH CTE AS (
SELECT DISTINCT Code, Connection,Standing FROM @T),
CTE2 as (
SELECT CTE.Code,Connection, Standing, COUNT(*) OVER (PARTITION BY Code) as CodeCount
FROM CTE)
SELECT Code, Connection, Standing FROM CTE2 WHERE CodeCount > 1
最后一个选项可能会更强大,具体取决于您要执行的操作。例如,如果您想知道与某个“代码”值相关的“连接”值或“站立”值存在多少次:
WITH CTE AS (
SELECT DISTINCT Code, Connection,Standing FROM @T),
CTE2 as (
SELECT CTE.Code,Connection, Standing, COUNT(*) OVER (PARTITION BY Code) as CodeCount,
COUNT(*) OVER (PARTITION BY Code, Connection)
as ConnectionByCodeCount, COUNT(*) OVER (PARTITION BY Code, Standing)
as StandingByCodeCount
FROM CTE)
SELECT Code, Connection, ConnectionByCodeCount, Standing, StandingByCodeCount FROM CTE2 WHERE CodeCount > 1
会返回:
Code| Connection| ConnectionByCodeCount | Standing| StandingByCodeCount
----| ----------| -----------------------| --------| -------------------
XXX | YYY | 1 | YYY | 2 (times YYY, Code XXX)
XXX | ZZZ | 1 | YYY | 2
YYY | SSS | 2 (times SSS, Code YYY)| YYY | 1
YYY | SSS | 2 | ZZZ | 1