使用 Sql Server 2005 过滤自下而上的递归 CTE

发布于 2024-11-27 14:01:07 字数 1768 浏览 0 评论 0原文

我试图从下往上查询单个数据库表中的数据层次结构(我不想包括由于权限而没有特定类型孩子的父母)。架构和示例数据如下:

create table Users(
id int,
name varchar(100));

insert into Users values (1, 'Jill');

create table nodes(
    id int,
    name varchar(100),
    parent int,
    nodetype int);

insert into nodes values (1, 'A', 0, 1);
insert into nodes values (2, 'B', 0, 1);
insert into nodes values (3, 'C', 1, 1);
insert into nodes values (4, 'D', 3, 2);
insert into nodes values (5, 'E', 1, 1);
insert into nodes values (6, 'F', 5, 2);
insert into nodes values (7, 'G', 5, 2);

create table nodeAccess(
    userid int,
    nodeid int,
    access int);

insert into nodeAccess values (1, 1, 1);
insert into nodeAccess values (1, 2, 1);
insert into nodeAccess values (1, 3, 1);
insert into nodeAccess values (1, 4, 1);
insert into nodeAccess values (1, 5, 1);
insert into nodeAccess values (1, 6, 0);
insert into nodeAccess values (1, 7, 1);


with Tree(id, name, nodetype, parent)
as
(
    select n.id, n.name, n.nodetype, n.parent
    from nodes as n
    inner join nodeAccess as na on na.nodeid = n.id
    where na.access =1 and na.userid=1 and n.nodetype=2

    union all

    select n.id, n.name, n.nodetype, n.parent
    from nodes as n
    inner join Tree as t on t.parent = n.id
    inner join nodeAccess as na on na.nodeid = n.id
    where na.access =1 and na.userid=1 and n.nodetype=1
)
select * from Tree

产量:

  id    name    nodetype    parent
   4    D        2             3
   7    G        2             5
   5    E        1             1
   1    A        1             0
   3    C        1             1
   1    A        1             0

如何才能在结果集中不包含重复项?针对实际表的查询在最低级别有更多的节点,因此父节点有更多的重复项。该解决方案至少需要与 SQL Server 2005 配合使用。

提前致谢!

I'm trying to query a hierarchy of data in a single database table from the bottom up (I don't want to include parents that don't have a particular type of child due to authorities). The schema and sample data are as follows:

create table Users(
id int,
name varchar(100));

insert into Users values (1, 'Jill');

create table nodes(
    id int,
    name varchar(100),
    parent int,
    nodetype int);

insert into nodes values (1, 'A', 0, 1);
insert into nodes values (2, 'B', 0, 1);
insert into nodes values (3, 'C', 1, 1);
insert into nodes values (4, 'D', 3, 2);
insert into nodes values (5, 'E', 1, 1);
insert into nodes values (6, 'F', 5, 2);
insert into nodes values (7, 'G', 5, 2);

create table nodeAccess(
    userid int,
    nodeid int,
    access int);

insert into nodeAccess values (1, 1, 1);
insert into nodeAccess values (1, 2, 1);
insert into nodeAccess values (1, 3, 1);
insert into nodeAccess values (1, 4, 1);
insert into nodeAccess values (1, 5, 1);
insert into nodeAccess values (1, 6, 0);
insert into nodeAccess values (1, 7, 1);


with Tree(id, name, nodetype, parent)
as
(
    select n.id, n.name, n.nodetype, n.parent
    from nodes as n
    inner join nodeAccess as na on na.nodeid = n.id
    where na.access =1 and na.userid=1 and n.nodetype=2

    union all

    select n.id, n.name, n.nodetype, n.parent
    from nodes as n
    inner join Tree as t on t.parent = n.id
    inner join nodeAccess as na on na.nodeid = n.id
    where na.access =1 and na.userid=1 and n.nodetype=1
)
select * from Tree

Yields:

  id    name    nodetype    parent
   4    D        2             3
   7    G        2             5
   5    E        1             1
   1    A        1             0
   3    C        1             1
   1    A        1             0

How can I not include the duplicates in the result set? The queries against the real tables have many more nodes at the lowest levels and hence many more duplicates of the parent nodes. The solution needs to work with at least SQL Server 2005.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

十级心震 2024-12-04 14:01:07

最简单(不一定是最有效)的解决方案:

...
)
SELECT DISTINCT id,name,nodetype,parent FROM Tree;

这会更改示例输出的顺序,因为 DISTINCT 运算符实现排序。如果那里有一些故意排序,我无法检测到它,但如果您知道所需的顺序,则可以添加 ORDER BY

The simplest (not necessarily the most efficient) solution:

...
)
SELECT DISTINCT id,name,nodetype,parent FROM Tree;

This changes the order from your sample output because the DISTINCT operator implements a sort. If there is some intentional ordering there I cannot detect it but you can add an ORDER BY if you know the order you want.

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