我正在尝试使用Oracle数据库(SQLDEVELOPER)中的SQL查询将xml多项目序列数据转换为包含列和行的普通表。我能够转换单例序列字段,但我遇到了多项序列的问题。 这是xml数据的示例:
<row id='1129040398101-20150630' xml:space='preserve'><c1>20150601</c1><c2>20150630</c2><c3>20150601</c3><c3 m='2'>20150601</c3><c3 m='3'>20150623</c3><c3 m='4'>20150624</c3><c3 m='5'>20150625</c3><c3 m='6'>20150626</c3><c3 m='7'>20150629</c3><c3 m='8'>20150630</c3><c4>22</c4><c4 m='2'>22</c4><c4 m='3'>1</c4><c4 m='4'>1</c4><c4 m='5'>1</c4><c4 m='6'>3</c4><c4 m='7'>1</c4><c4 m='8'>1</c4><c5>200000.00</c5><c5 m='2'>5221.06</c5><c5 m='3'>220451.26</c5><c5 m='4'>231832.26</c5><c5 m='5'>254182.26</c5><c5 m='6'>260682.26</c5><c5 m='7'>267642.26</c5><c5 m='8'>282644.26</c5><c6>32</c6><c6 m='2'>35</c6><c6 m='3'>32</c6><c6 m='4'>32</c6><c6 m='5'>32</c6><c6 m='6'>32</c6><c6 m='7'>32</c6><c6 m='8'>32</c6><c9>51000</c9></row>
答案 0 :(得分:0)
我这样做了: 1)按照下一格式将xml文件上传到数据库 data data_value
其中数据例如是c1,数据值是20150601。 你可以制作一个正则表达式来匹配&lt;&gt;中的所有内容作为数据和内部的一切&gt; &LT; / as data_value。
在表格中以此格式存储数据后,您只需转动表格即可。 有关透视sql表的更多信息,请查看此处 Convert Rows to columns using 'Pivot' in SQL Server
希望有所帮助
答案 1 :(得分:0)
您没有指定您的RDBMS。这非常重要,因为XML支持是高度特定于供应商的。以下是针对SQL-Server的。它应该 - 至少 - 给你一个提示如何继续:
DECLARE @xml XML=
N'<row id="1129040398101-20150630" xml:space="preserve"><c1>20150601</c1><c2>20150630</c2><c3>20150601</c3><c3 m="2">20150601</c3><c3 m="3">20150623</c3><c3 m="4">20150624</c3><c3 m="5">20150625</c3><c3 m="6">20150626</c3><c3 m="7">20150629</c3><c3 m="8">20150630</c3><c4>22</c4><c4 m="2">22</c4><c4 m="3">1</c4><c4 m="4">1</c4><c4 m="5">1</c4><c4 m="6">3</c4><c4 m="7">1</c4><c4 m="8">1</c4><c5>200000.00</c5><c5 m="2">5221.06</c5><c5 m="3">220451.26</c5><c5 m="4">231832.26</c5><c5 m="5">254182.26</c5><c5 m="6">260682.26</c5><c5 m="7">267642.26</c5><c5 m="8">282644.26</c5><c6>32</c6><c6 m="2">35</c6><c6 m="3">32</c6><c6 m="4">32</c6><c6 m="5">32</c6><c6 m="6">32</c6><c6 m="7">32</c6><c6 m="8">32</c6><c9>51000</c9></row>';
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS StraightOrder
,ROW_NUMBER() OVER(PARTITION BY nd.value(N'local-name(.)',N'varchar(100)') ORDER BY (SELECT NULL)) AS GroupedOrder
,nd.value(N'local-name(.)',N'nvarchar(100)') NodeName
,nd.value(N'@m','int') AS AttributeM
,nd.value(N'text()[1]','nvarchar(100)') AS NodeValue
FROM @xml.nodes(N'/row/*') AS A(nd);
结果
+---------------+--------------+----------+------------+-----------+
| StraightOrder | GroupedOrder | NodeName | AttributeM | NodeValue |
+---------------+--------------+----------+------------+-----------+
| 1 | 1 | c1 | NULL | 20150601 |
+---------------+--------------+----------+------------+-----------+
| 2 | 1 | c2 | NULL | 20150630 |
+---------------+--------------+----------+------------+-----------+
| 3 | 1 | c3 | NULL | 20150601 |
+---------------+--------------+----------+------------+-----------+
| 4 | 2 | c3 | 2 | 20150601 |
+---------------+--------------+----------+------------+-----------+
| 5 | 3 | c3 | 3 | 20150623 |
+---------------+--------------+----------+------------+-----------+
| 6 | 4 | c3 | 4 | 20150624 |
+---------------+--------------+----------+------------+-----------+
| 7 | 5 | c3 | 5 | 20150625 |
+---------------+--------------+----------+------------+-----------+
| 8 | 6 | c3 | 6 | 20150626 |
+---------------+--------------+----------+------------+-----------+
| 9 | 7 | c3 | 7 | 20150629 |
+---------------+--------------+----------+------------+-----------+
| 10 | 8 | c3 | 8 | 20150630 |
+---------------+--------------+----------+------------+-----------+
| 11 | 1 | c4 | NULL | 22 |
+---------------+--------------+----------+------------+-----------+
| 12 | 2 | c4 | 2 | 22 |
+---------------+--------------+----------+------------+-----------+
| 13 | 3 | c4 | 3 | 1 |
+---------------+--------------+----------+------------+-----------+
| 14 | 4 | c4 | 4 | 1 |
+---------------+--------------+----------+------------+-----------+
| 15 | 5 | c4 | 5 | 1 |
+---------------+--------------+----------+------------+-----------+
| 16 | 6 | c4 | 6 | 3 |
+---------------+--------------+----------+------------+-----------+
| 17 | 7 | c4 | 7 | 1 |
+---------------+--------------+----------+------------+-----------+
| 18 | 8 | c4 | 8 | 1 |
+---------------+--------------+----------+------------+-----------+
| 19 | 1 | c5 | NULL | 200000.00 |
+---------------+--------------+----------+------------+-----------+
| 20 | 2 | c5 | 2 | 5221.06 |
+---------------+--------------+----------+------------+-----------+
| 21 | 3 | c5 | 3 | 220451.26 |
+---------------+--------------+----------+------------+-----------+
| 22 | 4 | c5 | 4 | 231832.26 |
+---------------+--------------+----------+------------+-----------+
| 23 | 5 | c5 | 5 | 254182.26 |
+---------------+--------------+----------+------------+-----------+
| 24 | 6 | c5 | 6 | 260682.26 |
+---------------+--------------+----------+------------+-----------+
| 25 | 7 | c5 | 7 | 267642.26 |
+---------------+--------------+----------+------------+-----------+
| 26 | 8 | c5 | 8 | 282644.26 |
+---------------+--------------+----------+------------+-----------+
| 27 | 1 | c6 | NULL | 32 |
+---------------+--------------+----------+------------+-----------+
| 28 | 2 | c6 | 2 | 35 |
+---------------+--------------+----------+------------+-----------+
| 29 | 3 | c6 | 3 | 32 |
+---------------+--------------+----------+------------+-----------+
| 30 | 4 | c6 | 4 | 32 |
+---------------+--------------+----------+------------+-----------+
| 31 | 5 | c6 | 5 | 32 |
+---------------+--------------+----------+------------+-----------+
| 32 | 6 | c6 | 6 | 32 |
+---------------+--------------+----------+------------+-----------+
| 33 | 7 | c6 | 7 | 32 |
+---------------+--------------+----------+------------+-----------+
| 34 | 8 | c6 | 8 | 32 |
+---------------+--------------+----------+------------+-----------+
| 35 | 1 | c9 | NULL | 51000 |
+---------------+--------------+----------+------------+-----------+