문제

SQL 쿼리에 지정된 관계에 따라 XML을 출력하지 않는 SQL Server에서 XML을 명시 적으로 직면 해 있습니다.쿼리는 Pubs 데이터베이스에서 수행되며 XML 경로는 XML 명시 적으로 Trainer가 필요로하는 것이 더 쉽습니다.

    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>
.

도움이 되었습니까?

해결책

일반적으로 명시적인 모드가 전혀 필요하지 않습니다.거의 모든 XML을 생성 할 수 있습니다 XML 경로의 경우 :

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은 속성 중심이므로 XML 원시 :

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 바이올린 데모

다른 팁

여기에 저에게 일한 해결책이 있습니다.주문 BY 절을보십시오.SQL Server Management Studio를 사용중인 경우 BY 절에서는 원하는대로 출력 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.XML 명시 적 짜증이납니다.그러나 실제로 실제로 오래된 시스템을 개발 / 업그레이드하는 경우 새로운 기술로 대체하기 전에 알아야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top