简体   繁体   English

根据SQL中的序列对值进行分组

[英]Grouping values based on sequence in SQL

Is there a way to create just using select statements a table that contains in a column the range of the repeating values like in this example? 有没有一种方法可以仅使用select语句创建一个表,该表在一个列中包含重复值的范围(如本示例中所示)?

Example: 例:

from the input table: 从输入表中:

id value:
1 25
2 25
3 24
4 25
5 25
6 25
7 28
8 28
9 11

should result: 应导致:

range value
1-2 25
3-3 24
4-6 25
7-8 28
9-9 11

Note: The id values from the input table are always in order and the difference between 2 values ordered by id is always equal to 1 注意:输入表中的id值始终是有序的,并且按id排序的2个值之间的差始终等于1

You want to find sequences of consecutive values. 您要查找连续值的序列。 Here is one approach, using window functions: 这是使用窗口函数的一种方法:

select min(id), max(id), value
from (select id, value,
             row_number() over (order by id) as rownum,
             row_number() over (partition by value order by id) as seqnum
      from t
     ) t
group by (rownum - seqnum), value;

This takes into account that a value might appear in different places among the rows. 这考虑到值可能出现在行之间的不同位置。 The idea is simple. 这个想法很简单。 rownum is a sequential number. rownum是一个序号。 seqnum is a sequential number that increments for a given value. seqnum是对于给定值递增的序列号。 The difference between these is constant for values that are in a row. 对于连续的值,它们之间的差异是恒定的。

Let me add, if you actually want the expression as "1-2", we need more information. 让我补充一下,如果您实际上希望表达式为“ 1-2”,我们需要更多信息。 Assuming the id is a character string, one of the following would work, depending on the database: 假设id是一个字符串,则根据数据库的不同,可以使用以下方法之一:

select min(id)+'-'+max(id), . . .
select concat(min(id), '-', max(id)), . . .
select min(id)||'-'||max(id), . . .

If the id is an integer (as I suspect), then you need to replace the id s in the above expressions with cast(id as varchar(32)) , except in Oracle, where you would use cast(id as varchar2(32)) . 如果id是一个整数(我怀疑),那么您需要使用cast(id as varchar(32))替换上述表达式中的id s,但在Oracle中除外,在Oracle中您可以使用cast(id as varchar2(32))

SELECT CONCAT(MIN(id), '-', MAX(id)) AS id_range, value
  FROM input_table
 GROUP BY value

也许这样:

SELECT MIN(ID), MAX(ID), VALUE FROM TABLE GROUP BY VALUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM