关于使用值层次结构更新列的 TSQL 问题

发布于 2025-01-11 17:43:41 字数 1251 浏览 2 评论 0原文

我有一个包含两列的表,层次结构(字母顺序)和访问权限,如下所示。

N  Hierarchy                    Access
1  A                            Y
2  A >B                         N
3  A >B >C                      NULL
4  A >B >C >D                   NULL
5  A >B >C >D >E                NULL
6  A >B >C >D >E >F             NULL
7  A >B >C >D >E >F >G          Y
8  A >B >C >D >E >F >G >J       NULL

我需要用这个逻辑更新访问列。如果 Access 值为 null,则使用 Access 不为 null 的下一个更高层次结构中的 Access 更新此行的 Access。

例如,

第 7 行的 Access 为 null,查询会将第 7 行的 Access 更新为 Y。因为层次结构 G 是 Y。

第 6 行将为 N,因为层次结构 E、D 和 C 为 null,B 为 N(不为 null)。

第 5 行将为 N,因为层次结构 D 和 C 为空,而 B 为 N(不为空)。

第 4 行将为 N,因为层次结构 C 为空,而 B 为 N(不为空)。

所以所需的输出将如下所示

N  Hierarchy                    Access
1  A                            Y
2  A >B                         N
3  A >B >C                      N
4  A >B >C >D                   N
5  A >B >C >D >E                N
6  A >B >C >D >E >F             N
7  A >B >C >D >E >F >G          Y
8  A >B >C >D >E >F >G >J       Y

我怎样才能实现这一目标?谢谢。

I have this table with two columns, Hierarchy (Alphabet order) and Access as below.

N  Hierarchy                    Access
1  A                            Y
2  A >B                         N
3  A >B >C                      NULL
4  A >B >C >D                   NULL
5  A >B >C >D >E                NULL
6  A >B >C >D >E >F             NULL
7  A >B >C >D >E >F >G          Y
8  A >B >C >D >E >F >G >J       NULL

I need to update Access column with this logic. If the Access value is null, update the Access for this row with Access from next higher hierarchy where Access is not null.

For example,

Row 7's Access is null, the query will update Row 7's Access to Y. Because hierarchy G is Y.

Row 6 will be N, because hierarchy E, D and C is null, and B is N (not null).

Row 5 will be N, because hierarchy D and C is null, and B is N (not null).

Row 4 will be N, because hierarchy C is null, and B is N (not null).

So the desired output would look like this

N  Hierarchy                    Access
1  A                            Y
2  A >B                         N
3  A >B >C                      N
4  A >B >C >D                   N
5  A >B >C >D >E                N
6  A >B >C >D >E >F             N
7  A >B >C >D >E >F >G          Y
8  A >B >C >D >E >F >G >J       Y

How can I achieve this? Thank you.

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

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

发布评论

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

评论(1

早茶月光 2025-01-18 17:43:41

老实说,我首先要重复我的评论:

我真的建议修改你的设计。像这样存储分隔数据不是一个好主意。 SQL Server 有一个内置的 hierachyid 数据类型,如果您不想使用它,最好使用主键和外键关系。

对于非规范化数据来说,这并不是很好的实现。您必须使用 LIKE 表达式,然后通过执行以下操作来删除不需要的 JOIN 行,其中包含“每组中的前 1 个”

CREATE TABLE dbo.DelimitedHierarchy (N int NOT NULL, 
                                     Hierarchy varchar(200) NOT NULL,
                                     Access char(1) NULL);
GO

INSERT INTO dbo.DelimitedHierarchy (N, Hierarchy, Access)
VALUES (1,'A','Y'),
       (2,'A >B','N'),
       (3,'A >B >C',NULL),
       (4,'A >B >C >D',NULL),
       (5,'A >B >C >D >E',NULL),
       (6,'A >B >C >D >E >F',NULL),
       (7,'A >B >C >D >E >F >G','Y'),
       (8,'A >B >C >D >E >F >G >J',NULL);
GO

WITH rCTE AS(
    SELECT DH.N,
           DH.Hierarchy,
           DH.Access,
           1 AS Level
    FROM dbo.DelimitedHierarchy DH
    WHERE DH.Hierarchy NOT LIKE '%>%'
    UNION ALL
    SELECT DH.N,
           DH.Hierarchy,
           ISNULL(DH.Access,r.Access),
           r.Level + 1
    FROM dbo.DelimitedHierarchy DH
         JOIN rCTE r ON DH.Hierarchy LIKE r.Hierarchy + ' >%')
SELECT TOP (1) WITH TIES 
       *
FROM rCTE r
ORDER BY ROW_NUMBER() OVER (PARTITION BY Hierarchy ORDER BY Level DESC);
GO

DROP TABLE dbo.DelimitedHierarchy;

:正确规范化的数据集,不使用 hierarchyid 那么查询将如下所示:

CREATE TABLE dbo.NormalisedHierarchy (N int NOT NULL, 
                                     value char(1) NOT NULL,
                                     parent char(1) NULL,
                                     Access char(1) NULL);
GO

INSERT INTO dbo.NormalisedHierarchy (N, Value, Parent, Access)
VALUES (1,'A',NULL,'Y'),
       (2,'B','A','N'),
       (3,'C','B',NULL),
       (4,'D','C',NULL),
       (5,'E','D',NULL),
       (6,'F','E',NULL),
       (7,'G','F','Y'),
       (8,'J','G',NULL);
GO

WITH rCTE AS(
    SELECT NH.N,
           NH.[value],
           NH.Access,
           1 AS Level
    FROM dbo.NormalisedHierarchy NH
    WHERE NH.parent IS NULL
    UNION ALL
    SELECT NH.N,
           NH.[value],
           ISNULL(NH.Access,r.Access),
           r.Level + 1
    FROM dbo.NormalisedHierarchy NH
         JOIN rCTE r ON NH.parent = r.[value])
SELECT *
FROM rCTE r
ORDER BY [Level] ASC;
GO

DROP TABLE dbo.NormalisedHierarchy;

或者,实现 hierarchyid。我没有提供解决方案,因为我从未使用过它们,并且使用过自引用标准化表。

Honestly, I'm going to firstly repeat my comment:

I really suggest fixing your design. Storing delimited data like this is not a good idea. SQL Server has a built in hierachyid data type, and if you don't want to use that, you are far better off using a primary and foreign key relationship.

This isn't pretty to achieve with your denormalised data. You have to use LIKE expressions and then get rid of the unwanted JOINed rows with a "TOP 1 in each group", by doing something like this:

CREATE TABLE dbo.DelimitedHierarchy (N int NOT NULL, 
                                     Hierarchy varchar(200) NOT NULL,
                                     Access char(1) NULL);
GO

INSERT INTO dbo.DelimitedHierarchy (N, Hierarchy, Access)
VALUES (1,'A','Y'),
       (2,'A >B','N'),
       (3,'A >B >C',NULL),
       (4,'A >B >C >D',NULL),
       (5,'A >B >C >D >E',NULL),
       (6,'A >B >C >D >E >F',NULL),
       (7,'A >B >C >D >E >F >G','Y'),
       (8,'A >B >C >D >E >F >G >J',NULL);
GO

WITH rCTE AS(
    SELECT DH.N,
           DH.Hierarchy,
           DH.Access,
           1 AS Level
    FROM dbo.DelimitedHierarchy DH
    WHERE DH.Hierarchy NOT LIKE '%>%'
    UNION ALL
    SELECT DH.N,
           DH.Hierarchy,
           ISNULL(DH.Access,r.Access),
           r.Level + 1
    FROM dbo.DelimitedHierarchy DH
         JOIN rCTE r ON DH.Hierarchy LIKE r.Hierarchy + ' >%')
SELECT TOP (1) WITH TIES 
       *
FROM rCTE r
ORDER BY ROW_NUMBER() OVER (PARTITION BY Hierarchy ORDER BY Level DESC);
GO

DROP TABLE dbo.DelimitedHierarchy;

If you had a properly normalised data set, not using hierarchyid then the query would look like this:

CREATE TABLE dbo.NormalisedHierarchy (N int NOT NULL, 
                                     value char(1) NOT NULL,
                                     parent char(1) NULL,
                                     Access char(1) NULL);
GO

INSERT INTO dbo.NormalisedHierarchy (N, Value, Parent, Access)
VALUES (1,'A',NULL,'Y'),
       (2,'B','A','N'),
       (3,'C','B',NULL),
       (4,'D','C',NULL),
       (5,'E','D',NULL),
       (6,'F','E',NULL),
       (7,'G','F','Y'),
       (8,'J','G',NULL);
GO

WITH rCTE AS(
    SELECT NH.N,
           NH.[value],
           NH.Access,
           1 AS Level
    FROM dbo.NormalisedHierarchy NH
    WHERE NH.parent IS NULL
    UNION ALL
    SELECT NH.N,
           NH.[value],
           ISNULL(NH.Access,r.Access),
           r.Level + 1
    FROM dbo.NormalisedHierarchy NH
         JOIN rCTE r ON NH.parent = r.[value])
SELECT *
FROM rCTE r
ORDER BY [Level] ASC;
GO

DROP TABLE dbo.NormalisedHierarchy;

Alternatively, implement a hierarchyid. I don't include a solution for that, as I have never worked with them, and have used self referencing normalised tables.

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