SQL XML创建xml树(父子)的显式问题

时间:2013-11-17 03:43:43

标签: sql sql-server xml sqlxml for-xml-explicit

我在sql server中面临一些xml显式问题,根据我在sql查询中指定的关系,它不输出xml。查询是在pubs数据库上完成的,虽然xml路径更容易使用,但我的教练需要在xml显式上完成。

    SELECT  1               AS Tag,
        NULL            AS Parent,
        NULL            AS [TitleTypes!1],
        NULL            AS [TitleType!2!Type],
        NULL            AS [TitleType!2!AveragePrice],
        NULL            AS [Title!3!title_id],
        NULL            AS [Title!3!price]

UNION ALL
SELECT  2,
        1,
        NULL            ,
        type            AS [TitleType!2!Type],
        AVG(price)      AS [TitleType!2!AveragePrice],
        NULL            AS [Title!3!title_id],
        NULL            AS [Title!3!price]
from titles 
GROUP BY type   
UNION ALL
SELECT  3,
        2,
        NULL            ,
        type            AS [TitleType!2!Type],
        NULL            AS [TitleType!2!AveragePrice],
        title_id        AS [Title!3!title_id],
        price           AS [Title!3!price]

from titles     

FOR XML EXPLICIT;

它产生的输出:

    <TitleTypes>
  <TitleType Type="business    " AveragePrice="13.7300" />
  <TitleType Type="mod_cook    " AveragePrice="11.4900" />
  <TitleType Type="popular_comp" AveragePrice="21.4750" />
  <TitleType Type="psychology  " AveragePrice="13.5040" />
  <TitleType Type="trad_cook   " AveragePrice="15.9633" />
  <TitleType Type="UNDECIDED   ">
    <Title title_id="BU1032" price="19.9900" />
    <Title title_id="BU1111" price="11.9500" />
    <Title title_id="BU2075" price="2.9900" />
    <Title title_id="BU7832" price="19.9900" />
    <Title title_id="MC2222" price="19.9900" />
    <Title title_id="MC3021" price="2.9900" />
    <Title title_id="MC3026" />
    <Title title_id="PC1035" price="22.9500" />
    <Title title_id="PC8888" price="20.0000" />
    <Title title_id="PC9999" />
    <Title title_id="PS1372" price="21.5900" />
    <Title title_id="PS2091" price="10.9500" />
    <Title title_id="PS2106" price="7.0000" />
    <Title title_id="PS3333" price="19.9900" />
    <Title title_id="PS7777" price="7.9900" />
    <Title title_id="TC3218" price="20.9500" />
    <Title title_id="TC4203" price="11.9500" />
    <Title title_id="TC7777" price="14.9900" />
  </TitleType>
</TitleTypes>

我想要的输出:

   <TitleTypes>
    <TitleType Type="business" AveragePrice="11.22">
        <Title title_id="BU1111" Price="11.34"/>
        <Title title_id="TC7777" Price="14.2"/>
    </TitleType>
    <TitleType Type="popular_comp" AveragePrice="13.99">
        <Title title_id="BU1111" Price="15.9"/>
        <Title title_id="TC7777" Price="16.22"/>
    </TitleType>
</TitleTypes>

2 个答案:

答案 0 :(得分:2)

通常,您根本不需要显式模式。你几乎可以生成每个xml 希望与for xml path

select
    t1.type as [@Type],
    avg(t1.price) as [@AveragePrice],
    (
        select
            t2.title_id as [@title_id],
            t2.price as [@price]
        from titles as t2
        where t2.type = t1.type
        for xml path('Title'), type
    )
from titles as t1
group by t1.type
for xml path('TitleType'), root('TitleTypes')

但是由于你的xml是以属性为中心的,所以你更容易使用for xml raw

select
    t1.type as [Type],
    avg(t1.price) as AveragePrice,
    (
        select
            t2.title_id
            t2.price
        from titles as t2
        where t2.type = t1.type
        for xml raw('Title'), type
    )
from titles as t1
group by t1.type
for xml raw('TitleType')

<强> sql fiddle demo

答案 1 :(得分:0)

这是适合我的解决方案。查看ORDER BY子句。如果您在ORDER BY子句中使用sql server management studio,它将显示可能的列名,您可以根据需要格式化输出xml。

SELECT  1               AS Tag,
            NULL            AS Parent,
            NULL            AS [TitleTypes!1],
            NULL            AS [TitleType!2!Type],
            NULL            AS [TitleType!2!AveragePrice],
            NULL            AS [Title!3!title_id],
            NULL            AS [Title!3!price]

    UNION ALL
    SELECT  2,
            1,
            NULL            ,
            type            AS [TitleType!2!Type],
            AVG(price)      AS [TitleType!2!AveragePrice],
            NULL            AS [Title!3!title_id],
            NULL            AS [Title!3!price]
    from titles 
    GROUP BY type   
    UNION ALL
    SELECT  3,
            2,
            NULL            ,
            type            AS [TitleType!2!Type],
            NULL            AS [TitleType!2!AveragePrice],
            title_id        AS [Title!3!title_id],
            price           AS [Title!3!price]

    from titles     

    ORDER BY [TitleTypes!1], [TitleType!2!Type], [Title!3!title_id] ,Tag

    FOR XML EXPLICIT;

P.S。 FOR XML EXPLICIT糟透了。但是,如果您正在开发/升级一个非常古老的系统,那么在更换新技术之前,您必须先了解它。