FORXML SQL Group By Element

时间:2017-04-27 20:23:31

标签: sql sql-server sql-server-2012 for-xml

我试图在一个节点下将一些元素组合在一起。这是我目前的SQL;

declare @xml xml

set @xml = (
select (
    select
        'DERIVED' '@type', 
        m.NuixDerivedFieldName '@name', (
        SELECT
            NuixFieldType as 'metadata/@type',
            NuixFieldName as 'metadata/@name'
        from eddsdbo.MetadataMapping m1
        where m1.NuixDerivedFieldName = m.NuixDerivedFieldName
        for xml path ('first-non-blank'), type
    )
from (select distinct NuixDerivedFieldName from eddsdbo.MetadataMapping) m
for xml path ('metadata'))
)

;WITH XMLNAMESPACES(DEFAULT 'http://nuix.com/fbi/metadata-profile')
select @xml for XML PATH ('metadata-list'), ROOT ('metadata-profile')

这给了我以下输出;

<metadata-profile xmlns="http://nuix.com/fbi/metadata-profile">
  <metadata-list>
      <metadata type="DERIVED" name="Barcode" xmlns="">
          <first-non-blank>
              <metadata type="CUSTOM" name="Barcode" />
          </first-non-blank>
          <first-non-blank>
              <metadata type="EVIDENCE" name="Barcode" />
          </first-non-blank>
      </metadata>

我想将具有相同名称的元素组合在一起&#39; <first-non-blank>元素下的元数据元素的属性。

所需的输出应为;

    <metadata-profile xmlns="http://nuix.com/fbi/metadata-profile">
      <metadata-list>
          <metadata type="DERIVED" name="Barcode" xmlns="">
              <first-non-blank>
                  <metadata type="CUSTOM" name="Barcode" />
                  <metadata type="EVIDENCE" name="Barcode" />
              </first-non-blank>
          </metadata>
...

我的数据库看起来像这样;

NuixFieldName                  NuixFieldType                  NuixDerivedFieldName
------------------------------ ------------------------------ ------------------------------
_EmailEntryID                  PROPERTY                       EmailEntryID
Audited                        Audited                        Audited
Author                         PROPERTY                       Author
Barcode                        CUSTOM                         Barcode
Barcode                        EVIDENCE                       Barcode

我还想从xlmns元素中删除metadata命名空间标识符。

提前致谢!

1 个答案:

答案 0 :(得分:0)

你可以试试这个

DECLARE @SampleData AS TABLE
(
   NuixFieldName varchar(20),
   NuixFieldType varchar(20),
   NuixDerivedFieldName varchar(20)
)

INSERT INTO @SampleData
VALUES
  ('_EmailEntryID','PROPERTY','EmailEntryID'),
  ('Audited',      'Audited ','Audited'),
  ('Author ',      'PROPERTY','Author '),
  ('Barcode',      'CUSTOM  ','Barcode'),
  ('Barcode',      'EVIDENCE','Barcode')

DECLARE @xml XML

  SET @xml = (
  SELECT 
     -- sd.NuixDerivedFieldName AS [@name],
      'DERIVED' AS [@type],
      sd.NuixDerivedFieldName AS [@name],
      (
        SELECT 
         sd2.NuixFieldType as '@type',
         sd2.NuixFieldName as '@name' 
        FROM @SampleData sd2 WHERE sd2.NuixDerivedFieldName = sd.NuixDerivedFieldName
        FOR XML PATH ('metadata'),ROOT('first-non-blank'), TYPE
      )
  FROM (select DISTINCT sd.NuixDerivedFieldName from @SampleData sd ) sd
  FOR XML PATH('metadata'), ROOT('metadata-list'),TYPE
)  

;WITH XMLNAMESPACES(DEFAULT 'http://nuix.com/fbi/metadata-profile')
SELECT @xml FOR XML PATH (''),ROOT('metadata-profile')

返回:

    <metadata-profile xmlns="http://nuix.com/fbi/metadata-profile">
 <metadata-list>
   <metadata type="DERIVED" name="Audited">
    <first-non-blank>
      <metadata type="Audited " name="Audited" />
    </first-non-blank>
   </metadata>
   <metadata type="DERIVED" name="Author ">
    <first-non-blank>
      <metadata type="PROPERTY" name="Author " />
    </first-non-blank>
   </metadata>
   <metadata type="DERIVED" name="Barcode">
    <first-non-blank>
      <metadata type="CUSTOM  " name="Barcode" />
      <metadata type="EVIDENCE" name="Barcode" />
    </first-non-blank>
   </metadata>
   <metadata type="DERIVED" name="EmailEntryID">
    <first-non-blank>
      <metadata type="PROPERTY" name="_EmailEntryID" />
    </first-non-blank>
   </metadata>
 </metadata-list>
</metadata-profile>