[英]CASE on two rows with identical IDs but different values
I have the following table.我有下表。 What I need is an sql query that will analyze two/more rows with the same id and: (1) CASE 'Result' is the same for both rows, print 'Passed';
我需要的是一个 sql 查询,它将分析具有相同 id 的两个/多个行,并且:(1)CASE 'Result' 对两行都相同,打印 'Passed'; (2) CASE 'Result' is not the same for both rows, print 'Failed'.
(2) CASE 'Result' 两行不一样,打印 'Failed'。
ID Result
73 2000 Passed
73 2000
43 2000 Failed
43 1003
Is it all possible??一切皆有可能吗??
If you want one row per id, then use group by
:如果您想要每个 id 一行,请使用
group by
:
select id,
(case when min(result) = max(result) then 'Passed'
else 'Failed'
end) as flag
from t
group by id;
If you want the value on all rows of your original table, then it is simple:如果您想要原始表的所有行上的值,那么很简单:
select id,
(case when not exists (select 1
from t t2
where t2.result) = t.result
)
then 'Passed' else 'Failed'
end) as flag
from t
group by id;
However, putting the result on one row is problematic.然而,并将结果在一行上是有问题的。 There is no way to distinguish the rows, so it is more challenging (not impossible, but I suspect one of the above solves your real problem).
无法区分行,因此更具挑战性(并非不可能,但我怀疑上述方法之一可以解决您的真正问题)。
Try below:试试下面:
select id, case when count (distinct result)=1 then 'Passed' else 'Failed' end
group by id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.