如何按照 SQL 中出现的顺序处理组?
我想运行下表,每次处理不同的组。第一次处理 group 1
的子级 3,4,5
,然后是 group 4
的 8
,然后是
。组 3
的 >6,7
我尝试使用 CURSOR 但我没能每次都获得不同的组。
重要:我必须按照组出现的顺序处理组,即组#1,然后#4,然后#3
更新
id group child
----------- ----------- -----------
1 1 3
2 1 4
3 1 5
4 4 8
5 3 6
6 3 7
我使用的查询是
declare @tbl table (
id int identity(1,1) not null,
parent int null,
child int null);
WITH t1 (parent, child)
AS
(
SELECT Parentid, Id
FROM mytable
WHERE ParentId = 1 and Id <> ParentId
UNION ALL
SELECT Parentid,Id
FROM mytable t2, t1
WHERE t2.ParentId = t1.child
)
insert into @tbl(parent, child)
SELECT parent,child
FROM t1
SELECT *
FROM @tbl
谢谢
I want to run over the below table and each time to process different group. 1st time process childs 3,4,5
of group 1
, then 8
of group 4
and then 6,7
of group 3
.
I tried using CURSOR but I didn't managed to get each time different group.
IMPORTANT: I must process the groups in the order they appears, namely group #1, then #4 and then #3
UPDATED
id group child
----------- ----------- -----------
1 1 3
2 1 4
3 1 5
4 4 8
5 3 6
6 3 7
the query I used is
declare @tbl table (
id int identity(1,1) not null,
parent int null,
child int null);
WITH t1 (parent, child)
AS
(
SELECT Parentid, Id
FROM mytable
WHERE ParentId = 1 and Id <> ParentId
UNION ALL
SELECT Parentid,Id
FROM mytable t2, t1
WHERE t2.ParentId = t1.child
)
insert into @tbl(parent, child)
SELECT parent,child
FROM t1
SELECT *
FROM @tbl
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server 不保证记录的顺序,除非您指定 ORDER BY。
即使将它们直接插入表中并读回,并行性、存储注意事项、索引和所有排序(我的技术术语)也可能导致它们以不同的顺序读回。
这并不意味着您每次都会得到不同的订单,只是您无法在没有 ORDER BY 的情况下保证订单。
就您而言,这意味着您需要另一列来说明数据插入的顺序。一个简单的方法就是在表上创建一个 IDENTITY 列。数据插入的顺序将保留在该列中,您可以在 ORDER BY 中使用它。
SQL Server doesn't guarantee the order of records, unless you specify an ORDER BY.
Even when inserting them directly into a table, and reading them back, parrallelism, storage considerations, indexes, and all-sorts (my technical term) can cause them to be read back in a different order.
This doesn't mean you get a different order every time, just that you can't guarnetee the order without the ORDER BY.
In your case, this means that you need another column to state what order the data was inserted. An easy way of doing that my be just to create an IDENTITY column on the table. The order the data is inserted will then be preserved in that column, and you can use it in an ORDER BY.
您可以使用
MIN
分组函数按父项在序列中首次出现的顺序对它们进行排序。像ROW_NUMBER()
或RANK()
这样的排名函数将获取每个父级的序号位置。You can use the
MIN
grouping function to sort parents by the order in which they first appear in the sequence. A ranking function likeROW_NUMBER()
orRANK()
will get the ordinal position of each parent.您可以使用排名函数
RANK()
和ROW_NUMBER()
以及ORDER BY
子句来阐明结果的顺序。You can use the ranking functions
RANK()
andROW_NUMBER()
, together with anORDER BY
clause to clarify the order of results.该脚本将返回我所需要的内容。它按 1,4,3 的顺序返回组,而不是 1,3,4
脚本和结果如下。
结果
This script will return EXACTLY what I need. It returns group in the order 1,4,3 and not 1,3,4
The script and the result are below.
The result