T-SQL MS SQL 东西。如何使用 group by 将行合并为单行
T-SQL MS SQL Stuff. How to combine rows into single row with group by
我有一个SQL查询:
SELECT [B].[Id]
,[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
,[D].[Name] AS ResponsibleDepartment
FROM [Breakdowns_Kohl].[dbo].[Breakdowns] AS [B]
INNER JOIN Workcenters AS [WC] ON B.WorkcenterId = WC.Id
INNER JOIN SubAreas AS [S] ON B.SubAreaId = S.Id
INNER JOIN Areas AS [A] ON B.AreaId = A.ID
LEFT JOIN Relation_Department_Workcenter AS REL_DW ON B.WorkcenterId = REL_DW.WorkcenterId
LEFT JOIN Department AS [D] ON REL_DW.DepartmentId = D.Id
哪个returnstable:
我不知道如何将它放入单行,最后一列用逗号分隔,如:
将最后一列聚合成一个名为 ResponsibleDepartment 的逗号分隔字符串,类似这样
SELECT [B].[Id]
,[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
,string_agg([D].[Name] , ',') within group (order by [B].id) ResponsibleDepartment
FROM [Breakdowns_Kohl].[dbo].[Breakdowns] AS [B]
INNER JOIN Workcenters AS [WC] ON B.WorkcenterId = WC.Id
INNER JOIN SubAreas AS [S] ON B.SubAreaId = S.Id
INNER JOIN Areas AS [A] ON B.AreaId = A.ID
LEFT JOIN Relation_Department_Workcenter AS REL_DW ON B.WorkcenterId = REL_DW.WorkcenterId
LEFT JOIN Department AS [D] ON REL_DW.DepartmentId = D.Id
group by [B].[Id],
[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
如果您使用的是 SQL 服务器 2017 或更高版本,您可以轻松使用 String_AGG 聚合函数,如果不是,您可以使用 stuff 函数和 xml 就像这个问题:
我有一个SQL查询:
SELECT [B].[Id]
,[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
,[D].[Name] AS ResponsibleDepartment
FROM [Breakdowns_Kohl].[dbo].[Breakdowns] AS [B]
INNER JOIN Workcenters AS [WC] ON B.WorkcenterId = WC.Id
INNER JOIN SubAreas AS [S] ON B.SubAreaId = S.Id
INNER JOIN Areas AS [A] ON B.AreaId = A.ID
LEFT JOIN Relation_Department_Workcenter AS REL_DW ON B.WorkcenterId = REL_DW.WorkcenterId
LEFT JOIN Department AS [D] ON REL_DW.DepartmentId = D.Id
哪个returnstable:
我不知道如何将它放入单行,最后一列用逗号分隔,如:
将最后一列聚合成一个名为 ResponsibleDepartment 的逗号分隔字符串,类似这样
SELECT [B].[Id]
,[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
,string_agg([D].[Name] , ',') within group (order by [B].id) ResponsibleDepartment
FROM [Breakdowns_Kohl].[dbo].[Breakdowns] AS [B]
INNER JOIN Workcenters AS [WC] ON B.WorkcenterId = WC.Id
INNER JOIN SubAreas AS [S] ON B.SubAreaId = S.Id
INNER JOIN Areas AS [A] ON B.AreaId = A.ID
LEFT JOIN Relation_Department_Workcenter AS REL_DW ON B.WorkcenterId = REL_DW.WorkcenterId
LEFT JOIN Department AS [D] ON REL_DW.DepartmentId = D.Id
group by [B].[Id],
[B].[CreateDate]
,[Author]
,[B].[WorkcenterId]
,[B].[AreaId]
,[B].[SubAreaId]
,[B].[Description]
,[PriorityId]
,[BreakdownStatusEnum]
,[ApproveDate]
,[StartDate]
,[FinishedDate]
,[BreakdownStartCheckListId]
,[ApprovedBy]
,[BreakdownReason]
如果您使用的是 SQL 服务器 2017 或更高版本,您可以轻松使用 String_AGG 聚合函数,如果不是,您可以使用 stuff 函数和 xml 就像这个问题: