如何按照 SQL 中出现的顺序处理组?

发布于 2024-11-09 12:44:58 字数 1023 浏览 0 评论 0原文

我想运行下表,每次处理不同的组。第一次处理 group 1 的子级 3,4,5,然后是 group 48,然后是 组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-11-16 12:44:58

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.

野味少女 2024-11-16 12:44:58

您可以使用 MIN 分组函数按父项在序列中首次出现的顺序对它们进行排序。像 ROW_NUMBER()RANK() 这样的排名函数将获取每个父级的序号位置。

SELECT parent,
    RANK() OVER (ORDER BY MIN(id)) [ranking]
FROM t1
GROUP BY parent
ORDER BY [ranking]

You can use the MIN grouping function to sort parents by the order in which they first appear in the sequence. A ranking function like ROW_NUMBER() or RANK() will get the ordinal position of each parent.

SELECT parent,
    RANK() OVER (ORDER BY MIN(id)) [ranking]
FROM t1
GROUP BY parent
ORDER BY [ranking]
我不在是我 2024-11-16 12:44:58

您可以使用排名函数 RANK()ROW_NUMBER() 以及 ORDER BY 子句来阐明结果的顺序。

SELECT parent, child,
    RANK() OVER (ORDER BY parent) [group],
    ROW_NUMBER() OVER (PARTITION BY parent ORDER BY child) [item]
FROM t1
ORDER BY parent, child

You can use the ranking functions RANK() and ROW_NUMBER(), together with an ORDER BY clause to clarify the order of results.

SELECT parent, child,
    RANK() OVER (ORDER BY parent) [group],
    ROW_NUMBER() OVER (PARTITION BY parent ORDER BY child) [item]
FROM t1
ORDER BY parent, child
一影成城 2024-11-16 12:44:58

该脚本将返回我所需要的内容。它按 1,4,3 的顺序返回组,而不是 1,3,4
脚本和结果如下。

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 id, parent
    from (select max(id) as id, parent
         from @tbl
         group by parent) t
    order by t.id

结果

 id          parent
----------- -----------
3           1
4           4
6           3

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.

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 id, parent
    from (select max(id) as id, parent
         from @tbl
         group by parent) t
    order by t.id

The result

 id          parent
----------- -----------
3           1
4           4
6           3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文