如果列中的所有内容相等,则Postgres查询获取值

问题描述 投票:0回答:5

我有一个包含元数据的表,如果它们全部相等,我需要获取值,或者如果它们不相等则返回'mixed',或者如果all都为null则返回null。

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;

预期结果将是以下结果之一 -

'mixed','small','mixed',null

sql postgresql
5个回答
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
投票

一个巧妙的技巧是选择列的maxmin并进行比较:

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
投票

尝试..

    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;

此解决方案适用于您的测试数据。检查一下。 (我不想使用“table”作为表名,所以我使用“mytable”)


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);
© www.soinside.com 2019 - 2024. All rights reserved.