我有一个类似的表
col1 | col2 |
---|---|
第一行 | |
第二个 | a |
第一个 | b |
第二行 | |
第一个 | c |
第二行 |
您需要一个指定排序的列,因为SQL表表示无序集。
对于这样的列,只需使用条件聚合:
select t.*,
sum(case when col2 = 'myval' then 1 else 0 end) over (order by <ordering col>) as col3
from t;