将单行XML路径结果分为多行



我在这里进行了一些指导。我有一个表格,我需要将所有数据混合在一起的所有数据(属性),例如:

Device Index     Attribute index     Attributes
      5               59              WS020121
      5               83              9C-B6-54-A0-41-40
      5               90              GROUPdarkwahm
      6               59              WS020122
      6               83              9D-B8-54-A0-50-40
      6               90              GROUPdarkperm

我要做的是将数据分为多列,以便显示为:

   Device              Mac Address              User
   WS020121            9C-B6-54-A0-41-40        GROUPdarkwahm 
   WS020122            9D-B8-54-A0-50-40        GROUPdarkperm            

经过一项研究,建议我使用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,GROUPdarkwahm,WS022871,D0-27-  88-92-9B-8A,GROUPsecuritypc,ABSE-PEARSOND,00-05-9A-3C-78-00;68-F7-28-92-0E-7A,GROUPSlavinM

只是想知道我需要如何调整查询,以使其像我上面提到的那样将其分成多个列和行。

因此,根据您的查询,数字: 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

最新更新