简体   繁体   English

如何计算同一列和同一组中的不同值

[英]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 -如果一个人同时具有YN状态,则显示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

Checking your query 检查您的查询

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! 看起来您做对了!

Creating a correct condition 创建正确的条件

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 如果您有更多变量,那么我建议您调查一下卡诺地图

Checking the 'MIN' answer 检查“ MIN”答案

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.

相关问题 如何计算同一列上具有不同where子句的值? - How to count values with different where clauses on the same column? 在不同的MySQL列中计数相同的值 - Count same values in different column mysql 如何在不增加GROUP BY的情况下计算不同的值 - How to COUNT different values without adding to GROUP BY 如何在MS Access中编写SQL查询以通过外键对同一字段和组中的不同值进行计数 - How to write a SQL Query in MS Access to Count different values in same field and group by a Foreign Key 查询以将同一列中两行的值分组并显示计数 - Query to group values from two rows in the same column and display count 如何计算同一列中的两个不同值并将这些计数输出到两个不同的列 - How to count two different values in the same column and output those counts to two different columns 在 SQL 中,如何在列 SQL 中执行计数相同的值,另外 5 列不在 GROUP BY 子句中? - In SQL how to do a Count Same values in a column SQL with 5 more columns not in GROUP BY clause? 转换行中的列并获取 postresql 中同一列的不同值的计数 - convert column in rows and get count of different values of same column in postresql 如何在 postgresql 中创建列以计算 varchar 格式的列的相同单元格中的不同值? - How do I create a column in postgresql to count different values in the same cells of a column that are in varchar format? 如何按SQL中不同级别的同一列分组? - How to group by same column on different levels in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM