我有如下場景:
create table #Example (
id int
, overall_id int
, parent_id int
, child_id int
);
insert into #Example values
(1, 25963, 491575090, 491575090)
,(2, 25963, 547952026, 491575090)
,(3, 25963, 547952026, 230085039)
,(4, 25963, 547952026, 547952026);
select e.*
from #Example as e;
drop table #Example;
我想排除 ID 為“2”的記錄,因為那是它自己的父記錄(參見 ID“1”)。
我不想排除 3,因為子記錄不是它自己的父記錄。而且我不想排除 1 和 4,因為它們是他們自己的父記錄。
一個問題是,在我的業務場景中,我沒有相應的“ID”欄位,這是我在此示例中提供的,以便我可以唯一地參考每一行。
任何有關排除記錄 2 的技術的幫助將不勝感激!
uj5u.com熱心網友回復:
我仍然不明白這個問題,但預期的結果是:
select *
from #Example as E
where not exists (
select 42
from #Example as IE
where
-- There is a row that is self parenting?!
IE.parent_id = IE.child_id and
-- The row under consideration is related in a child/parent way?
IE.child_id = E.child_id and
-- It isn't the same row as we're considering.
IE.id <> E.id );
請參閱dbfiddle。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446845.html
標籤:tsql
上一篇:以非美國格式格式化電話號碼
