我正在寻找解决方案(MS SQL 2008,顺便说一句):
ID | ParentID | Feature_1 | Feature_2
+-----+------------+------------+----------+
1 | NULL | A | B
2 | 1 | A | B
3 | 1 | A | C
4 | 2 | A | C
每当一个子(具有ParentID的记录)具有与其父级相同的一组功能(Feature_1和Feature_2)时,我想忽略它,基本上不会在我的select *中显示它。
所以结果集应该是
ID | ParentID | Feature_1 | Feature_2
+-----+------------+------------+----------+
1 | NULL | A | B
3 | 1 | A | C
4 | 2 | A | C
请注意,ID = 2会被删除,但会显示ID = 4,因为它具有与其父级不同的一组功能。
非常感谢任何帮助!
答案 0 :(得分:1)
SELECT
Child.ID,
Child.ParentID,
Child.Feature_1,
Child.Feature_2
FROM
MyTable AS Child
LEFT OUTER JOIN MyTable AS Parent
ON Child.ParentID = Parent.ID
WHERE
Parent.Feature_1 <> Child.Feature_1
OR Parent.Feature_2 <> Child.Feature_2
OR Child.ParentID IS NULL
ORDER BY
Child.ID
答案 1 :(得分:0)
SELECT *
FROM table A
WHERE a.ParentID IS NULL OR NOT EXISTS (SELECT 1
FROM table b
WHERE a.ParentID = b.ID
AND a.Feature_1 = b.Feature_1 AND a.Feature_2 = b.Feature_2)