用连接到原始 SQL 表的表字符串事件填充的列
Posted
技术标签:
【中文标题】用连接到原始 SQL 表的表字符串事件填充的列【英文标题】:Column populated with table string occurrences joined to original SQL table 【发布时间】: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 |
【问题讨论】:
【参考方案1】:你可以使用窗口函数来做到这一点:
select * , sum(case when Type NOT LIKE '%jack%' then 1 end) over (partition by ID) as count
from table
【讨论】:
是的,效果非常好,非常感谢!以上是关于用连接到原始 SQL 表的表字符串事件填充的列的主要内容,如果未能解决你的问题,请参考以下文章