【问题标题】:Column populated with table string occurrences joined to original SQL table用连接到原始 SQL 表的表字符串事件填充的列
【发布时间】:2021-08-02 05:35:38
【问题描述】:

我想查询一个显示我的原始数据的表以及一个根据条件('Type' NOT LIKE '%jack%')计算值('ID')出现次数的列

ID big med Type
1001 x 1 lumber_jack
1002 y 2 jack_knife
1001 z 3 peter_pan
1005 a 4 rock_star
1005 b 5 paper_hands
1007 c 6 to_the_moon

示例:ID = 1001 出现 2 次,但在“类型”列中只有一个不包含“%jack%”,因此计数 = 1

我想要的输出是:

ID big med Type count
1001 x 1 lumber_jack 1
1002 y 2 jack_knife 0
1001 z 3 peter_pan 1
1005 a 4 rock_star 2
1005 b 5 paper_hands 2
1007 c 6 to_the_moon 1

【问题讨论】:

    标签: sql join presto trino


    【解决方案1】:

    你可以使用窗口函数来做到这一点:

    select * , sum(case when Type NOT LIKE '%jack%' then 1 end) over (partition by ID)  as count 
    from table 
    

    【讨论】:

    • 是的,效果非常好,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2019-09-10
    • 2016-08-11
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多