SQL 需要从列返回范围
我有一个表,其中有一个名为 ID 的整数列,其值可能有间隙(例如 1,2,3,4,7,8,10,14,15,16,20)
我想找到一个查询,在上面的示例中,结果如下:
1-4 7-8 10 14-16日 20
= UPDATE =
感谢下面的代码(似乎在 SQL Server 中运行良好),我觉得我非常接近让它在 MS-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(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
) 作为表 按 PORTID ASC 订购
I have a table that has an integer column named ID with values that may have gaps (e.g. 1,2,3,4,7,8,10,14,15,16,20)
I would like to find a query that would, in the example above, result in this:
1-4
7-8
10
14-16
20
= UPDATE =
Thanks to the code below (which seems to work well in SQL Server), I feel I am very close to getting this to work in MS-Access which is the goal. I'm still getting a syntax error though in my statment which I can't figure out...
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(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
) as tbl
ORDER BY PORTID ASC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用了一个名为 @t1 的表变量,但只需将其替换为您的表名称即可。
这里它正在发挥作用。
编辑
要使此功能在 Access 中工作,您必须稍微更改连接。试试这个:
I used a table variable called @t1 but just replace that with your table name.
Here it is in action.
EDIT
To make this work in Access you will have to change the joins a little bit. Try this:
这将为您提供范围以及范围之间的差距,以便您可以看到您拥有什么或您需要什么。提供样本数据
This will give you both the ranges and the gaps between the ranges so you can see what you have or what you need. sample data provided
如果您访问函数
row_number()
,这将起作用。或者使用子查询而不是公共表表达式。
结果:
在 SQL Server 上尝试 https://data.stackexchange.com/stackoverflow/q/ 119411/
This will work if you access to function
row_number()
.or with a sub-query instead of a common table expression.
Result:
Try it on SQL Server https://data.stackexchange.com/stackoverflow/q/119411/
另一种尝试:
它肯定会很慢,但我没有看到其他方法来对访问中的行进行编号。
Another try:
It'll be slow for sure, but I don't see other way to number rows in access.