2016-11-02 5 views
2

これは何らかの方法で回答されていれば非常に残念です。私はすべてをチェックして、それを理解することはできません。2組のデータの比較

週ごとのデータを比較するにはpostgresqlの方法を見つける必要があります。すべてのデータは同じテーブルに存在し、週番号の列があります。データが完全に重複するとは限りませんが、グループ内のデータを比較する必要があります。

これらはデータ・セットであると言う。

Week 2 
+--------+--------+------+---------+-------+ 
| group | num | color| ID  | week #| 
+--------+--------+------+---------+-------+ 
| a | 1 | red | a1red | 2 | 
| a | 2 | blue | a2blue | 2 | 
| b | 3 | blue | b3blue | 2 | 
| c | 7 | black| c7black | 2 | 
| d | 8 | black| d8black | 2 | 
| d | 9 | red | d9red | 2 | 
| d | 10 | gray | d10gray | 2 | 
+--------+--------+------+---------+-------+ 

Week 3 
+--------+--------+------+---------+-------+ 
| group | num | color| ID  | week #| 
+--------+--------+------+---------+-------+ 
| a | 1 | red | a1red | 3 | 
| a | 2 | green| a2green | 3 | 
| b | 3 | blue | b3blue | 3 | 
| b | 5 | green| b5green | 3 | 
| c | 7 | black| c7black | 3 | 
| e | 11 | blue | d11blue | 3 | 
| e | 12 | other| d12other| 3 | 
| e | 14 | brown| d14brown| 3 | 
+--------+--------+------+---------+-------+ 

各行グループから作らID、番号、及びカラー値を有しています。

私はその後、週2内に存在する3週のいずれかのグループのために、週3からすべてのグループをつかむために、クエリが必要になります。

  1. フラグのIDのグループA.
  2. のように、変更されているグループ内で持っていいだろう任意のID年代がグループに追加または削除された場合
  3. フラグ、グループのようにB.

一つの機能が、必須ではないが、3週は週1と比較させることです2週目に存在しないグループ。

私は2週間を分割し、結果を得るためにインターセプト/例外を使用しようと考えましたが、これを正しく動作させる方法を頭に入れてはいけません。どんなヒントも高く評価されます。あなたはこのような何かを行うことができますちょうど2つの(既知の)週間

+1

:あなたはテキスト表現と一緒に暮らすことができれば、あなたは違いを表示するためにhstore拡張子を使用することができますか? –

+0

ちょうど別の列です。これを明確にするためにテーブルを更新しました。 – Jakewb89

答えて

0

:変更された属性を取得

select coalesce(w1.group_nr, w2.group_nr) as group_nr, 
     coalesce(w1.num, w2.num) as num, 
     case 
     when w1.group_nr is null then 'missing in first week' 
     when w2.group_nr is null then 'missing in second week' 
     when (w1.color, w1.id) is distinct from (w2.color, w2.id) then 'data has changed' 
     else 'no change' 
     end as status, 
     case 
      when 
       w1.group_nr is not null 
      and w2.group_nr is not null 
      and w1.color is distinct from w2.color then 'color is different' 
     end as color_change, 
     case 
      when 
       w1.group_nr is not null 
      and w2.group_nr is not null 
      and w1.id is distinct from w2.id then 'id is different' 
     end as id_change 
from (
    select group_nr, num, color, id, hstore 
    from data 
    where week = 2 
) as w1 
    full outer join (
    select group_nr, num, color, id 
    from data 
    where week = 3 
) w2 on (w1.group_nr, w1.num) = (w2.group_nr, w2.num) 

は少し不器用です。週数、そのテーブルの中に隠されている

select coalesce(w1.group_nr, w2.group_nr) as group_nr, 
     coalesce(w1.num, w2.num) as num, 
     case 
     when w1.group_nr is null then 'missing in first week' 
     when w2.group_nr is null then 'missing in second week' 
     when (w1.color, w1.id) is distinct from (w2.color, w2.id) then 'data has changed' 
     else 'no change' 
     end as status, 
     w2.attributes - w1.attributes as changed_attributes 
from (
    select group_nr, num, color, id, hstore(data) - 'week'::text as attributes 
    from data 
    where week = 2 
) as w1 
    full outer join (
    select group_nr, num, color, id, hstore(data) - 'week'::text as attributes 
    from data 
    where week = 3 
) w2 on (w1.group_nr, w1.num) = (w2.group_nr, w2.num); 
+0

ああ!どうもありがとうございました。あなたが基本的な思考プロセスを展開した後、すべてがクリックされ、今はうまく動作しています。 – Jakewb89