亲爱的技术我需要你的帮助
我有一张表有2列,一列是Series,第二列是item 像
Series item
1 apple
2 apple
3 apple
5 apple
6 apple
7 apple
8 apple
我希望以
结果from to item total
1 3 apple 3
5 8 apple 4
答案 0 :(得分:2)
大多数数据库都有一个名为row_number()
的函数,可生成序列号。它可以在这种情况下使用。此函数与Series
之间的差异对于组来说是不变的:
select min(Series) as "from", max(Series) as "to", item, count(*) as num
from (select t.*, row_number() over (partition by item order by Series) as seqnum
from table t
) t
group by (Series - seqnum), item;