filmov
tv
How to select multiple rows of data in a single row in SQL Server

Показать описание
How to select multiple rows of data in a single row in SQL Server
Convert Multiple rows into a single row with commas separated
INPUT QUERY
-----------------------------------
SELECT
c.CategoryName
, sc.SubCategoryName
FROM SubCategory sc
INNER JOIN Category c
ON sc.CategoryID = c.CategoryID
OUTPUT QUERY
------------------------------------
SELECT
c.CategoryName
,STRING_AGG(sc.SubCategoryName, ',') AS SubCategories
FROM SubCategory sc
INNER JOIN Category c
ON sc.CategoryID = c.CategoryID
GROUP BY c.CategoryName
#sqlserver2017
#sqlserver2019
#sqlserver2022
Convert Multiple rows into a single row with commas separated
INPUT QUERY
-----------------------------------
SELECT
c.CategoryName
, sc.SubCategoryName
FROM SubCategory sc
INNER JOIN Category c
ON sc.CategoryID = c.CategoryID
OUTPUT QUERY
------------------------------------
SELECT
c.CategoryName
,STRING_AGG(sc.SubCategoryName, ',') AS SubCategories
FROM SubCategory sc
INNER JOIN Category c
ON sc.CategoryID = c.CategoryID
GROUP BY c.CategoryName
#sqlserver2017
#sqlserver2019
#sqlserver2022