2017-11-09 2 views
0

I次の表のサンプルを持っている:は、親と子の関係に基づいて、テーブル内の1つの列の値を比較し

+----+-------+-------------+--------------+-----------+-----------+ 
| 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 |   |   | 
+----+-------+-------------+--------------+-----------+-----------+ 

今私の問題は、私は親子関係に基づいて、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 | 
+----+-------+-------------+--------------+-----------+-----------+---------+-----------+-----------+---------------+ 

私の新しいフラッグフィールド 'の値' フィールドがない場合であっても、常に1を返します。等しい。 私は何を間違えていますか?

+0

新しいフラグフィールドですか? – maSTAShuFu

+0

@maSTAShuFu:IS_childequalはFLAGフィールドで、0以外の場合は1を返します。スクロールの隠れたコサージュが必要です。 – AkhilT

+0

スクリプトを実行してc1.valueとc2.valueを表示すると、結果はどうなりますか? – maSTAShuFu

答えて

0

あなたの問題は、あなたのクエリが正常に動作しているサンプルデータを他の場所でなければなりません:SQLフィドルでクエリから

SQL fiddle with your data and query

結果(最初の行だけが期待通りに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 | 


編集:あなたは、子R用​​が必要であることをコメントしているのでOWSだけでなく、あなたがより良い、その値を取得するためにサブクエリを使用して、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 | 
+0

あなたのご意見ありがとうございますが、子供の行にもIS_childequal = 1が必要です。つまり、最初の3行です。 – AkhilT

+0

私はその結果を得るための質問で答えを更新しました。 –

+0

ありがとうございます。これは私が探していたものです。 – AkhilT

関連する問題