我有一个表,该表的行中有数据,数据需要按列汇总。当前,数据确实会进入列中,但是数据重复并且顺序错误。同样,当我输入多个服务器名称时,我将获得服务器1列和信息服务器2列将为空,服务器3列将包含数据。
我已经查看了关于stackoverflow,social.msdn.microsoft.com,sqlservercentral.com和sqlshack.com的答案
WITH
cte (Captivate_Id, Building_Name ,Server_Name,Server_Number)
as
(
SELECT b.[Captivate_Id], b.[Building_Name] as Building_Name
,(s.Server_Name) as Server_Name
,ROW_NUMBER() OVER(ORDER BY s.Server_Name DESC) AS Server_Number
FROM [CAP-CORPCRM02].[CAP_ProductionED].dbo.server s
join [CAP-CORPCRM02].[CAP_ProductionED].dbo.building b on
s.building_id = b.building_id
WHERE s.Server_Name IS NULL
UNION ALL
SELECT b.[Captivate_Id], b.[Building_Name] as Building_Name
,(s.Server_Name) as Server_Name
,ROW_NUMBER() OVER(ORDER BY s.Server_Name DESC) AS Server_Number
FROM [CAP-CORPCRM02].[CAP_ProductionED].dbo.server s
join [CAP-CORPCRM02].[CAP_ProductionED].dbo.building b on
s.building_id = b.building_id
AND b.Captivate_Id IN
(
'FFF1234'
)
)
SELECT Captivate_ID as ID,Building_Name as Building_Name,
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name
AND ct.Server_Number=1) Server1, -----, -- works but order wrong
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name
AND ct.Server_Number=2) Server2, -- works but order wrong
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name
AND ct.Server_Number=3) Server3
FROM cte
输出
ID Building_Name Server1 Server2 Server3
FFF1234 some name servername1 servername2 NULL
FFF1234 some name servername1 servername2 NULL
我希望输出为
ID Building_Name Server1 Server2 Server3
FFF1234 some name servername1 servername2 blank
答案 0 :(得分:1)
您对ROW_NUMBER()
的工作方式有误解。使用两个不同的ROW_NUMBER()
命令,您将拥有两个单独的行号序列。您可以通过如下更改CTE来解决问题:
WITH cte1 (Captivate_Id, Building_Name ,Server_Name)
AS (
SELECT b.[Captivate_Id], b.[Building_Name] as Building_Name
,'' as Server_Name -- If you want a blank servername, make it so
FROM [CAP-CORPCRM02].[CAP_ProductionED].dbo.server s
join [CAP-CORPCRM02].[CAP_ProductionED].dbo.building b
on s.building_id = b.building_id
WHERE s.Server_Name IS NULL
UNION ALL
SELECT b.[Captivate_Id], b.[Building_Name] as Building_Name
,(s.Server_Name) as Server_Name
FROM [CAP-CORPCRM02].[CAP_ProductionED].dbo.server s
join [CAP-CORPCRM02].[CAP_ProductionED].dbo.building b
on s.building_id = b.building_id
AND b.Captivate_Id IN (
'FFF1234'
)
),
cte (Captivate_Id, Building_Name ,Server_Name, Server_Number)
as (
SELECT Captivate_ID,
Building_Name,
Server_Name,
ROW_NUMBER() OVER(ORDER BY s.Server_Name DESC) AS Server_Number
FROM cte1
)
SELECT Captivate_ID as ID,Building_Name as Building_Name,
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name AND ct.Server_Number=1) Server1,-----, -- works but order wrong
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name AND ct.Server_Number=2) Server2, -- works but order wrong
(SELECT Server_Name FROM CTE ct WHERE ct.Building_Name=cte.Building_Name AND ct.Server_Number=3) Server3
FROM cte
SPOILER-原始错误代码
与 cte(Captivate_Id,Building_Name,Server_Name,Server_Number) 作为( 选择Captivate_ID, 建筑名称, 服务器名称, ROW_NUMBER()OVER(ORDER BY s.Server_Name DESC)AS Server_Number 来自( 选择b。[Captivate_Id],b。[Building_Name]作为Building_Name ,''作为Server_Name-如果您想要一个空白的服务器名,请这样 -,ROW_NUMBER()OVER(ORDER BY s.Server_Name DESC)AS Server_Number 来自[CAP-CORPCRM02]。[CAP_ProductionED] .dbo.server s 加入[CAP-CORPCRM02]。[CAP_ProductionED] .dbo.building b 在s.building_id = b.building_id上 s.Server_Name是NULL 全联盟 选择b。[Captivate_Id],b。[Building_Name]作为Building_Name ,(s.Server_Name)作为Server_Name -,ROW_NUMBER()OVER(ORDER BY s.Server_Name DESC)AS Server_Number 来自[CAP-CORPCRM02]。[CAP_ProductionED] .dbo.server s 加入[CAP-CORPCRM02]。[CAP_ProductionED] .dbo.building b 在s.building_id = b.building_id上 AND b.Captivate_Id IN( 'FFF1234' ) ) )