2016-09-06 2 views
0

다른 행의 값을 기반으로 특정 열이 비어있는 행을 업데이트하는 방법에 대해 궁금합니다.postgres - 다른 행을 기반으로 행 업데이트

enter image description here

는 I는 다음 두 행의 값이 첫 번째 행에 대한 열 (user_name에, 데이터베이스 _, connection_from)를 업데이트 할. session_id는 동일합니다.

감사합니다.

+0

이의 중요한 부분은 데이터를 이해하는 당신이 수집하려는하지 : 내가 당신이 그 세 개의 열을 위해 작동 할 session_id에 따라 데이터와 비 NULL 값을보고 있다고 가정하자 업데이트 자체. 필요한 데이터를 선택하는 방법을 알면 업데이트가 간단합니다. 테이블 정의 (특히 기본 키)의 세부 사항을 모른 채 더 구체적 일 수는 없습니다. BTW, 연결 정보를 connection_ip inet 및 connection_port int로 저장하는 것이 좋습니다. Inet 유형은 강력한 연산자를 제공합니다. –

답변

0

업데이트 할 값을 선택하는 방법에 대한 질문이 다소 모호합니다. ,

update t 
    set user_name = coalesce(t.user_name, tt.user_name), 
     database_name = coalesce(t.database_name, tt.database_name), 
     connection_from = coalesce(t.connection_from, tt.connection_from) 
    from (select session_id, max(user_name) as user_name, 
       max(database_name) as database_name, 
       max(connection_from) as connection_from 
      from t 
      group by session_id 
     ) tt 
    where t.session_id = tt.session_id and 
      (t.user_name is null or t.database_name is null or t.connection_from is null); 
+0

대단히 감사합니다. 거의 일했다. connection_from 열이 업데이트되지 않았습니다. 작은 잡기. connection_from 열은 null처럼 보입니다. select *를 실행하면 connection_from이 null이고 where 행은 반환되지 않습니다. 나를 안내 해줄 수 있니? 다시 한번 감사드립니다. – vignesh28

+0

@ vignesh28. . . 'coalesce()'를'case. t.connection_from> '','t.connection_from else '대신'tt.connection_from end'와 같이 대체하십시오. –

관련 문제