我正在寻找一些结合两个查询的帮助:
第一个:
SELECT TITLE,
count(TITLE) as DUPLICATES
FROM CONTENT
WHERE CONTENTTYPE = 'ATTACHMENT' GROUP BY TITLE having count(TITLE) > 1
第二个只是
SELECT CONTENTID,
TITLE
FROM CONTENT
我想将 CONTENTID 视为第一个查询的一部分,但我不确定该怎么做。 这两个查询针对同一个表 CONTENT。
我怀疑您想查看属于附件记录数超过 1 的TITLE
组的所有记录。 我们可以在这里尝试使用COUNT
:
WITH cte AS (
SELECT *, COUNT(*) OVER (PARTITION BY TITLE) cnt
FROM CONTENT
WHERE CONTENTTYPE = 'ATTACHMENT'
)
SELECT CONTENTID, TITLE
FROM cte
WHERE cnt > 1;
此方法使用 COUNT
作为分析函数来标识与您的请求匹配的TITLE
记录。
如果
要查看与重复附件标题关联的行,可以使用窗口函数:
SELECT c.* -- or whatever columns you want
FROM (SELECT c.*, COUNT(*) OVER (PARTITION BY TITLE) as cnt
FROM CONTENT c
WHERE CONTENTTYPE = 'ATTACHMENT'
) c
WHERE cnt >= 2;
SELECT min(CONTENTID) as CONTENTID, TITLE, count(TITLE) as DUPLICATES
FROM CONTENT
WHERE CONTENTTYPE = 'ATTACHMENT'
GROUP BY TITLE
HAVING count(TITLE) > 1