如果在做东西时重复相同的功能,如何只显示一次文本单元?
How to display Text Unit only one time if it repeated for same Feature when do stuff?
我使用 SQL Server 2012 并遇到一个问题:我无法仅显示一次文本单元,因为它重复了使用 Stuff 的功能。
我需要的是当 Text Unit 为相同的功能重复时,不需要重复它 - 只显示一次。
就我而言,我遇到的问题是,当相同的文本单元用于相同的功能时,我无法阻止重复的文本单元。
电压 |电压 | Voltage ONLY 一个 电压显示 .
CREATE TABLE #FinalTable
(
PartID INT,
DKFeatureName NVARCHAR(100),
TextUnit NVARCHAR(100),
StatusId INT
)
INSERT INTO #FinalTable (PartID, DKFeatureName, TextUnit, StatusId)
VALUES
(1211, 'PowerSupply', 'Voltage', 3),
(1211, 'PowerSupply', 'Voltage', 3),
(1211, 'PowerSupply', 'Voltage', 3)
SELECT
PartID, DKFeatureName,
COUNT(PartID) AS CountParts,
TextUnit = STUFF ((SELECT ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID
AND a.DKFeatureName = b.DKFeatureName
AND StatusId = 3
FOR XML PATH('')), 1, 2, ' ')
INTO
#getUnitsSticky
FROM
#FinalTable a
GROUP BY
PartID, DKFeatureName
HAVING
(COUNT(PartID) > 1)
SELECT *
FROM #getUnitsSticky
预期结果是:
Voltage
不正确的结果或我不需要的结果如下:
Voltage|Voltage|Voltage
要将其纳入完整答案 - 这应该是您的 SQL(稍微缩短并删除了最后一个临时 table):
SELECT
PartID, DKFeatureName,
COUNT(PartID) AS CountParts,
TextUnit = STUFF ((SELECT distinct ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID
AND a.DKFeatureName = b.DKFeatureName
AND StatusId = 3
FOR XML PATH('')), 1, 2, ' ')
FROM #FinalTable a
GROUP BY PartID, DKFeatureName
HAVING (COUNT(PartID) > 1)
TomC的回答基本正确。但是,当使用此方法与 SQL 服务器时,通常更有效的是在子查询中获取行,然后在外部查询中使用 stuff()
。这样,每行中的值只处理一次。
所以:
SELECT PartID, DKFeatureName, CountParts,
STUFF( (SELECT ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID AND
b.DKFeatureName = a.DKFeatureName AND
StatusId = 3
FOR XML PATH('')
), 1, 3, ' ') as TextUnit
INTO #getUnitsSticky
FROM (SELECT PartID, DKFeatureName, COUNT(*) as CountParts
FROM #FinalTable a
GROUP BY PartID, DKFeatureName
HAVING COUNT(*) > 1
) a;
这还会从串联结果中删除前导 space。
我使用 SQL Server 2012 并遇到一个问题:我无法仅显示一次文本单元,因为它重复了使用 Stuff 的功能。
我需要的是当 Text Unit 为相同的功能重复时,不需要重复它 - 只显示一次。
就我而言,我遇到的问题是,当相同的文本单元用于相同的功能时,我无法阻止重复的文本单元。
电压 |电压 | Voltage ONLY 一个 电压显示 .
CREATE TABLE #FinalTable
(
PartID INT,
DKFeatureName NVARCHAR(100),
TextUnit NVARCHAR(100),
StatusId INT
)
INSERT INTO #FinalTable (PartID, DKFeatureName, TextUnit, StatusId)
VALUES
(1211, 'PowerSupply', 'Voltage', 3),
(1211, 'PowerSupply', 'Voltage', 3),
(1211, 'PowerSupply', 'Voltage', 3)
SELECT
PartID, DKFeatureName,
COUNT(PartID) AS CountParts,
TextUnit = STUFF ((SELECT ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID
AND a.DKFeatureName = b.DKFeatureName
AND StatusId = 3
FOR XML PATH('')), 1, 2, ' ')
INTO
#getUnitsSticky
FROM
#FinalTable a
GROUP BY
PartID, DKFeatureName
HAVING
(COUNT(PartID) > 1)
SELECT *
FROM #getUnitsSticky
预期结果是:
Voltage
不正确的结果或我不需要的结果如下:
Voltage|Voltage|Voltage
要将其纳入完整答案 - 这应该是您的 SQL(稍微缩短并删除了最后一个临时 table):
SELECT
PartID, DKFeatureName,
COUNT(PartID) AS CountParts,
TextUnit = STUFF ((SELECT distinct ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID
AND a.DKFeatureName = b.DKFeatureName
AND StatusId = 3
FOR XML PATH('')), 1, 2, ' ')
FROM #FinalTable a
GROUP BY PartID, DKFeatureName
HAVING (COUNT(PartID) > 1)
TomC的回答基本正确。但是,当使用此方法与 SQL 服务器时,通常更有效的是在子查询中获取行,然后在外部查询中使用 stuff()
。这样,每行中的值只处理一次。
所以:
SELECT PartID, DKFeatureName, CountParts,
STUFF( (SELECT ' | ' + TextUnit
FROM #FinalTable b
WHERE b.PartID = a.PartID AND
b.DKFeatureName = a.DKFeatureName AND
StatusId = 3
FOR XML PATH('')
), 1, 3, ' ') as TextUnit
INTO #getUnitsSticky
FROM (SELECT PartID, DKFeatureName, COUNT(*) as CountParts
FROM #FinalTable a
GROUP BY PartID, DKFeatureName
HAVING COUNT(*) > 1
) a;
这还会从串联结果中删除前导 space。