SQL Server 通过将列填充到列表中来组合多行

jhdbpxl9  于 2023-01-08  发布在  其他
关注(0)|答案(3)|浏览(102)

我有四个表employees、associations_employees、associations、association_items。下面的选择查询生成了连接行。

    • 注意:我为SQL和Coldfusion语言制作了标记,这是因为我使用Coldfusion作为我的脚本语言。我不确定我应该依赖SQL还是使用我的脚本语言。**
    • 查询**
SELECT  AE.userid, E.firstname,
    A.title, AI.itemvalue
FROM  associations_employees AE
    INNER JOIN employees E on E.userid = AE.useridFK
    INNER JOIN associations A on A.associationid = AE.associationidFK
    INNER JOIN association_items AI on AI.associationidFK = AE.associationidFK
    • 电流选择输出**
userID  firstname  title           itemvalue
------  ---------  -----           ---------
5603    Jesh       Learner Type    Family Literacy   
5603    Jesh       Learner Type    Elementary School 
5603    Jesh       Learner Type    Academic             
5603    Jesh       Personnel Type  Staff             
5605    jennone    Personnel Type  Site Supervisor   
5605    jennone    Personnel Type  Rops member       
5607    Sharon     Personnel Type  Rops member       
5607    Sharon     Personnel Type  Site Supervisor
5607    Sharon     Mentor Type     High School
5607    Sharon     Mentor Type     Op. Read
5607    Sharon     Mentor Type     Enrichment
5607    Sharon     Mentor Type     General

正如您所注意到的,除了"itemvalue"列之外,有多个行是相似的。我需要将这些行合并以生成以下结果。

    • 所需产出**
userID  firstname  title           itemvalue
------  ---------  ------          ---------
5603    Jesh       Learner Type    Family Literacy;Elementary School;Academic
5603    Jesh       Personnel Type  Staff
5605    jennone    Personnel Type  Rops member;Site Supervisor;Staff
5607    Sharon     Personnel Type  Rops member;Site Supervisor
5607    Sharon     Mentor Type     Enrichment;General;High School;Op. Read
kulphzqa

kulphzqa1#

您可以使用STUFF方法来实现这一点:

SELECT AE.userid,
       E.firstname,
       A.title,
       STUFF((SELECT ',' + [AI.itemvalue]
              FROM association_items AI
              WHERE AI.associationidFK = AE.associationidFK
              FOR XML PATH('')), 1, 1, '') AS itemvalue
FROM associations_employees AE
INNER JOIN employees E ON E.userid = AE.useridFK
INNER JOIN associations A ON A.associationid = AE.associationidFK
GROUP BY AE.userid, E.firstname, A.title,

这还没有经过测试,所以可能需要一些微调。

hmae6n7t

hmae6n7t2#

如果您需要ColdFusion解决方案,cfoutput的group属性将起作用。步骤1是向查询中添加order by子句。

order by userid, title

接下来是cfoutput标记。

<cfoutput query="yourquery" group = "userid">
  <cfoutput group = "title">
    #userid # #firstname# #lastname# #title#
    <cfset items = ''>
    <cfoutput>
      <cfset items = listappend(items,itemvalue,';')>
    </cfoutput>
    #items#
  </cfoutput>
</cfoutput>

这是基本的方法,你必须添加格式,并弄清楚如何从itemvalue列表中排除尾部的分号。

zsbz8rwp

zsbz8rwp3#

你可以使用类似于Dan的数组方法,数组可能更快

<cfoutput query="yourquery" group = "userid">
  <cfoutput group = "title">
    #userid # #firstname# #lastname# #title#
    <cfset items = []>
    <cfoutput>
        <cfset ArrayAppend(items, itemvalue)>
    </cfoutput>
    #ArrayToList(items, ";")#
  </cfoutput>
</cfoutput>

相关问题