在获取我要在SQL查询中查找的XML结果时遇到了一些麻烦。
我正在使用这个
SELECT
RTRIM(UN_NMBR),
(SELECT RTRIM(SUB_UNIT_ID)
FOR XML PATH('SUBUNIT'), TYPE),
(SELECT NEW_STAT, [TIME]
FOR XML PATH('STATUS'), TYPE)
FROM
(SELECT
UN_NMBR, NEW_STAT,
CONVERT(TIME,STAMP_CHANGE) as 'TIME'
FROM
Test.SDUPDADM.CADUNTDB2
WHERE
CFS_NUMBR = '021918-73') u
JOIN
Test.SDUPDADM.CADSUBDB2 s on s.UNT_NUMBER = u.UN_NMBR
WHERE
CFS_NUMBR = '021918-73'
FOR XML PATH('UNIT'), TYPE, ROOT('RUNTIMES')
我得到这个结果:
<RUNTIMES>
<UNIT>501A<SUBUNIT>HB5134</SUBUNIT><STATUS> <NEW_STAT>ON_SCENESTOP</NEW_STAT><TIME>12:13:23</TIME></STATUS></UNIT>
<UNIT>501A<SUBUNIT>HB5134</SUBUNIT><STATUS><NEW_STAT>CODE4 </NEW_STAT><TIME>12:29:33</TIME></STATUS></UNIT>
<UNIT>501A<SUBUNIT>HB5134</SUBUNIT><STATUS><NEW_STAT>AVAIL </NEW_STAT><TIME>12:36:29</TIME></STATUS></UNIT>
<UNIT>503A<SUBUNIT>HB5910</SUBUNIT><STATUS><NEW_STAT>DISP </NEW_STAT><TIME>12:13:24</TIME></STATUS></UNIT>
<UNIT>503A<SUBUNIT>HB5910</SUBUNIT><STATUS><NEW_STAT>EN_ROUTE </NEW_STAT><TIME>12:13:26</TIME></STATUS></UNIT>
<UNIT>503A<SUBUNIT>HB5910</SUBUNIT><STATUS><NEW_STAT>ON_SCENE </NEW_STAT><TIME>12:20:42</TIME></STATUS></UNIT>
<UNIT>503A<SUBUNIT>HB5910</SUBUNIT><STATUS><NEW_STAT>AVAIL </NEW_STAT><TIME>12:26:17</TIME></STATUS></UNIT>
</RUNTIMES>
但是我真正想要的是:
<RUNTIMES>
<UNIT>501A
<SUBUNIT>HB5134
<STATUS>
<NEW_STAT>ON_SCENESTOP</NEW_STAT>
<TIME>12:13:23</TIME>
</STATUS>
<STATUS>
<NEW_STAT>CODE4 </NEW_STAT>
<TIME>12:29:33</TIME>
</STATUS>
<STATUS>
<NEW_STAT>AVAIL </NEW_STAT>
<TIME>12:36:29</TIME>
</STATUS>
</SUBUNIT>
</UNIT>
<UNIT>503A
<SUBUNIT>HB5910
<STATUS>
<NEW_STAT>DISP </NEW_STAT>
<TIME>12:13:24</TIME>
</STATUS>
<STATUS>
<NEW_STAT>EN_ROUTE </NEW_STAT>
<TIME>12:13:26</TIME>
</STATUS>
<STATUS>
<NEW_STAT>ON_SCENE </NEW_STAT>
<TIME>12:20:42</TIME>
</STATUS>
<STATUS>
<NEW_STAT>AVAIL </NEW_STAT>
<TIME>12:26:17</TIME>
</STATUS>
</SUBUNIT>
</UNIT>
</RUNTIMES>
我的嵌套中肯定有东西掉了。我在查询中做错什么了?