将每个子标记转换为具有多个分隔符的单个列(SQL Server)

时间:2017-06-08 11:35:10

标签: sql-server xml tsql xpath xquery

这是我的XML

<plan>
  <prescription>
    <name>ABC</name>
    <frequency>Daily</frequency>
    <dailyfrequency>
       <morning>2</morning>
       <afternoon></afternoon>
       <night>1</night>
    </dailyfrequency>
  </prescription>
  <prescription>
    <name>EDF</name>
    <frequency>Daily</frequency>
    <dailyfrequency>
      <morning>5</morning>
      <afternoon>5</afternoon>
      <evening>4</evening>
      <night>1</night>
   </dailyfrequency>
   <dayfrequency></dayfrequency>
  </prescription>
  <prescription>
    <name>YTER</name>
    <frequency>Weekly</frequency>
    <dailyfrequency>
      <morning>5</morning>
      <afternoon>5</afternoon>
      <evening>4</evening>
      <night>1</night>
    </dailyfrequency>
    <dayfrequency>Monday,Tuesday,Wednesday</dayfrequency>
  </prescription>
</plan>

我使用Cross Join

查询如下所示
PLAN=    STUFF(XMLData.query('for $a in 
    (*:ClinicalDocuments/Visits/Visit/prescriptions/prescription) 

return <a>{concat("$", $a)}</a>').value('.', 'NVARCHAR(MAX)'), 1, 1, '') 

但我的结果会显示如下

**PLAN**
ABCDaily21$EDFDaily5541$YTERWeekly5541Monday,Tuesday,Wednesday

但我真的想要像下面这样(假设 $ - 处方中每个标签的列分隔符 ! - Column Delimiter for每个处方本身

**PLAN**
ABC$Daily$2$0$0$1$0!EDF$Daily$5$5$4$1$0!YTER$Weekly$5$5$4$1$Monday,Tuesday,Wednesday

请有人在这帮助我!

(注意:某些子标签可能不存在,即表示它没有值)

非常感谢, Jayendran

1 个答案:

答案 0 :(得分:0)

以下可能是您需要的,但我真的不知道,您希望如何处理元素不存在。在您的示例中,您放置了0,但这不是缺失值,而是专用默认值。这需要一些关于底层数据类型的知识......

试试这个:

DECLARE @xml XML=
N'<plan>
  <prescription>
    <name>ABC</name>
    <frequency>Daily</frequency>
    <dailyfrequency>
       <morning>2</morning>
       <afternoon></afternoon>
       <night>1</night>
    </dailyfrequency>
  </prescription>
  <prescription>
    <name>EDF</name>
    <frequency>Daily</frequency>
    <dailyfrequency>
      <morning>5</morning>
      <afternoon>5</afternoon>
      <evening>4</evening>
      <night>1</night>
   </dailyfrequency>
   <dayfrequency></dayfrequency>
  </prescription>
  <prescription>
    <name>YTER</name>
    <frequency>Weekly</frequency>
    <dailyfrequency>
      <morning>5</morning>
      <afternoon>5</afternoon>
      <evening>4</evening>
      <night>1</night>
    </dailyfrequency>
    <dayfrequency>Monday,Tuesday,Wednesday</dayfrequency>
  </prescription>
</plan>';

更新:添加谓词以排除<dailyfrequence>

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/prescription') AS A(p)
    FOR XML PATH(''),TYPE).value(N'.',N'nvarchar(max)'),1,1,'')

结果

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

缺少的值由无值表示。