SQL 需要从列中返回范围
Posted
技术标签:
【中文标题】SQL 需要从列中返回范围【英文标题】:SQL need to return ranges from a column 【发布时间】:2011-11-28 18:42:02 【问题描述】:我有一个表,其中有一个名为 ID 的整数列,其中的值可能有间隙(例如 1、2、3、4、7、8、10、14、15、16、20)
我想找到一个查询,在上面的示例中,结果如下:
1-4 7-8 10 14-16 20
= 更新 =
感谢下面的代码(它似乎在 SQL Server 中运行良好),我觉得我非常接近让它在 MS-Access 中运行,这是目标。虽然在我的陈述中我仍然遇到语法错误,但我无法弄清楚......
选择值从 (
SELECT islands.PORTID, CSTR(islands.PORTID ) as val
FROM MYTABLE islands
WHERE NOT EXISTS (SELECT * FROM MYTABLE t2 WHERE t2.PORTID = islands.PORTID - 1)
AND NOT EXISTS (SELECT * FROM MYTABLE t2 WHERE t2.PORTID = islands.PORTID + 1)
UNION
SELECT
rngStart.PORTID
,CSTR(rngStart.PORTID ) + '-'
+ CSTR(MIN(rngEnd.PORTID)) as val
FROM MYTABLE rngStart
INNER JOIN MYTABLE checkNext ON checkNext.PORTID = rngStart.PORTID + 1
INNER JOIN
(
SELECT PORTID
FROM MYTABLE tblRangeEnd
WHERE NOT EXISTS (SELECT * FROM MYTABLE t2 WHERE t2.PORTID = tblRangeEnd.PORTID + 1)
) rngEnd on rngEnd.PORTID > rngStart.PORTID
WHERE NOT EXISTS (SELECT * FROM MYTABLE t2 WHERE t2.PORTID = rngStart.PORTID - 1)
GROUP BY rngStart.PORTID
) 作为 tbl 按 PORTID ASC 排序
【问题讨论】:
Sql 服务器? PostgreSQL?甲骨文?语法可能会有所不同,因此了解正确的语法会很有用。 最终我需要能够支持 SQL Server、Oracle 和 Access。不幸的是,MS-Access 是最重要的。 @user1069916:请参阅我的编辑以了解如何在 Access 中进行这项工作。 SQL Query for Grouping the results based on sequence的可能重复 【参考方案1】:SELECT val FROM
(
-- Get the islands
SELECT islands.ID, CAST(islands.ID as varchar(10)) as val
FROM @t1 islands
WHERE NOT EXISTS (SELECT * FROM @t1 t2 WHERE t2.ID = islands.ID - 1)
AND NOT EXISTS (SELECT * FROM @t1 t2 WHERE t2.ID = islands.ID + 1)
UNION
-- Get the ranges
SELECT
rngStart.ID
,CAST(rngStart.ID as varchar(10)) + '-'
+ CAST(MIN(rngEnd.ID) as varchar(10)) as val
FROM @t1 rngStart
INNER JOIN @t1 checkNext ON checkNext.ID = rngStart.ID + 1
INNER JOIN
(
SELECT ID
FROM @t1 tblRangeEnd
WHERE NOT EXISTS (SELECT * FROM @t1 t2 WHERE t2.ID = tblRangeEnd.ID + 1)
) rngEnd on rngEnd.ID > rngStart.ID
WHERE NOT EXISTS (SELECT * FROM @t1 t2 WHERE t2.ID = rngStart.ID - 1)
GROUP BY rngStart.ID
) as tbl
ORDER BY ID ASC
我使用了一个名为 @t1 的表变量,但只需将其替换为您的表名即可。
Here 正在行动中。
编辑
要在 Access 中进行这项工作,您必须稍微更改联接。试试这个:
SELECT val FROM
(
SELECT islands.PORTID, CSTR(islands.PORTID) as val
FROM MYTABLE islands
WHERE NOT EXISTS
(SELECT * FROM MYTABLE t2 WHERE t2.PORTID = islands.PORTID - 1)
AND NOT EXISTS
(SELECT * FROM MYTABLE t2 WHERE t2.PORTID = islands.PORTID + 1)
UNION
SELECT
rngStart.PORTID
,CSTR(rngStart.PORTID) + '-' + CSTR(MIN(endPORTID)) as val
FROM MYTABLE rngStart
INNER JOIN
(
SELECT checkNext.PORTID as nextPORTID, rngEnd.PORTID as endPORTID
FROM MYTABLE checkNext
INNER JOIN
(
SELECT rngEnd.PORTID
FROM MYTABLE rngEnd
WHERE NOT EXISTS
(SELECT * FROM MYTABLE t2 WHERE t2.PORTID = rngEnd.PORTID + 1)
) AS rngEnd on rngEnd.PORTID > checkNext.PORTID - 1
) AS checkNext ON checkNext.nextPORTID = rngStart.PORTID + 1
WHERE NOT EXISTS
(SELECT * FROM MYTABLE t2 WHERE t2.PORTID = rngStart.PORTID - 1)
GROUP BY rngStart.PORTID
) as tbl
ORDER BY PORTID ASC
【讨论】:
这简直太棒了。非常感谢。【参考方案2】:create table #sequence (id int not null primary KEY)
insert into #sequence(id)
select 1
union all select 2
union all select 3
union all select 4
union all select 7
union all select 8
union all select 10
union all select 14
union all select 15
union all select 16
union all select 20
--Find Contig ranges
select l.id as start,
(
select min(a.id) as id
from #sequence as a
left outer join #sequence as b on a.id = b.id - 1
where b.id is null
and a.id >= l.id
) AS fend
from #sequence as l
left outer join #sequence as r on r.id = l.id - 1
where r.id is null;
--Find missing values in sequence
select l.id + 1 as start, min(fr.id) - 1 as stop
from #sequence as l
left outer join #sequence as r on l.id = r.id - 1
left outer join #sequence as fr on l.id < fr.id
where r.id is null and fr.id is not null
group by l.id, r.id;
drop table #sequence
这将为您提供范围和范围之间的差距,以便您了解自己拥有什么或需要什么。提供的样本数据
【讨论】:
【参考方案3】:如果您访问函数row_number()
,这将起作用。
with C as
(
select ID,
ID - row_number() over(order by ID) as grp
from YourTable
)
select min(ID) as MinID,
max(ID) as MaxID
from C
group by grp
或使用子查询代替公用表表达式。
select min(ID) as MinID,
max(ID) as MaxID
from (select ID,
ID - row_number() over(order by ID) as grp
from YourTable) as C
group by grp
结果:
MinID MaxID
----------- -----------
1 4
7 8
10 10
14 16
20 20
在 SQL Server 上试用https://data.stackexchange.com/***/q/119411/
【讨论】:
【参考方案4】:再试一次:
SELECT
MIN(i), MAX(i)
FROM (
select
i - (SELECT COUNT(*) FROM tbl t WHERE t.i < tbl.i) g,
i
from tbl
) t
GROUP BY g
肯定会很慢,但我没有看到其他方法来对访问中的行进行编号。
【讨论】:
以上是关于SQL 需要从列中返回范围的主要内容,如果未能解决你的问题,请参考以下文章