我遇到与此question相同的问题。
但问题是,查询只适用于MySQL而不适用于MSSQL。我试图手动转换它,但我似乎无法理解像:= [value] AS [value]
一样的语法等同于
所以,我想知道是否有办法将MySQL查询转换为在MSSQL上使用它?
编辑:预期输出
输入
Company Date Paper Condition
Company1 19-12-2007 PaperA Release Second Term
Company1 19-12-2007 PaperA Add Third Term
Company1 19-12-2007 PaperA Append First Term
Company2 19-12-2007 PaperA Add Third Term
Company2 19-12-2007 PaperA Append First Term
Company2 19-12-2007 PaperA Add Third Term
输出
Company Date Paper Condition
Company1 19-12-2007 PaperA Release Second Term
Add Third Term
Append First Term
Company2 19-12-2007 PaperA Add Third Term
Append First Term
Add Third Term
答案 0 :(得分:0)
在SQL 2017+中,您可以使用STRING_AGG
:
SELECT Company, Date, Paper, STRING_AGG(Condition,CHAR(13)+CHAR(10)) AS Condition
FROM dbo.YourTable
GROUP BY Company, Date, Paper;
在早期的SQL版本中,使用FOR XML和STUFF来删除额外的换行符:
SELECT Company, Date, Paper
,STUFF((SELECT CHAR(13)+CHAR(10)+Condition FROM dbo.Example AS b
WHERE b.Company = a.Company AND b.Date = a.Date AND b.Paper = a.Paper
FOR XML PATH(''), TYPE).value('.','nvarchar(MAX)'),1,2,'') AS Condition
FROM dbo.YourTable AS a
GROUP BY Company, Date, Paper;