2015-11-02 69 views
2

我在使用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 

謝謝。 親切的問候。

+0

這不是很明顯,你的「我想要」和你的「我有」部分如何配合在一起。請用一些示例數據說明您的表格結構。尤其是您的ContactsRecieved似乎是「我想要」中的一個計數器,並且似乎是您的「我有」部分中的一種ID。請注意,您要創建嵌套的層次結構... – Shnugo

+0

嗨。謝謝您的回答。 我想要的是第一個XML結構,但我擁有的是第二個,我不知道如何重寫SQL查詢以獲得第二個XML結構。 –

+0

是的,這是清楚的,但請再次閱讀我的評論,並回答問題,thx – Shnugo

回答

2

你可以嘗試這樣的:

只是整個代碼粘貼到一個空的查詢窗口,然後執行。適應您的需求。

DECLARE @tbl TABLE(CTID INT,day INT,month INT,year INT,Time TIME,periodData VARCHAR(100)); 
INSERT INTO @tbl VALUES 
(2,13,10,2015,{t'08:00:00'},'5.05004') 
,(2,13,10,2015,{t'08:15:00'},'5.24004') 
,(2,13,10,2015,{t'08:30:00'},'4.49004') 
,(2,13,10,2015,{t'08:45:00'},'4.50004') 
,(2,13,10,2015,{t'09:00:00'},'5.65004') 
,(2,13,10,2015,{t'09:15:00'},'7.00006') 
,(2,13,10,2015,{t'09:30:00'},'7.12006') 
,(2,13,10,2015,{t'09:45:00'},'7.39006') 
,(2,13,10,2015,{t'10:00:00'},'8.51007') 
,(2,13,10,2015,{t'10:15:00'},'7.86006'); 

WITH DistinctIDs AS --assuming that in your original data there are more IDs 
(
    SELECT DISTINCT CTID 
    FROM @tbl 
) 
,DistinctDates AS 
(
    SELECT DISTINCT CTID 
        ,day 
        ,month 
        ,year 
    FROM @tbl 
) 
SELECT DistinctIDs.CTID 
     ,DistinctDates.* 
FROM DistinctIDs 
OUTER APPLY 
(
    SELECT dd.day AS [date/day] 
      ,dd.month AS [date/month] 
      ,dd.year AS [date/year] 
      ,PeriodData.periodData AS [node()] 
    FROM DistinctDates AS dd 
    CROSS APPLY 
    (
     SELECT pd.Time AS [time] 
       ,0 AS [contactsRecieved] --Don't know the meaning of this "0"... All example lines are the same...    
     FROM @tbl AS pd 
     WHERE pd.CTID=DistinctIDs.CTID 
      AND pd.day = dd.day 
      AND pd.month = dd.month 
      AND pd.year = dd.year 
     FOR XML PATH('periodData'),TYPE 
    ) AS PeriodData(periodData) 
    WHERE dd.CTID=DistinctIDs.CTID 
    FOR XML PATH(''),TYPE 
) AS DistinctDates(forecastData) 
FOR XML PATH('ctForecast'), ROOT('forecastImport') 
+0

謝謝你的時間來幫助我,這對我來說意味着很多。我目前正在運行你的查詢,儘管它已經被修改了一些,但是由於它大概有600,000行,它可能需要一段時間:)但是它看起來會起作用。它有你所做的@tbl的正確的XML佈局,所以我確信它可以在全尺寸的表上工作。 此外,'0'表示不同CTID上的調用數量(CTID =應用程序ID),所以這就是爲什麼它是0,CTID部門wasnt/isnt在該特定日期開放:) –