将单行 xml 路径结果拆分为多行
Split single row xml path result into multiple rows
我在这里寻求一些指导。我有一个 table,其中包含我需要的所有数据,它们都混合在一个列(属性)中,如下所示:
Device Index Attribute index Attributes
5 59 WS020121
5 83 9C-B6-54-A0-41-40
5 90 GROUP\darkwahm
6 59 WS020122
6 83 9D-B8-54-A0-50-40
6 90 GROUP\darkperm
我想做的是将数据分成多列,这样它就会显示为:
Device Mac Address User
WS020121 9C-B6-54-A0-41-40 GROUP\darkwahm
WS020122 9D-B8-54-A0-50-40 GROUP\darkperm
经过一些研究,我被建议使用 XML 路径来获得这些结果,所以我创建了一个查询:
SELECT cast (av1.attributeValue as varchar(50)) + ','
--cast( av1.attributeValue as varchar(50)) + ','
FROM [dbo].[DeviceAttributes] Av1
WHERE av1.AttributeIndex=59 or av1.AttributeIndex=83 or av1.AttributeIndex=90
FOR XML PATH ('')
这在我的所有数据都在一起的意义上是有效的,但它像这样排在一行中:
WS022743,B0-5A-DA-B4-51-01;60-6D-C7-34-86-05,GROUP\darkwahm,WS022871,D0-27- 88-92-9B-8A,GROUP\securitypc,ABSE-PEARSOND,00-05-9A-3C-78-00;68-F7-28-92-0E-7A,GROUP\SlavinM
只是想知道我需要如何调整查询以使其像我上面提到的那样拆分成多个列和行。
因此,根据您的查询,数字:59, 83 and 90
是这些行的常量,如果是,您可以试试这个:
SELECT t1.Attributes as device, t2.Attributes as Mac_Address , t3.Attributes as "user" from
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index= 59) t1
INNER JOIN
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index = 83) t2
ON t1.Device_Index = t2.Device_Index
INNER JOIN
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index = 90) t3
ON t1.Device_Index = t3.Device_Index
我在这里寻求一些指导。我有一个 table,其中包含我需要的所有数据,它们都混合在一个列(属性)中,如下所示:
Device Index Attribute index Attributes
5 59 WS020121
5 83 9C-B6-54-A0-41-40
5 90 GROUP\darkwahm
6 59 WS020122
6 83 9D-B8-54-A0-50-40
6 90 GROUP\darkperm
我想做的是将数据分成多列,这样它就会显示为:
Device Mac Address User
WS020121 9C-B6-54-A0-41-40 GROUP\darkwahm
WS020122 9D-B8-54-A0-50-40 GROUP\darkperm
经过一些研究,我被建议使用 XML 路径来获得这些结果,所以我创建了一个查询:
SELECT cast (av1.attributeValue as varchar(50)) + ','
--cast( av1.attributeValue as varchar(50)) + ','
FROM [dbo].[DeviceAttributes] Av1
WHERE av1.AttributeIndex=59 or av1.AttributeIndex=83 or av1.AttributeIndex=90
FOR XML PATH ('')
这在我的所有数据都在一起的意义上是有效的,但它像这样排在一行中:
WS022743,B0-5A-DA-B4-51-01;60-6D-C7-34-86-05,GROUP\darkwahm,WS022871,D0-27- 88-92-9B-8A,GROUP\securitypc,ABSE-PEARSOND,00-05-9A-3C-78-00;68-F7-28-92-0E-7A,GROUP\SlavinM
只是想知道我需要如何调整查询以使其像我上面提到的那样拆分成多个列和行。
因此,根据您的查询,数字:59, 83 and 90
是这些行的常量,如果是,您可以试试这个:
SELECT t1.Attributes as device, t2.Attributes as Mac_Address , t3.Attributes as "user" from
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index= 59) t1
INNER JOIN
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index = 83) t2
ON t1.Device_Index = t2.Device_Index
INNER JOIN
(SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index = 90) t3
ON t1.Device_Index = t3.Device_Index