列填充了 table 个字符串并连接到原始 SQL table

Column populated with table string occurrences joined to original SQL table

我想查询一个 table 来显示我的原始数据,以及一个根据条件 ([=26) 计算值 ('ID') 出现次数的列=] 不像 '%jack%')

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 次,但只有一次不包含 'Type' 列中的“%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

您可以使用 window 函数来完成:

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