SQL查询-消除记录

时间:2018-12-13 07:48:09

标签: sql sql-server

数据以以下格式存储,父代码可以有零个或多个子代。显示父母和子女的状态。问题是,使用SQL查询,我们可以仅消除状态和子状态已关闭的父级。例如对于下面的示例,只需消除P1和p4。

ParentCode ChildCode ParentStatus ChildStatus
P1           0         Closed       Closed
P1           C1        Closed       Closed
P2           0         Closed       Closed
P2           C2        Closed       Open
P3           0         Open         Closed
P3           C3        Open         Open
P4           0         Closed       Closed
P5           0         Open         Closed

3 个答案:

答案 0 :(得分:1)

如果我理解正确,则可以尝试使用OUTPUT子查询来消除。

INSERT

sqlfiddle

编辑

您可以尝试将not existsSELECT * FROM T t1 where not exists ( select 1 from T tt where tt.ParentStatus = t1.ParentStatus and tt.ParentStatus = 'Closed' and tt.ChildStatus = t1.ChildStatus and tt.ChildStatus = 'Closed' ) 一起使用,以根据您的评论做出期望,

CTE

sqlfiddle

答案 1 :(得分:0)

使用以下查询,

select * from Table
where ParentStatus <> 'Closed' OR ChildStatus <> 'Closed'

答案 2 :(得分:0)

我认为简单聚合适用于此数据结构:

$Foldersize = "{0:N2} MB" -f ((Get-ChildItem -LiteralPath $Folder -Force -Recurse -EV +err -EA Continue | Measure-Object -Property Length -Sum).Sum / 1MB)