2016-11-02 7 views
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를 가지고

이러한 데이터 세트이다 말한다.

나는 3 주에서 모든 그룹을 잡기 위해 쿼리를 필요로, 다음 주 2에 존재하는 3 주에있는 어떤 그룹 :

  1. 플래그 ID의의를 그룹 A.
  2. 에서와 같이 변경 한 그룹 내에서 어떤 ID 년대 B. 그룹처럼 그룹에 추가 또는 제거 된 경우
  3. 플래그는
  4. 가지고 좋은, 꼭 필요한 것은 아닙니다

한 기능은, 주 3가 1 주에 대해 비교해야하는 것 2 주에 존재하지 않는 그룹.

2 주를 나누고 결과를 얻기 위해 가로 채기/예외를 사용하는 방법에 대해 생각해 보았지만 정상적으로 작동하도록하는 방법을 고민하지는 못했습니다. 모든 팁을 많이 주시면 감사하겠습니다. 당신이 이런 식으로 뭔가를 할 수 두 (알려진) 주 동안

+1

해당 테이블에서 주 번호는 숨겨져 있습니까? –

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

조금 서투른입니다. 텍스트 표현으로 살 수있는 경우 hstore 확장자를 사용하여 차이를 표시 할 수 있습니다.

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

관련 문제