作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有嵌套选择的查询。我在 SQL Server 2017 中编写了此查询,现在我必须将其转换为在 SQL Server 2014 中使用。据我所知 STRING_AGG
必须转换为 FOR XML PATH
和 STUFF
,我尝试了某种类型的查询,但找不到答案。
SQL Server 2017 中的原始工作查询是:
SELECT filesanadrow, STRING_AGG(CONVERT(nvarchar(MAX),result.tarafhesabsharh), ' ; ') AS tarafhesabha
FROM (SELECT DISTINCT k.row AS filesanadrow, CONVERT(NVARCHAR(MAX), t.tarafhesabsharh) AS tarafhesabsharh
FROM filesanad AS k INNER JOIN tarafhesab AS t ON k.row = t.filesanadrow) AS result
GROUP BY filesanadrow
STRING_AGG(CONVERT(nvarchar(MAX),result.tarafhesabsharh), ' ; ')
必须转换为 FOR XML PATH 和 STUFF。
SELECT filesanadrow,
STUFF((SELECT distinct '' + t2.tarafhesabsharh
from result t2 where t1.filesanadrow = t2.filesanadrow
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,0,'') AS tarafhesabha
FROM (SELECT DISTINCT k.row AS filesanadrow, CONVERT(NVARCHAR(MAX), t.tarafhesabsharh) AS tarafhesabsharh
FROM filesanad AS k INNER JOIN tarafhesab AS t ON k.row = t.filesanadrow) AS result
GROUP BY filesanadrow
(Invalid object name 'result'. SQL2.sql 3 1 )
SELECT filesanadrow,
STUFF((SELECT '; ' + name FROM result FOR XML PATH('')), 1, 1, '') AS tarafhesabha
FROM (SELECT DISTINCT k.row AS filesanadrow, CONVERT(NVARCHAR(MAX), t.tarafhesabsharh) AS tarafhesabsharh
FROM filesanad AS k INNER JOIN tarafhesab AS t ON k.row = t.filesanadrow) AS result
GROUP BY filesanadrow
Invalid object name 'result'. SQL2.sql 2 1
SELECT filesanadrow,
STUFF((SELECT '; ' + tarafhesabsharh FROM tarafhesab FOR XML PATH('')), 1, 1, '') AS tarafhesabha
FROM (SELECT DISTINCT k.row AS filesanadrow, CONVERT(NVARCHAR(MAX), t.tarafhesabsharh) AS tarafhesabsharh
FROM filesanad AS k INNER JOIN tarafhesab AS t ON k.row = t.filesanadrow) AS result
GROUP BY filesanadrow
最佳答案
CROSS APPLY有点像 INNER JOIN
的嵌套版本.
而一个 OUTER APPLY
就像 LEFT JOIN
的嵌套版本.
所以试试这个例子:
create table filesanad
(
id int identity(1,1) primary key,
[row] int
);
insert into filesanad values
(1),(1),(2),(2),(3)
GO5 rows affected
create table tarafhesab
(
id int identity(1,1) primary key,
filesanadrow int,
tarafhesabsharh varchar(30)
);
insert into tarafhesab
(filesanadrow, tarafhesabsharh) values
(1,'foo'),(1,'bar'),
(2,'buz');
GO3 rows affected
--
-- CROSS APPLY on a FOR XML
--
SELECT f.filesanadrow, ca.tarafhesabha
FROM
(
SELECT [row] AS filesanadrow
FROM filesanad
GROUP BY [row]
) f
CROSS APPLY
(
SELECT STUFF(q.x.value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS tarafhesabha
FROM
(
SELECT DISTINCT '; ' + t.tarafhesabsharh
FROM tarafhesab AS t
WHERE t.filesanadrow = f.filesanadrow
FOR XML PATH(''), type
) q(x)
WHERE q.x IS NOT NULL
) ca;
GOfilesanadrow | tarafhesabha-----------: | :----------- 1 | bar; foo 2 | buz
--
-- OUTER APPLY on a FOR XML
--
SELECT f.filesanadrow, ca.tarafhesabha
FROM
(
SELECT [row] AS filesanadrow
FROM filesanad
GROUP BY [row]
) f
OUTER APPLY
(
SELECT STUFF(q.x.value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS tarafhesabha
FROM
(
SELECT DISTINCT '; ' + t.tarafhesabsharh
FROM tarafhesab AS t
WHERE t.filesanadrow = f.filesanadrow
FOR XML PATH(''), type
) q(x)
WHERE q.x IS NOT NULL
) ca;
GOfilesanadrow | tarafhesabha-----------: | :----------- 1 | bar; foo 2 | buz 3 | null
--
-- nested query with a FOR XML
--
SELECT
f.[row] AS filesanadrow,
(
SELECT STUFF(q.x.value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS tarafhesabha
FROM
(
SELECT DISTINCT '; ' + t.tarafhesabsharh
FROM tarafhesab AS t
WHERE t.filesanadrow = f.[row]
FOR XML PATH(''), type
) q(x)
WHERE q.x IS NOT NULL
) AS tarafhesabha
FROM filesanad f
GROUP BY f.[row]
GOfilesanadrow | tarafhesabha-----------: | :----------- 1 | bar; foo 2 | buz 3 | null
关于sql - 将 STRING_AGG 转换为 FOR XML PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320859/
我是一名优秀的程序员,十分优秀!