问题描述
当5或9不存在时,我需要一个空值(或另一个标志)
when 5 or 9 does not exist, i need a null value (or another flag)
我有3栏.部分,状态和名称.在SECTION中,最多可包含10行(状态1至10).我必须在SECTION中比较STATUS 5和9的NAME值.然后指出每个部分的两个名称(用于状态5和状态9)是否相同.
I have 3 columns. SECTION, STATUS and NAME. Within a SECTION there are a maximum of 10 rows (STATUS 1 to 10). I have to compare the value of NAME for STATUS 5 and 9 within a SECTION. AND then indicate if those 2 NAMES (for STATUS 5 and 9) are the same for each SECTION.
部分状态名称1 51 61 9 b2 4 c2 5天2 9天2 10天3 5 e3 10 e
section status name1 5 a1 6 a1 9 b2 4 c2 5 d2 9 d2 10 d3 5 e3 10 e
所需的输出
Section equalnames
1 no
2 yes
3 null/flag
推荐答案
我将使用布尔值而不是字符串,而仅使用带过滤的聚合:
I would use a boolean instead of strings and just use aggregation with filtering:
select group, min(name) = max(name) as names_equal_flag
from t
where status in (5, 9)
group by group;
请注意, group
对于列来说确实是个坏名字,因为它是一个SQL关键字.我假设您在实际数据中使用一个更合适的名称.
Note that group
is a really bad name for a column because it is a SQL keyword. I assume you have a more appropriate name in your actual data.
如果需要字符串,可以使用 case
:
If you want a string, you can use case
:
select group,
(case when min(name) = max(name) then 'yes' else 'no' end) as names_equal_flag
from t
where status in (5, 9)
group by group;
这篇关于比较组中具有特定值的行PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!