当前位置:Gxlcms > 数据库问题 > SQL多行并一行统计例子之STUFF()函数+FOR XML PATH()函数应用

SQL多行并一行统计例子之STUFF()函数+FOR XML PATH()函数应用

时间:2021-07-01 10:21:17 帮助过:28人阅读

* FROM tbiz_ProjectRelation

技术分享图片

目标统计每个项目有几条申请记录

Step1

SELECT  ProjectID ,
        RelationIDs = STUFF(( SELECT , + CONVERT(VARCHAR,[RelationID]) FROM  tbiz_ProjectRelation t WHERE ProjectID = t1.ProjectID FOR XML PATH(‘‘)), 1, 1, ‘‘)
FROM tbiz_ProjectRelation t1

Step2

SELECT  ProjectID ,
        RelationIDs = STUFF(( SELECT , + CONVERT(VARCHAR,[RelationID]) FROM  tbiz_ProjectRelation t WHERE ProjectID = t1.ProjectID FOR XML PATH(‘‘)), 1, 1, ‘‘)
FROM tbiz_ProjectRelation t1 GROUP BY ProjectID

Step3

SELECT  ProjectID ,
        RelationIDs = STUFF(( SELECT , + CONVERT(VARCHAR,[RelationID]) FROM  tbiz_ProjectRelation t WHERE ProjectID = t1.ProjectID FOR XML PATH(‘‘)), 1, 1, ‘‘),
        COUNT([RelationID]) AS [COUNT]
FROM tbiz_ProjectRelation t1 GROUP BY ProjectID

技术分享图片

 

SQL多行并一行统计例子之STUFF()函数+FOR XML PATH()函数应用

标签:函数应用   arc   font   var   .project   bubuko   gpo   body   count   

人气教程排行