0

私は次のようなXMLデータを持っています

<Answers>
<AnswerSet>
    <Answer questionId="Name" IncludeInGroup="false">ds</Answer>
    <Answer questionId="Email" IncludeInGroup="false">sds</Answer>
    <Answer questionId="Password" IncludeInGroup="false">QqH4x4jhNCs=</Answer>
    <Answer questionId="GUID" IncludeInGroup="false">dsdsd</Answer>
    <Answer questionId="RegistrationIDInfo" IncludeInGroup="false">sdsd</Answer>
    <Answer questionId="Login" type="list" IncludeInGroup="false" value="1" color="blue">Enabled</Answer>
    <Answer questionId="FriendList" IncludeInGroup="false" RegionName="Friends" RepeatRegion="0">ds</Answer>   
    <Answer questionId="testDropdown" type="list" selectedValue="2" IncludeInGroup="false" TP="1" TP1="2">item2</Answer>
    <Answer questionId="testRadio" type="list" IncludeInGroup="false" value="3" color="222" test="123" a="1" a1="22">item234</Answer>
    <Answer questionId="TestCheckbox" type="list" value="Item1" RepeatRegion="0" IncludeInGroup="false">Item1</Answer>
    <Answer questionId="TestCheckbox" type="list" value="Item3" RepeatRegion="0" IncludeInGroup="false">Item3</Answer>
    <Answer questionId="TestCheckbox" type="list" value="Item1" RepeatRegion="1" IncludeInGroup="false">Item1</Answer>
    <Answer questionId="TestCheckbox" type="list" value="Item2" RepeatRegion="1" IncludeInGroup="false">Item2</Answer>
</AnswerSet>

groupbyrepeatregion属性を持つquestionid="TestCheckbox"のデータを取得する必要があります

出力

Item1、Item3 | Item1、Item2

以下のクエリで試しましたが、Item1 | Item3 | Item1|Item2が返されます。

@ContentをXMLとして宣言しますset@Content ='Above XML ...' @Fieldをvarchar(100)として宣言しますset @ Field ='TestCheckbox' SELECT ISNULL(STUFF((select'|' + T.answers.value('。 '、' VARCHAR(max)')FROM @ Content.nodes(' Answers / AnswerSet / Answer [@ questionId = sql:variable( "@ Field")]')AS T(answers)for xml path('')) 、1、1、'')、'')

上記の出力を取得するようにガイドしてください。

ありがとう。

4

1 に答える 1

1

これを試して

;with cte as
(
select 
t.Answers.value('@RepeatRegion', 'int') region,
T.answers.value('.', 'VARCHAR(max)') value
FROM @Content.nodes('Answers/AnswerSet/Answer[@questionId=sql:variable("@Field")]') AS T(answers) 
) 

select
    STUFF ((
        select '|' + v.results
        from
        (       
        SELECT distinct 
        top 100 percent
            region,
            stuff(
            (
                select cast(',' as varchar(max)) + c1.value
            from cte c1
            WHERE c1.region = cte.region
            for xml path('')
            ), 1, 1, '') AS results
        FROM
            cte
        order by cte.region

        ) v
        for xml path('')
    ),1,1,'')
于 2012-11-16T12:24:30.837 に答える