3

次のxmlを検討してください。

<Persons num="3">
  <Person age="5" />
  <Person age="19" />
</Persons>

このxmlをリレーショナルテーブルに抽出する必要があります。

Persons table (Age1 int, Age2 int, Age3 int , Age4 int)

構文解析は、次の制約を満たす必要があります。

  • 18歳以上のすべての人は、列番号が最小の列に割り当てられ、値は18である必要があります。
  • その人の年齢が与えられていない場合、それは18に等しい
  • 18歳未満のすべての人は従う必要があります
  • 4人未満の場合、提供されない人は年齢=-1でなければなりません

与えられた例では、3人がいて、そのうちの2人の年齢が提供されています:それぞれ5歳と19歳。テーブルPersonsの内容は次のとおりである必要があります。

18 18 5 -1

xpathを使用してこれを行うための最良の方法はありますか?

今まで私はxmlを解析して年齢を割り当てることができますが、明確ではないのは順序付けの方法です。

declare @XmlData xml = 
'<Persons num="3">
    <Person age="5" />
    <Person age="19" />
</Persons>'

declare @Persons table (Age1 int, Age2 int, Age3 int , Age4 int)
insert into @Persons (Age1, Age2, Age3, Age4)
select ISNULL(Age1, case when Num>= 1 then 18 else -1 end) Age1
    , ISNULL(Age2, case when Num>= 2 then 18 else -1 end) Age2
    , ISNULL(Age3, case when Num>= 3 then 18 else -1 end) Age3
    , ISNULL(Age4, case when Num>= 4 then 18 else -1 end) Age4
from (
    select Persons.Person.value('@num','smallint') as Num
          ,Persons.Person.value('Person[@age<18][1]/@age','smallint') as Age1
          ,Persons.Person.value('Person[@age<18][2]/@age','smallint') as Age2
          ,Persons.Person.value('Person[@age<18][3]/@age','smallint') as Age3
          ,Persons.Person.value('Person[@age<18][4]/@age','smallint') as Age4
    from @XmlData.nodes('/Persons') Persons(Person)
 ) Persons  

select *
from @Persons

結果は

5 18 18 -1
4

2 に答える 2

1

私は少し汚い解決策を見つけました:

select ISNULL(Age1, case when Num>= 1 then 18 else -1 end) Age1
    , ISNULL(Age2, case when Num>= 2 then 18 else -1 end) Age2
    , ISNULL(Age3, case when Num>= 3 then 18 else -1 end) Age3
    , ISNULL(Age4, case when Num>= 4 then 18 else -1 end) Age4
from (
    select Persons.Person.value('@num','smallint') as Num
          ,Persons.Person.value('xs:integer(fn:number(@num))+1','int') as Num1
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))][1]/@age','smallint') as Age1
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-1][1]/@age','smallint') as Age2
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-2][1]/@age','smallint') as Age3
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-3][1]/@age','smallint') as Age4 
    from @XmlData.nodes('/Persons') Persons(Person)
 ) Persons

解決策のアイデアは、最初に18以上の連絡先を抽出し、次に0 <18歳未満の連絡先を抽出し、最後に提供されていない連絡先を-1に設定することです。

UPD:ソリューションが正しい結果を提供したという事実にもかかわらず、そのコストは高くなります:推定実行計画で約1000

于 2011-05-20T13:39:40.613 に答える
0

別のソリューションでは、もう少しSQLコードが必要ですが、推定実行プランのコストはわずか80です。

問題ステートメントには1つの制約があります。Persons /@numはPersonタグの数と等しくなければなりません。

制限は次のとおりです。

  • 1部屋あたりの人数に限りがあります

これがSQLコードです:

--//initial xml data
declare @XmlData xml = 
'<Persons roomid="1" num="3">
    <Person age="19" />
    <Person age="10" /> 
    <Person age="5" />
</Persons>
<Persons roomid="4" num="4">
    <Person age="17" />
    <Person age="10" /> 
    <Person age="5" />
    <Person age="1" />
</Persons>'

--//shade xml into temporal table: rank is applied to an age in descreasing order
declare @tmp table (age int, roomid int, orderid int)
insert into @tmp(age,roomid,orderid)
select Persons.age
      ,Persons.roomid
      ,ROW_NUMBER () over (partition by Persons.roomid order by Persons.age desc)
from(
    select Ps.P.value('(@age)[1]','smallint') age       
          ,Ps.P.value('(../@roomid)[1]','smallint') roomid
    from @XmlData.nodes('/Persons/Person') Ps(P)
)Persons
order by Persons.roomid,Persons.age desc    

--//provide ordering for roomid: since roomid may be different (the only thing that is required that roomid is unique)
declare @roomidmapping table (roomid int, roomorderid int)
insert into @roomidmapping(roomid, roomorderid)
select roomid, ROW_NUMBER () over  (order by roomid asc)
from @tmp
group by roomid
declare @roomnumber int = @@ROWCOUNT
--//final result
;WITH ConsequtiveNums AS
(
    SELECT 1 AS Number
    UNION ALL
    SELECT Number+1
    FROM ConsequtiveNums
    WHERE Number<@roomnumber
)
select (select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 1 and M.roomorderid = CN.Number)
      ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 2 and M.roomorderid = CN.Number)
      ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 3 and M.roomorderid = CN.Number)
      ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 4 and M.roomorderid = CN.Number)
from ConsequtiveNums CN
于 2011-05-21T08:10:22.970 に答える