遇到产品时增加行号 - T SQL - 单一查询(不允许超过一个语句)

Increase Row Number when ever Product is encountered - T SQL - Single Query (More than one statement not allowed)

下面有一个table

ID  Vals
1   Product
2   Milk
3   Butter
4   Cheese
5   Yogurt
6   Product
7   Muesli
8   Porridge
9   Product
10  Banana

需要如下输出

RWNUM   ID  Vals
1       1   Product
1       2   Milk
1       3   Butter
1       4   Cheese
1       5   Yogurt
2       6   Product
2       7   Muesli
2       8   Porridge
3       9   Product
3       10  Banana

每遇到一次Product,RWNUM列的值就会加1。

这需要在单个 TSQL 查询中实现。

欢迎任何想法。

您似乎想要“product”的累计总和:

select t.*,
       sum(case when val = 'Product' then 1 else 0 end) over (order by id) as rwnum
from t;