顺序SQL查询

时间:2017-01-23 16:00:29

标签: sql sql-server

SQL问题的新手,请原谅我

简单SQL查询的任何线索?我试图对一系列结果进行排序,其中有一系列数字中断。

例如R00000D至R99999D。

然而,一些数字已在无连续顺序示例R00000D至R12549D中分配,则下一个序列为R200001D至R50000D。

所以我想将这些分组

R00000D to R12549D
R20000D to R50000D
R69154D to R99999D

我能够做到的唯一方法是导出到excel和冗长的方式并计算列数。

我的开始查询很简单

Select *
FROM SystemNote
WHERE Note LIKE 'R%'

2 个答案:

答案 0 :(得分:1)

我会根据您的订购要求制作一张桌子并加入其中 - 如果您的要求发生变化,您可以修改该表格。在这里,我将其作为内联表,但您可以拥有一个实际的表或CTE。

此代码适用于大多数平台,只需稍加修改即可。我知道它适用于DB2,我相信Oracle。不确定values语句是否在SQL Server中有效。您可能需要更改为SELECT / UNION ALLs。

SELECT *
FROM SystemNote
LEFT JOIN (
  VALUES
    ('R00000D','R12549D',1),
    ('R20000D','R50000D',2),
    ('R69154D','R99999D',3)
) AS ORD(S,E,ORD) ON Note BETWEEN ORD.S AND ORD.E
WHERE Note LIKE 'R%'
ORDER BY COALESCE(ORD.ORD,4) ASC, Note
  

注意,这会使任何项目不在您定义的范围内。这可能(或可能不是)你想要的。你没有定义它。

答案 1 :(得分:1)

用于支持Windows功能的数据库。 可能需要对不同的提供商进行小的更改。

<强>演示

create table SystemNote (Note char(7));

insert into SystemNote values 
 ('R00000D'),('R00001D'),('R00002D'),('R00003D'),('R00004D')
,('R00012D'),('R00013D')
,('R00015D'),('R00016D'),('R00017D'),('R00018D')
,('R00021D')
,('R00025D'),('R00026D'),('R00027D')
select      min (Note)  as from_Note
           ,max (Note)  as to_Note
           ,count (*)   as Notes

from       (select      Note
                       ,row_number () over (order by Note)  as rn

            from        SystemNote

            where       Note like 'R%'
            ) sn

group by    cast (substr(Note,2,5) as int) - rn

order by    from_Note
+-----------+---------+-------+
| from_note | to_note | notes |
+-----------+---------+-------+
| R00000D   | R00004D | 5     |
+-----------+---------+-------+
| R00012D   | R00013D | 2     |
+-----------+---------+-------+
| R00015D   | R00018D | 4     |
+-----------+---------+-------+
| R00021D   | R00021D | 1     |
+-----------+---------+-------+
| R00025D   | R00027D | 3     |
+-----------+---------+-------+
select  dense_rank () over (order by cast (substr(Note,2,5) as int)-rn) as group_id
       ,row_number () over (partition by cast (substr(Note,2,5) as int)-rn order by note) as seq
       ,min (Note) over (partition by cast (substr(Note,2,5) as int)-rn) as from_Note
       ,max (Note) over (partition by cast (substr(Note,2,5) as int)-rn) as to_Note
           ,Note

from       (select      Note
                       ,row_number () over (order by Note)  as rn

            from        SystemNote

            where       Note like 'R%'
            ) sn

order by    Note
+----------+-----+-----------+---------+---------+
| group_id | seq | from_note | to_note | note    |
+----------+-----+-----------+---------+---------+
| 1        | 1   | R00000D   | R00004D | R00000D |
+----------+-----+-----------+---------+---------+
| 1        | 2   | R00000D   | R00004D | R00001D |
+----------+-----+-----------+---------+---------+
| 1        | 3   | R00000D   | R00004D | R00002D |
+----------+-----+-----------+---------+---------+
| 1        | 4   | R00000D   | R00004D | R00003D |
+----------+-----+-----------+---------+---------+
| 1        | 5   | R00000D   | R00004D | R00004D |
+----------+-----+-----------+---------+---------+
| 2        | 1   | R00012D   | R00013D | R00012D |
+----------+-----+-----------+---------+---------+
| 2        | 2   | R00012D   | R00013D | R00013D |
+----------+-----+-----------+---------+---------+
| 3        | 1   | R00015D   | R00018D | R00015D |
+----------+-----+-----------+---------+---------+
| 3        | 2   | R00015D   | R00018D | R00016D |
+----------+-----+-----------+---------+---------+
| 3        | 3   | R00015D   | R00018D | R00017D |
+----------+-----+-----------+---------+---------+
| 3        | 4   | R00015D   | R00018D | R00018D |
+----------+-----+-----------+---------+---------+
| 4        | 1   | R00021D   | R00021D | R00021D |
+----------+-----+-----------+---------+---------+
| 5        | 1   | R00025D   | R00027D | R00025D |
+----------+-----+-----------+---------+---------+
| 5        | 2   | R00025D   | R00027D | R00026D |
+----------+-----+-----------+---------+---------+
| 5        | 3   | R00025D   | R00027D | R00027D |
+----------+-----+-----------+---------+---------+