我在使用FOR XML PATH子句時遇到問題。我希望它看起來像我下面的內容:FOR XML路徑打開/關閉
<forecastImport>
<ctForecast>
<ctID>2</ctID>
<forecastData>
<date><day>24</day><month>12</month><year>2014</year></date>
<periodData>
<time>00:00</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>00:15</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>00:30</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>00:45</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>01:00</time>
<contactsReceived>0</contactsReceived>
</periodData>
......
<periodData>
<time>23:15</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>23:30</time>
<contactsReceived>0</contactsReceived>
</periodData>
<periodData>
<time>23:45</time>
<contactsReceived>0</contactsReceived>
</periodData>
</forecastData>
</ctForecast>
這是應該的,但無論我做什麼我得到這樣的結果:
<forecastImport>
<ctForecast>
<CTID>2</CTID>
<forecastData>
<date>
<day>13</day>
<month>10</month>
<year>2015</year>
</date>
<periodData>
<Time>08:00:00</Time>
</periodData>
<contacsReceived>
<periodData>5.05004</periodData>
</contacsReceived>
</forecastData>
</ctForecast>
<ctForecast>
<CTID>2</CTID>
<forecastData>
<date>
<day>13</day>
<month>10</month>
<year>2015</year>
</date>
<periodData>
<Time>08:15:00</Time>
</periodData>
<contacsReceived>
<periodData>5.24004</periodData>
</contacsReceived>
</forecastData>
</ctForecast>
<ctForecast>
的forecastData應關閉了一整天后, ,而不是每個時間間隔。我使用下面的查詢:
SELECT
--[ID]
[CTID]
,[forecastData/date/day] = Day(dDate)
,[forecastData/date/month] = month(dDate)
,[forecastData/date/year] = year(dDate)
,[dtmInterval] AS [forecastData/periodData/Time]
,[CO_Forecast] AS [forecastData/contacsReceived/periodData]
--,[AHT_Forecast]
FROM [ProductionForecast].[dbo].[Forecast]
FOR XML PATH ('ctForecast'), ROOT ('forecastImport')
我真的不知道該怎麼辦,我剛纔學到的FOR XML子句,僅熟悉PATH和AUTO的時刻。我使用的是Microsoft SQL 2014
這是表結構:
CTID forecastData/date/day forecastData/date/month forecastData/date/year periodData/Time contacsReceived/periodData
2 13 10 2015 08:00:00 5.05004
2 13 10 2015 08:15:00 5.24004
2 13 10 2015 08:30:00 4.49004
2 13 10 2015 08:45:00 4.50004
2 13 10 2015 09:00:00 5.65004
2 13 10 2015 09:15:00 7.00006
2 13 10 2015 09:30:00 7.12006
2 13 10 2015 09:45:00 7.39006
2 13 10 2015 10:00:00 8.51007
2 13 10 2015 10:15:00 7.86006
謝謝。 親切的問候。
這不是很明顯,你的「我想要」和你的「我有」部分如何配合在一起。請用一些示例數據說明您的表格結構。尤其是您的ContactsRecieved似乎是「我想要」中的一個計數器,並且似乎是您的「我有」部分中的一種ID。請注意,您要創建嵌套的層次結構... – Shnugo
嗨。謝謝您的回答。 我想要的是第一個XML結構,但我擁有的是第二個,我不知道如何重寫SQL查詢以獲得第二個XML結構。 –
是的,這是清楚的,但請再次閱讀我的評論,並回答問題,thx – Shnugo