[英]How to count different values in a column and same group
I have to distinguish different values in a group, how do I perform that? 我必须在一个组中区分不同的值,我该如何执行? Example of my table:
我的桌子的例子:
Person Status
1 Y
2 N
3 Y
3 N
-If a person only has Y
status then display Yes
-如果一个人只有
Y
状态,则显示Yes
-If a person only has N
status then display No
-如果一个人只有
N
状态,则显示No
-If a person has both Y
and N
status then display No
-如果一个人同时具有
Y
和N
状态,则显示No
Result: 结果:
Person Status
1 Yes
2 No
3 No
How do I perform last logic when a person has both status? 当一个人同时具有两种身份时,我该如何执行最后的逻辑? Here is the statement I tried which worked but I am not 100% sure if it is accurate:
这是我尝试过的有效语句,但我不确定是否准确:
CASE WHEN SUM(CASE WHEN Status = 'Y' THEN 1 ELSE 0 END)>=1 AND SUM(CASE WHEN Status = 'N' THEN 1 ELSE 0 END)>=1 THEN 'No'
WHEN SUM(CASE WHEN Status = 'Y' THEN 1 ELSE 0 END)>=1 THEN 'Yes'
ELSE 'No'END
You can use MIN: 您可以使用MIN:
select CASE WHEN MIN(status) = 'Y' THEN 'Yes' ELSE 'No' END
, Person
from table
group by Person
For simplicity let's say that: 为了简单起见,我们说:
(Y) -> SUM(CASE WHEN Status = 'Y' THEN 1 ELSE 0 END)
(N) -> SUM(CASE WHEN Status = 'N' THEN 1 ELSE 0 END)
So your query can be written as pseudocode: 因此,您的查询可以写为伪代码:
case when (Y)>=1 and (N)>=1 then 'No'
when (Y)>=1 then 'Yes'
else 'No'
end
To check if your query is correct You need to build a truth table from your query for all possible input: 要检查查询是否正确,您需要针对所有可能的输入从查询中构建一个真值表:
(Y) | (N) (Y)>=1 | (N)>=1 (Y)>=1 and (N)>=1 Result
----------- ----------------- ------------------- --------
0 | 0 false | false false 'No'
0 | >=1 => false | true => false => 'No'
>=1 | 0 true | false false 'Yes'
>=1 | >=1 true | true true 'No'
Look's like You got it right! 看起来您做对了!
To be sure if You are making your condition correctly You need to build a truth table: 要确定您是否正确设置了条件,您需要构建一个真值表:
(1)
Yes | No | Result
-------------------------------
found | found | No
not found | found | No
found | not found | Yes
not found | not found | Null
or 要么
(2)
\ No
Yes \ Found | Not Found |
-------------------------------
Found | No | Yes |
-------------------------------
Not Found | No | Null |
------------------------------
The first thing to notice is that if status==No is found the result is always No. 首先要注意的是,如果找到status == No,则结果始终为No。
Only when a No is not found and a Yes is found then we return Yes. 仅当找不到“否”且找到“是”时,我们才返回“是”。
So Your condition can be written with a nested case. 因此,您的条件可以用嵌套的情况写。
select columns, ....
(
case when (SUM(CASE WHEN Status = 'N' THEN 1 ELSE 0 END)>=1) then 'No'
else case when (SUM(CASE WHEN Status = 'Y' THEN 1 ELSE 0 END)>=1) then 'Yes'
else NULL
end
end
) as result
It's easy with 2 variables in a condition. 一个条件中有2个变量很容易。
If You have more variables then I suggest You look into a Karnaugh map 如果您有更多变量,那么我建议您调查一下卡诺地图
Another answer here gave an original solution: 这里的另一个答案给出了一个原始的解决方案:
select CASE WHEN MIN(status) = 'Y' THEN 'Yes' ELSE 'No' END
, Person
from table
group by Person
Is it correct? 这是正确的吗?
Let's check first all the possible input. 让我们首先检查所有可能的输入。
Input:
{Y} - contains only Y in the column
{N} - contains only N in the column
{Y,N} - contains Y and N in the column
{} - Y and N do not appear in the column
Let's calculate all the possible results: 让我们计算所有可能的结果:
Input MIN(status) Result
------- ------------- --------
{Y} Y Yes
{N} => N => No
{Y,N} N No
{} NULL No
It's a correct answer. 这是正确的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.