【问题标题】:How to flag if something doesn't appear in a list如果某些内容未出现在列表中,如何进行标记
【发布时间】:2020-03-19 09:08:41
【问题描述】:

我不知道如何准确地在标题中表达这一点,但我有一个拥有多条记录的人的列表,每一行都是一条记录,一个字段列出了它是哪种类型的记录。我需要做的是能够在某人没有特定类型的记录时进行标记。

数据如下

person id       type
1                a
1                b
1                c
2                b
3                a
3                e
3                d
3                c
4                a
4                c
4                e

在这个列表中,id 3 和 4 的人没有类型 b,所以我想创建一个标志来突出显示它。在输出数据中,类型不是必需的,所以它只是 Person ID 和 flag。

任何帮助表示赞赏。

【问题讨论】:

    标签: sql sql-server ssms-2017


    【解决方案1】:

    使用窗口功能:

    select t.*, 
           coalesce(max(case when type = 'b' then 'yes' end) over (partition by personid), 'no') as b_flag
    from table t;
    

    你也可以做聚合:

    select personid, 
           coalesce(max(case when type = 'b' then 'yes' end), 'no') as b_flag
    from table t
    group by personid;
    

    【讨论】:

      猜你喜欢
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多