如何在 STUFF 函数中使用 order by 并排除特定值
How to use order by within the STUFF function and exclude specific values
如何在 STUFF 中使用 order by 并排除特定值?这是我的查询。
SELECT ID, STUFF((
SELECT ';' + t1.CTY
FROM country_tbl t1
WHERE t2.ID = t1.ID FOR XML PATH('')),1,1,'') AS aggregation
FROM master_tbl t2
WHERE t2.ID IN ('123456','123457')
GROUP BY ID;
ORDER BY T2.ID
t1
ID Country
123456 England
123457 Canada
t2
ID CTY
123456 England
123456 Japan
123456 France
123456 Canada
123457 England
123457 Japan
123457 France
Result
ID CTY
123456 Canada;France;Japan Except England (CTY Sorted A-Z)
123457 England;France;Japan No Exception (CTY Sorted A-Z)
如果我没理解错的话,您想以升序列出 t2 中的城市,除了那些在 t1 中出现的具有相同 ID 的城市。
如果是这样的话,那么你就很接近了,但还差得远。
首先,创建并填充示例表(请在您以后的问题中为我们省去这一步):
DECLARE @t1 AS TABLE (
[ID] int,
[Country] varchar(7)
);
INSERT INTO @t1 ([ID], [Country]) VALUES
(123456, 'England'),
(123457, 'Canada');
DECLARE @t2 AS TABLE (
[ID] int,
[CTY] varchar(7)
);
INSERT INTO @t2 ([ID], [CTY]) VALUES
(123456, 'England'),
(123456, 'Japan'),
(123456, 'France'),
(123456, 'Canada'),
(123457, 'England'),
(123457, 'Japan'),
(123457, 'France');
查询:
SELECT DISTINCT Id,
STUFF((
SELECT ';' + t2.CTY
FROM @t2 As t2
LEFT JOIN @t1 As t1
ON t2.Id = t1.Id
AND t2.CTY = t1.Country
WHERE t2.ID = t0.ID
AND t1.Id IS NULL
ORDER BY t2.CTY
FOR XML PATH('')),1,1,'') AS aggregation
FROM @t2 As t0
结果:
Id aggregation
123456 Canada;France;Japan
123457 England;France;Japan
您可以在子查询中使用 ORDER BY
和 WHERE
子句。像这样:
SELECT ID,
STUFF( (SELECT ';' + t1.CTY
FROM country_tbl t1
WHERE t2.ID = t1.ID AND
NOT (t1.id = '123456' AND t1.CTY = 'England')
ORDER BY t1.CTY
FOR XML PATH('')
), 1, 1, ''
) AS aggregation
FROM (SELECT DISTINCT id
FROM master_tbl t2
WHERE t2.ID IN ('123456','123457')
) t2
ORDER BY T2.ID;
我还将外部查询中的 GROUP BY
更改为 SELECT DISTINCT
以提高性能。
如何在 STUFF 中使用 order by 并排除特定值?这是我的查询。
SELECT ID, STUFF((
SELECT ';' + t1.CTY
FROM country_tbl t1
WHERE t2.ID = t1.ID FOR XML PATH('')),1,1,'') AS aggregation
FROM master_tbl t2
WHERE t2.ID IN ('123456','123457')
GROUP BY ID;
ORDER BY T2.ID
t1
ID Country
123456 England
123457 Canada
t2
ID CTY
123456 England
123456 Japan
123456 France
123456 Canada
123457 England
123457 Japan
123457 France
Result
ID CTY
123456 Canada;France;Japan Except England (CTY Sorted A-Z)
123457 England;France;Japan No Exception (CTY Sorted A-Z)
如果我没理解错的话,您想以升序列出 t2 中的城市,除了那些在 t1 中出现的具有相同 ID 的城市。 如果是这样的话,那么你就很接近了,但还差得远。
首先,创建并填充示例表(请在您以后的问题中为我们省去这一步):
DECLARE @t1 AS TABLE (
[ID] int,
[Country] varchar(7)
);
INSERT INTO @t1 ([ID], [Country]) VALUES
(123456, 'England'),
(123457, 'Canada');
DECLARE @t2 AS TABLE (
[ID] int,
[CTY] varchar(7)
);
INSERT INTO @t2 ([ID], [CTY]) VALUES
(123456, 'England'),
(123456, 'Japan'),
(123456, 'France'),
(123456, 'Canada'),
(123457, 'England'),
(123457, 'Japan'),
(123457, 'France');
查询:
SELECT DISTINCT Id,
STUFF((
SELECT ';' + t2.CTY
FROM @t2 As t2
LEFT JOIN @t1 As t1
ON t2.Id = t1.Id
AND t2.CTY = t1.Country
WHERE t2.ID = t0.ID
AND t1.Id IS NULL
ORDER BY t2.CTY
FOR XML PATH('')),1,1,'') AS aggregation
FROM @t2 As t0
结果:
Id aggregation
123456 Canada;France;Japan
123457 England;France;Japan
您可以在子查询中使用 ORDER BY
和 WHERE
子句。像这样:
SELECT ID,
STUFF( (SELECT ';' + t1.CTY
FROM country_tbl t1
WHERE t2.ID = t1.ID AND
NOT (t1.id = '123456' AND t1.CTY = 'England')
ORDER BY t1.CTY
FOR XML PATH('')
), 1, 1, ''
) AS aggregation
FROM (SELECT DISTINCT id
FROM master_tbl t2
WHERE t2.ID IN ('123456','123457')
) t2
ORDER BY T2.ID;
我还将外部查询中的 GROUP BY
更改为 SELECT DISTINCT
以提高性能。