我在SQL Server中有两个表:
Table1
具有列SEQ,ITEMNO,Location,EXPARRIVAL,OQordered,OQreceived,OQoutstand Table2
的列为SEQ,PONUMBER 我通过匹配SEQ
加入了两个表:
SELECT
a.[SEQ]
,a.[ITEMNO]
,a.[LOCATION]
,a.[EXPARRIVAL]
,CONVERT(DATE, CONVERT(VARCHAR(10), a.[EXPARRIVAL])) AS ETA
,a.[OQORDERED]
,a.[OQRECEIVED]
,a.[OQOUTSTAND]
,b.[PONUMBER]
FROM
[mason01].[dbo].[Table1] a
INNER JOIN
[mason01].[dbo].[Table2] b ON a.SEQ = b.SEQ
问题是...我想为结果指定名称c。
( Select * From a inner join b on a.seq=b.seq ) as c
然后,当行具有相同的ETA,ITEMNO和位置时,我想合并行,同时汇总OQordered,OQreceived,OQoutstand。另外,PO#应该与','组合。
请参考以下链接:
sql combine rows with same date
这是我尝试过的查询:
SELECT
a.[SEQ]
,a.[ITEMNO]
,a.[LOCATION]
,a.[EXPAR
,CONVERT(DATE, CONVERT(VARCHAR(10), a.[EXPARRIVAL])) AS ETA
,SUM(a.[OQORDERED]) AS orderedQTY
,SUM(a.[OQRECEIVED]) AS receivedQTY
,SUM(a.[OQOUTSTAND]) AS OutstandingQTY
,b.[PONUMBER] = STUFF((SELECT
',' + c.[PONUMBER],
CONVERT(DATE, CONVERT(VARCHAR(10), c.[EXPARRIVAL])) AS ETA
FROM [mason01].[dbo].[Table1] c
INNER JOIN [mason01].[dbo].[Table2] d ON c.SEQ = d.SEQ
WHERE a.[ETA] = c.[ETA] AND a.[ITEMNO] = c.[ITEMNO]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM
[mason01].[dbo].[Table1] a
INNER JOIN
[mason01].[dbo].[Table2] b ON a.SEQ = b.SEQ
GROUP BY
[ETA], [ITEMNO], [LOCATION]
这会导致错误:
第15级州立1行9的消息102
'='附近的语法不正确。第15层状态1的第13行的消息156
关键字“ FOR”附近的语法不正确。
谢谢!