Converting Every Child Tags in to a Single Column

2020-04-19 05:56发布

问题:

declare @xml xml='<plan>
    <prescriptions id="1">
        <prescription>
            <name>ABC</name>
            <frequency>Daily</frequency>
            <dailyfrequency>
               <morning>2</morning>
               <afternoon></afternoon>
               <night>1</night>
            </dailyfrequency>
            <dayfrequency></dayfrequency>
        </prescription>
        <prescription>
            <name>EDF</name>
            <frequency>Daily</frequency>
            <dailyfrequency>
              <morning>5</morning>
              <afternoon>5</afternoon>
              <night>1</night>
           </dailyfrequency>
           <dayfrequency></dayfrequency>
        </prescription>
        <prescription>
            <name>YTER</name>
            <frequency>Weekly</frequency>
            <dailyfrequency>
              <morning>5</morning>
              <afternoon>5</afternoon>
              <night>1</night>
            </dailyfrequency>
            <dayfrequency>Monday,Tuesday,Wednesday</dayfrequency>
        </prescription>
    </prescriptions>
    <prescriptions id="2">
        <prescription>
            <name>YTRE</name>
            <frequency>Daily</frequency>
            <dailyfrequency>
               <morning>2</morning>
               <afternoon></afternoon>
               <night>1</night>
            </dailyfrequency>
            <dayfrequency></dayfrequency>
          </prescription>
    </prescriptions>
</plan>'

We can query like below to segregate each elements with multiple delimiter to be resulted in same column.

SELECT STUFF(
(
    SELECT '!' + STUFF(p.query(N'for $n in .//*[local-name()!="dailyfrequency"]
                           return <a>{concat("$",($n/text())[1])}</a>'
                        ).value(N'.',N'nvarchar(max)'),1,1,'')
    FROM @xml.nodes(N'/plan/prescriptions/prescription') AS A(p)
    FOR XML PATH(''),TYPE).value(N'.',N'nvarchar(max)'),1,1,'')

Result:

ABC$Daily$2$$1$!EDF$Daily$5$5$1$!YTER$Weekly$5$5$1$Monday,Tuesday,Wednesday!YTRE$Daily$2$$1$

But the problem here is this will combine all the tags under a single column.When we look the xml it has 2 major division Prescription id=1 and prescritpion id=2.So our final result will be like this

Result:

ABC$Daily$2$$1$!EDF$Daily$5$5$1$!YTER$Weekly$5$5$1$Monday,Tuesday,Wednesday
YTRE$Daily$2$$1$

I think we have to Shred the Xml for each <Prescriptions> then have to calculate this

Could someone please here to solve this

Thanks in advance,Jayendran

回答1:

Change your query like this:

SELECT STUFF(
(
    SELECT '!' + STUFF(p.query(N'for $n in .//*[local-name()!="dailyfrequency"]
                           return <a>{concat("$",($n/text())[1])}</a>'
                        ).value(N'.',N'nvarchar(max)'),1,1,'')
    FROM p.nodes(N'prescription') AS A(p)
    FOR XML PATH(''),TYPE).value(N'.',N'nvarchar(max)'),1,1,'')
FROM @xml.nodes(N'/plan/prescriptions') AS A(p);

First we create a derived table for the different prescriptions, Secondly we use the former code for each prescription separately.