Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 显示具有多个选项的人的一行_Sql Server_Tsql_Dynamic_Pivot - Fatal编程技术网

Sql server 显示具有多个选项的人的一行

Sql server 显示具有多个选项的人的一行,sql-server,tsql,dynamic,pivot,Sql Server,Tsql,Dynamic,Pivot,我面临的问题是,我正在寻找实现以下目标的方法: 我需要为每个人获得一行,列显示哪些组已链接到该人。我有以下三张桌子 人员-包含人员数据 组-包含组数据 链接-包含个人和组之间的链接 示例数据(脚本): Create table persons (uniqueid int, email varchar(50)) Create table groups (uniqueid int, title varchar(50)) Create table link (uniqueid int, groupid

我面临的问题是,我正在寻找实现以下目标的方法:

我需要为每个人获得一行,列显示哪些组已链接到该人。我有以下三张桌子

  • 人员-包含人员数据
  • 组-包含组数据
  • 链接-包含个人和组之间的链接
  • 示例数据(脚本):

    Create table persons (uniqueid int, email varchar(50))
    Create table groups (uniqueid int, title varchar(50))
    Create table link (uniqueid int, groupid int, personid int)
    
    insert into persons (uniqueid, email) values (1, 'firstname1.lastname1@domain.com'), (2, 'firstname2.lastname2@domain.com'), (3, 'firstname3.lastname3@domain.com')
    insert into groups (uniqueid, title) values (1, 'Servicedesk'), (2, 'SecondLine'), (3, 'ThirdLine')
    insert into link (uniqueid, groupid, personid) values (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 3, 2), (6, 1, 3)
    
    select p.email, g.title
    FROM link as l
        left join groups g on l.groupid = g.uniqueid
        left join persons p on l.personid = p.uniqueid
    group by p.email, g.title
    
    email                           title
    firstname1.lastname1@domain.com SecondLine
    firstname1.lastname1@domain.com Servicedesk
    firstname2.lastname2@domain.com Servicedesk
    firstname2.lastname2@domain.com ThirdLine
    firstname3.lastname3@domain.com Servicedesk
    
    email                           Group1       Group2       Group3       
    firstname1.lastname1@domain.com Servicedesk  SecondLine   NULL
    firstname2.lastname2@domain.com Servicedesk  NULL         NULL
    firstname3.lastname3@domain.com Servicedesk  NULL         NULL
    
    当前查询:

    Create table persons (uniqueid int, email varchar(50))
    Create table groups (uniqueid int, title varchar(50))
    Create table link (uniqueid int, groupid int, personid int)
    
    insert into persons (uniqueid, email) values (1, 'firstname1.lastname1@domain.com'), (2, 'firstname2.lastname2@domain.com'), (3, 'firstname3.lastname3@domain.com')
    insert into groups (uniqueid, title) values (1, 'Servicedesk'), (2, 'SecondLine'), (3, 'ThirdLine')
    insert into link (uniqueid, groupid, personid) values (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 3, 2), (6, 1, 3)
    
    select p.email, g.title
    FROM link as l
        left join groups g on l.groupid = g.uniqueid
        left join persons p on l.personid = p.uniqueid
    group by p.email, g.title
    
    email                           title
    firstname1.lastname1@domain.com SecondLine
    firstname1.lastname1@domain.com Servicedesk
    firstname2.lastname2@domain.com Servicedesk
    firstname2.lastname2@domain.com ThirdLine
    firstname3.lastname3@domain.com Servicedesk
    
    email                           Group1       Group2       Group3       
    firstname1.lastname1@domain.com Servicedesk  SecondLine   NULL
    firstname2.lastname2@domain.com Servicedesk  NULL         NULL
    firstname3.lastname3@domain.com Servicedesk  NULL         NULL
    
    输出当前查询:

    Create table persons (uniqueid int, email varchar(50))
    Create table groups (uniqueid int, title varchar(50))
    Create table link (uniqueid int, groupid int, personid int)
    
    insert into persons (uniqueid, email) values (1, 'firstname1.lastname1@domain.com'), (2, 'firstname2.lastname2@domain.com'), (3, 'firstname3.lastname3@domain.com')
    insert into groups (uniqueid, title) values (1, 'Servicedesk'), (2, 'SecondLine'), (3, 'ThirdLine')
    insert into link (uniqueid, groupid, personid) values (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 3, 2), (6, 1, 3)
    
    select p.email, g.title
    FROM link as l
        left join groups g on l.groupid = g.uniqueid
        left join persons p on l.personid = p.uniqueid
    group by p.email, g.title
    
    email                           title
    firstname1.lastname1@domain.com SecondLine
    firstname1.lastname1@domain.com Servicedesk
    firstname2.lastname2@domain.com Servicedesk
    firstname2.lastname2@domain.com ThirdLine
    firstname3.lastname3@domain.com Servicedesk
    
    email                           Group1       Group2       Group3       
    firstname1.lastname1@domain.com Servicedesk  SecondLine   NULL
    firstname2.lastname2@domain.com Servicedesk  NULL         NULL
    firstname3.lastname3@domain.com Servicedesk  NULL         NULL
    
    预期最终结果:

    Create table persons (uniqueid int, email varchar(50))
    Create table groups (uniqueid int, title varchar(50))
    Create table link (uniqueid int, groupid int, personid int)
    
    insert into persons (uniqueid, email) values (1, 'firstname1.lastname1@domain.com'), (2, 'firstname2.lastname2@domain.com'), (3, 'firstname3.lastname3@domain.com')
    insert into groups (uniqueid, title) values (1, 'Servicedesk'), (2, 'SecondLine'), (3, 'ThirdLine')
    insert into link (uniqueid, groupid, personid) values (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 3, 2), (6, 1, 3)
    
    select p.email, g.title
    FROM link as l
        left join groups g on l.groupid = g.uniqueid
        left join persons p on l.personid = p.uniqueid
    group by p.email, g.title
    
    email                           title
    firstname1.lastname1@domain.com SecondLine
    firstname1.lastname1@domain.com Servicedesk
    firstname2.lastname2@domain.com Servicedesk
    firstname2.lastname2@domain.com ThirdLine
    firstname3.lastname3@domain.com Servicedesk
    
    email                           Group1       Group2       Group3       
    firstname1.lastname1@domain.com Servicedesk  SecondLine   NULL
    firstname2.lastname2@domain.com Servicedesk  NULL         NULL
    firstname3.lastname3@domain.com Servicedesk  NULL         NULL
    
    为了开始尝试解决这个问题,我尝试使用
    FOR XML PATH(“”)
    为每个人创建一行,所有组在一列中,但这会生成错误
    列名“Email Address”包含FOR XML所需的无效XML标识符;“”(0x0020)是出现故障的第一个字符。
    在我的生产环境中。 在这一行之后,我想将它们拆分为列。
    目前,我正在搜索要使用的正确函数,以便构建完整的查询。

    您可以动态生成列的名称,并在
    PIVOT

    DECLARE @persons TABLE (uniqueid int, email varchar(50))
    DECLARE @groups TABLE(uniqueid int, title varchar(50))
    DECLARE @link TABLE(uniqueid int, groupid int, personid int)
    
    insert into @persons (uniqueid, email) values (1, 'firstname1.lastname1@domain.com'), (2, 'firstname2.lastname2@domain.com'), (3, 'firstname3.lastname3@domain.com')
    insert into @groups (uniqueid, title) values (1, 'Servicedesk'), (2, 'SecondLine'), (3, 'ThirdLine')
    insert into @link (uniqueid, groupid, personid) values (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 2, 1), (5, 3, 2), (6, 1, 3)
    
    SELECT p.*
    FROM
    (
        select p.email
              ,g.title
              ,'Group'+CAST(ROW_NUMBER() OVER(PARTITION BY p.email ORDER BY (SELECT NULL)) AS varchar(2)) AS ColumnName
        FROM @link as l
            left join @groups g on l.groupid = g.uniqueid
            left join @persons p on l.personid = p.uniqueid
        group by p.email, g.title
    ) AS tbl
    PIVOT
    (
        MAX(title) FOR ColumnName IN(Group1,Group2,Group3, Group4 /*add as many as you need*/)
    ) AS p;
    
    结果

    email                           Group1      Group2      Group3  Group4
    firstname1.lastname1@domain.com SecondLine  Servicedesk NULL    NULL
    firstname2.lastname2@domain.com Servicedesk ThirdLine   NULL    NULL
    firstname3.lastname3@domain.com Servicedesk NULL        NULL    NULL
    

    我不知道如何获得预期的结果,group2和group3是从哪里派生的?我有一个函数,可以将字符串拆分为多列。但我还不能将我的数据放入每个电子邮件地址的1行中。答案很好,还添加了添加更多组的功能,以帮助将来验证OPs代码+1这种动态数据透视的方式正是我想要的+1.