根据父子关系

时间:2017-11-09 23:22:56

标签: sql sql-server tsql

我有以下表格样本:

+----+-------+-------------+--------------+-----------+-----------+
| ID | value | CommonField | Parentfield  |  child1   |  child2   |
+----+-------+-------------+--------------+-----------+-----------+
|  1 | abc   | 123         |              | 123child1 | 123child2 |
|  2 | abc   | 123child1   |          123 |           |           |
|  3 | abc   | 123child2   |          123 |           |           |
|  4 | def   | 456         |              | 456child1 | 456child2 |
|  5 | xyz   | 456child1   |          456 |           |           |
|  6 | def   | 456child2   |          456 |           |           |
+----+-------+-------------+--------------+-----------+-----------+

现在我的问题是我必须根据表中的Parent子关系比较'value'字段。我需要找到child1和child2的'value'字段相等,并根据该条件过滤记录。我的表以类似的方式有1000行。 我确实试着自己加入表并创建了一个标志字段,它告诉两个子行中'value'何时相等。

这是我的查询:

查询:

    Select p.id,
    p.value,
    p.CommonField,
    p.Parent field,
    p.child1,
    p.child2,
    CASE 
      When p.child1 IS NULL
        THEN p.parentfield
        ELSE c1.parentfield
    END AS Parent1
    CASE 
      When p.child1 IS NULL
        THEN p1.child1
        ELSE p.child1
    END AS child1p
    CASE 
      When p.child2 IS NULL
        THEN p1.child2
        ELSE p.child2
    END AS child2p
 CASE 
      When c1.value = c2.value 
        THEN 1
        ELSE 0
    END AS IS_Childequal
from Table as a
Inner Join Table p ON a.id = p.id
Left Join Table p1 ON p1.commonfield = p.parentfield
Left Join Table c1 ON c1.commonfield = p.child1
Left Join Table c2 ON c2.commonfield = p.child2 

结果:

+----+-------+-------------+--------------+-----------+-----------+---------+-----------+-----------+---------------+
| ID | value | CommonField | Parent field |  child1   |  child2   | parent1 |  child1p  |  child2p  | IS_childequal |
+----+-------+-------------+--------------+-----------+-----------+---------+-----------+-----------+---------------+
|  1 | abc   | 123         |              | 123child1 | 123child2 |     123 | 123child1 | 123child2 |             1 |
|  2 | abc   | 123child1   |          123 |           |           |     123 | 123child1 | 123child2 |             1 |
|  3 | abc   | 123child2   |          123 |           |           |     123 | 123child1 | 123child2 |             1 |
|  4 | def   | 456         |              | 456child1 | 456child2 |     456 | 456child1 | 456child2 |             1 |
|  5 | xyz   | 456child1   |          456 |           |           |     456 | 456child1 | 456child2 |             1 |
|  6 | def   | 456child2   |          456 |           |           |     456 | 456child1 | 456child2 |             1 |
+----+-------+-------------+--------------+-----------+-----------+---------+-----------+-----------+---------------+

即使'value'字段不相等,我的新Flag字段也会返回1。 我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您的问题必须在其他地方,因为您提供的示例数据可以正常运行:

SQL fiddle with your data and query

您在SQL小提示中的查询结果(只有第一行按预期方式IS_Childequal=1):

| id | value | CommonField | Parentfield |    child1 |    child2 | Parent1 |   child1p |   child2p | IS_Childequal |
|----|-------|-------------|-------------|-----------|-----------|---------|-----------|-----------|---------------|
|  1 |   abc |         123 |      (null) | 123child1 | 123child2 |     123 | 123child1 | 123child2 |             1 |
|  2 |   abc |   123child1 |         123 |    (null) |    (null) |     123 | 123child1 | 123child2 |             0 |
|  3 |   abc |   123child2 |         123 |    (null) |    (null) |     123 | 123child1 | 123child2 |             0 |
|  4 |   def |         456 |      (null) | 456child1 | 456child2 |     456 | 456child1 | 456child2 |             0 |
|  5 |   xyz |   456child1 |         456 |    (null) |    (null) |     456 | 456child1 | 456child2 |             0 |
|  6 |   def |   456child2 |         456 |    (null) |    (null) |     456 | 456child1 | 456child2 |             0 |


修改:由于您对子行的评论需要IS_childequal=1,因此最好使用子查询获取该值,然后使用ID将其与原始表连接对于父母和ParentField为孩子:

select ID, value, CommonField, ParentField, Child1, Child2,
  IS_Childequal, Child1Value, Child2Value
from Table1 as p
left join (select p.ID AS ParentID, p.CommonField as Parent,
    c1.value as Child1Value, c2.value as Child2Value,
    case when c1.value = c2.value then 1 else 0 end as IS_Childequal
  from Table1 as p
  left join Table1 c1 ON c1.CommonField = p.child1
  left join Table1 c2 ON c2.CommonField = p.child2
  where p.ParentField is null)
as parents on p.ID=ParentID or ParentField=Parent

结果(SQL fiddle):

| ID | value | CommonField | ParentField |    Child1 |    Child2 | IS_Childequal | Child1Value | Child2Value |
|----|-------|-------------|-------------|-----------|-----------|---------------|-------------|-------------|
|  1 |   abc |         123 |      (null) | 123child1 | 123child2 |             1 |         abc |         abc |
|  2 |   abc |   123child1 |         123 |    (null) |    (null) |             1 |         abc |         abc |
|  3 |   abc |   123child2 |         123 |    (null) |    (null) |             1 |         abc |         abc |
|  4 |   def |         456 |      (null) | 456child1 | 456child2 |             0 |         xyz |         def |
|  5 |   xyz |   456child1 |         456 |    (null) |    (null) |             0 |         xyz |         def |
|  6 |   def |   456child2 |         456 |    (null) |    (null) |             0 |         xyz |         def |