SELECT
TreatableComments, Category, Comment_Date, Comment_Time, Reported_Url
FROM
TestData.dbo.CXA
GROUP BY
TreatableComments, Category, Comment_Date, Comment_time, Reported_Url
HAVING
COUNT(TreatableComments) > 1 AND Category IS NULL
答案 0 :(得分:0)
看到您的问题没有数据库系统标签,我想在SQL Server(2016)中快速尝试一下:
--Data
DECLARE @table TABLE (TreatableComments VARCHAR(50), Category VARCHAR(50), CommentDate DATE, Reported_Url VARCHAR(50));
INSERT INTO @table
SELECT 'Stuff', 'Category 1', '20180101', 'stackoverflow.com'
UNION ALL
SELECT 'More Stuff', 'Category 2', '20180103', 'stackoverflow.com'
UNION ALL
SELECT 'Same', NULL, '20180101', 'stackoverflow.com'
UNION ALL
SELECT 'Same', NULL, '20180101', 'stackoverflow.com';
--Query
SELECT
TreatableComments,
Category,
CommentDate,
Reported_Url
FROM
@table
GROUP BY
TreatableComments,
Category,
CommentDate,
Reported_Url
HAVING
COUNT(TreatableComments) > 1
AND Category IS NULL;
当我运行它时,即它给了我一行:
TreatableComments Category CommentDate Reported_Url
Same NULL 2018-01-01 stackoverflow.com
因此,这意味着您的数据看起来与我的数据完全不同,并且没有任何带有NULL类别的重复项,或者您使用的数据库在这方面与SQL Server不同,因此tbh似乎不太可能,因为这是非常普通的SQL。
看到您真正想要的与您最初提出的问题完全不同,这是一个新答案:
--Data
DECLARE @table TABLE (TreatableComments VARCHAR(50), Category VARCHAR(50), CommentDate DATE, Reported_Url VARCHAR(50));
INSERT INTO @table
SELECT 'Stuff', 'Category 1', '20180101', 'stackoverflow.com'
UNION ALL
SELECT 'More Stuff', 'Category 2', '20180103', 'stackoverflow.com'
UNION ALL
SELECT 'Same', 'A', '20180101', 'stackoverflow.com'
UNION ALL
SELECT 'Same', NULL, '20180101', 'stackoverflow.com';
--Query
SELECT
t.*
FROM
@table t
INNER JOIN (
SELECT
TreatableComments,
CommentDate,
Reported_Url
FROM
@table
GROUP BY
TreatableComments,
CommentDate,
Reported_Url
HAVING
COUNT(TreatableComments) > 1) d ON d.TreatableComments = t.TreatableComments AND d.CommentDate = t.CommentDate AND t.Reported_Url = d.Reported_Url
WHERE
t.Category IS NULL;
这可以通过查找重复的案例,然后再联接回主表并仅选择类别为NULL
的行来实现。
新结果是:
TreatableComments Category CommentDate Reported_Url
Same NULL 2018-01-01 stackoverflow.com