简体   繁体   English

添加列的案例语句

[英]Case Statement to Add a Column

I have the following table:我有下表:

ID   Fruit
A    apple
A    banana
A    grapes
B    orange
B    apple
B    grapes
C    grapes
C    orange
C    banana

I would like to add a new column called Apple such to denote whether ID is associated with apple or not:我想添加一个名为 Apple 的新列来表示 ID 是否与苹果相关联:

ID   Fruit    Apple
A    apple    yes
A    banana   yes
A    grapes   yes
B    orange   yes
B    apple    yes
B    grapes   yes
C    grapes   no
C    orange   no
C    banana   no

Since this seems like a contrived example, I'll post several options.由于这似乎是一个人为的例子,我将发布几个选项。 The best one will depend on what you're really doing.最好的取决于你真正在做什么。

First up, this is likely to perform best, but it risks duplicating rows if you could have multiple matches for the JOINed table.首先,这可能表现最好,但如果您可以为 JOINed 表提供多个匹配项,则存在重复行的风险。 It's also the only solution I'm presenting to actually use a CASE expression as requested.这也是我提出的根据要求实际使用CASE表达式的唯一解决方案。

SELECT a.*, case when b.ID IS NOT NULL THEN 'Yes' ELSE 'No' END AS Apple
FROM MyTable a
LEFT JOIN MyTable b on b.ID = a.ID AND b.Fruit = 'Apple'

Alternatively, this will never duplicate rows, but has to re-run the nested query for each result row.或者,这将永远不会重复行,但必须为每个结果行重新运行嵌套查询。 If this is not a contrived example, but something more like homework, this is probably the expected result.如果这不是一个人为的例子,而更像是家庭作业,这可能是预期的结果。

SELECT *, coalesce(
    (
       SELECT TOP 1 'Yes' 
       FROM MyTable b 
       WHERE b.ID = a.ID AND b.Fruit = 'Apple'
    ), 'No') As Apple
FROM MyTable a

Finally, this also re-runs the nested query for each result row, but there is the potential a future enhancement will improve on that and it makes it possible to provide values for multiple columns from the same nested subquery.最后,这还会为每个结果行重新运行嵌套查询,但未来的增强功能可能会对此进行改进,并且可以为来自同一个嵌套子查询的多个列提供值。

SELECT a.*, COALESCE(c.Apple, 'No') Apple
FROM MyTable a
OUTER APPLY (
    SELECT TOP 1 'Yes' As Apple
    FROM MyTable b
    WHERE b.ID = a.ID AND b.Fruit = 'Apple'
) c

See them work here:看到他们在这里工作:

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=e1991e8541e7421b90f601c7e8c8906b https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=e1991e8541e7421b90f601c7e8c8906b

It could be achieved without self JOIN by using windowed COUNT_IF :它可以通过使用窗口化的COUNT_IF来实现,而无需 self JOIN :

SELECT *, COUNT_IF(Fruit = 'apple') OVER(PARTITION BY ID) > 0 AS Apple
FROM tab;

Output:输出:

在此处输入图像描述

For the following table,对于下表,

ID   Fruit
A    apple
A    banana
A    grapes
B    orange
B    apple
B    grapes
C    grapes
C    orange
C    banana

Adding a new column called Apple to denote whether ID is associated with apple or not, the resultset would be添加一个名为 Apple 的新列来表示 ID 是否与苹果相关联,结果集将是

ID   Fruit    Apple
A    apple    yes
A    banana   no
A    grapes   no
B    orange   no
B    apple    yes
B    grapes   no
C    grapes   no
C    orange   no
C    banana   no

If the expected resultset is as above, the below query will help to get the desired output.如果预期的结果集如上所示,则以下查询将有助于获得所需的输出。

select
    id,
    case
        when fruit='apple' then 'yes'
        when fruit!='apple' then 'no'
    end as Apple
from Fruits;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM