我有一个包含元数据的表,我需要获取值,如果它们全部相等,或者如果它们不相等则返回'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
答案 0 :(得分: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 ;
答案 1 :(得分:0)
一个巧妙的技巧是选择列的max
和min
并进行比较:
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
答案 2 :(得分: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);
答案 3 :(得分: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)
答案 4 :(得分:0)
我会尝试使用CTE的
$('#mon_from').clockpicker({
autoclose: true
});
此解决方案适用于您的测试数据。检查一下。 (我不想使用“table”作为表名,所以我使用“mytable”)