2017-12-17 5 views
0

메타 데이터가있는 테이블이 있는데 모두 같거나 값이 같지 않으면 '혼합'을 반환하거나 모두가 null 인 경우 값을 가져와야합니다. null를 돌려줍니다.Postgres 쿼리가 모두 열에있는 경우 값을 얻으려면

id | color | size | shape | area 
1 | blue | small | square | 
2 |   | small | circle | 
3 | blue | small | oval | 
4 | blue | small | oval | 

select distinct color, size, shape, area 
from table 
where id = 1 or id = 2 or id = 3; 

예상되는 결과는 하나 개의 결과 일 것이다 다음과 같은 -

'혼합', '작은',

+0

결과로 네 개의 열이있는 단일 행을 원하십니까? –

답변

0

깔끔한 트릭은 maxmin의를 선택하는 것입니다 널 (null) '혼합' 열하고 비교 :

SELECT CASE WHEN MAX(color) IS NOT DISTINCT FROM MIN(color) 
      THEN MAX(color) ELSE 'mixed' END, 
     CASE WHEN MAX(size) IS NOT DISTINCT FROM MIN(size) 
      THEN MAX(size) ELSE 'mixed' END, 
     CASE WHEN MAX(shape) IS NOT DISTINCT FROM MIN(shape) 
      THEN MAX(shape) ELSE 'mixed' END, 
     CASE WHEN MAX(area) IS NOT DISTINCT FROM MIN(area) 
      THEN MAX(area) ELSE 'mixed' END 
WHERE id IN (1, 2, 3) 
FROM my_table 
+0

감사합니다. 하지만 이것은 null 값을 고려하지 않는 것 같습니다. 그래서 나는 '혼합 된'색 대신에 푸른 색으로 되돌아 간다. – user2352131

0
select id, 
     (case when min(color) is not distinct from max(color) and 
        min(size) is not distinct from max(size) and 
        min(shape) is not distinct from max(shape) and 
        min(area) is not distinct from max(area) 
      then 'same' 
      else 'mixed' 
     end) 
from table 
where id in (1, 2, 3) 
group by id; 

또한 당이 작업을 수행 할 수 있습니다 열뿐만 아니라.

편집 :

내가 먼저 대답부터 문제가 조금 변경되었습니다 (또는 적어도 내가 오해) 생각 :

select (case when min(color) = max(color) and count(*) = count(color) or 
        min(color) is null 
      then min(color) else 'mixed' 
     end) as color, 
     (case when min(size) = max(size) and count(*) = count(size) or 
        min(size) is null 
      then min(size) else 'mixed' 
     end) as size, 
     (case when min(color) = max(shape) and count(*) = count(shape) or 
        min(shape) is null 
      then min(shape) else 'mixed' 
     end) as shape, 
     (case when min(area) = max(area) and count(*) = count(area) or 
        min(area) is null 
      then min(area) else 'mixed' 
     end) as area 
from table 
where id in (1, 2, 3); 
+0

나는 이것을 칼럼마다 분리했으나 null을 고려하지 않았다. 그래서 나는 '혼합 된'색 대신에 파란색으로 되돌아 간다. – user2352131

+0

@ user2352131. . . 원래의 대답은 질문을 해결하지 못했습니다. 열 단위가 아닌 행 단위로 나타납니다. –

1

시도 :

SELECT 
    CASE count(distinct color) WHEN 0 THEN NULL WHEN 1 THEN 
    CASE count(color)=count(*) WHEN true THEN min(color) ELSE 'mixed' END ELSE 'mixed' END color, 
    CASE count(distinct size) WHEN 0 THEN NULL WHEN 1 THEN 
    CASE count(size)=count(*) WHEN true THEN min(size) ELSE 'mixed' END ELSE 'mixed' END size, 
    CASE count(distinct shape) WHEN 0 THEN NULL WHEN 1 THEN 
    CASE count(shape)=count(*) WHEN true THEN min(shape) ELSE 'mixed' END ELSE 'mixed' END shape, 
    CASE count(distinct area) WHEN 0 THEN NULL WHEN 1 THEN 
    CASE count(area)=count(*) WHEN true THEN min(area) ELSE 'mixed' END ELSE 'mixed' END area 
FROM table ; 
0

봅니다 ..

SELECT 
CASE WHEN count(DISTINCT color)=0 THEN 'Null' WHEN count(DISTINCT color)+sum(case when color is null then 1 else 0 end) =1 THEN MAX(color) ELSE 'Mixed' END AS color, 

CASE WHEN count(DISTINCT size)=0 THEN 'Null' WHEN count(DISTINCT size)+sum(case when size is null then 1 else 0 end) =1 THEN MAX(size) ELSE 'Mixed' END AS size, 

CASE WHEN count(DISTINCT shape)=0 THEN 'Null' WHEN count(DISTINCT shape)+sum(case when shape is null then 1 else 0 end) =1 THEN MAX(shape) ELSE 'Mixed' END AS shape, 

CASE WHEN count(DISTINCT area)=0 THEN 'Null' WHEN count(DISTINCT area)+sum(case when area is null then 1 else 0 end) =1 THEN MAX(area) ELSE 'Mixed' END AS area 

      FROM my_table 
WHERE id IN (1, 2, 3) 
0

CTE를 사용하려고합니다.

with 
    -- distinct values 
    scolor as (select distinct color from mytable), 
    ssize as (select distinct size from mytable), 
    sshape as (select distinct shape from mytable), 
    sarea as (select distinct area from mytable) 
select 
    case 
     when (select count(*) from scolor) > 1 
     then 'mixed' 
     when (select count(*) from scolor) = 1 and (select * from scolor) is null 
     then null 
     else 
     (select * from scolor) 
    end, 
    case 
     when (select count(*) from ssize) > 1 
     then 'mixed' 
     when (select count(*) from ssize) = 1 and (select * from ssize) is null 
     then null 
     else 
     (select * from ssize) 
    end, 
    case 
     when (select count(*) from sshape) > 1 
     then 'mixed' 
     when (select count(*) from sshape) = 1 and (select * from sshape) is null 
     then null 
     else 
     (select * from sshape) 
    end, 
    case 
     when (select count(*) from sarea) > 1 
     then 'mixed' 
     when (select count(*) from sarea) = 1 and (select * from sarea) is null 
     then null 
     else 
     (select * from sarea) 
    end 
from mytable 
limit 1; 

이 솔루션은 테스트 데이터와 잘 작동합니다. 그냥 확인해. ("테이블"을 테이블 이름으로 사용하고 싶지 않으므로 "mytable"을 사용합니다.)