2015-12-02 31 views
2

我在SQL中的FOR XML子句有問題。數據表中的數據以間隔爲間隔(參見下面的示例),在截至目前的數據輸出中,我獲得了列中每個間隔的相同日期。用於xml構建問題的SQL

SQL表:

ID CTID dDate dtmInterval CO_Forecast AHT_Forecast 
1 2 2015-10-13 08:00:00 5.05004 457.67751 
2 2 2015-10-13 08:15:00 5.24004 457.67751 
3 2 2015-10-13 08:30:00 4.49004 457.67751 
4 2 2015-10-13 08:45:00 4.50004 457.67751 
5 2 2015-10-13 09:00:00 5.65004 457.67751 

我使用此代碼在我的FOR XML子句:

SELECT TOP 1000 

[ctID] 
,[forecastData/date/day] = Day(dDate) 
,[forecastData/date/month] = month(dDate) 
,[forecastData/date/year] = year(dDate) 
,[co_forecast] AS [forecastData/dailyData/contactsReceived] 
,[AHT_Forecast] AS [forecastData/dailyData/averageAHT] 

FROM [ProductionForecast].[dbo].[Forecast] 

group by [co_forecast], [AHT_Forecast], [CTID], dDate 

FOR XML PATH ('ctForecast'), ROOT ('forecastImport') 

輸出我得到的是以下幾點:

<forecastImport> 
    <ctForecast> 
    <ctID>2</ctID> 
    <forecastData> 
     <date> 
     <day>13</day> 
     <month>10</month> 
     <year>2015</year> 
     </date> 
     <dailyData> 
     <contactsReceived>5.05004</contactsReceived> 
     <averageAHT>457.67751</averageAHT> 
     </dailyData> 
    </forecastData> 
    </ctForecast> 
    <ctForecast> 
    <ctID>2</ctID> 
    <forecastData> 
     <date> 
     <day>13</day> 
     <month>10</month> 
     <year>2015</year> 
     </date> 
     <dailyData> 
     <contactsReceived>5.24004</contactsReceived> 
     <averageAHT>457.67751</averageAHT> 
     </dailyData> 
    </forecastData> 
    </ctForecast> 

一切鉗開關閉正確的地方,但我連續獲得96次相同的日期,哪些服務器誰需要此數據不能讀取。

我希望它輸出什麼是以下幾點:

<forecastImport> 
<ctForecast> 
<ctID>95</ctID> 
<forecastData> 
<date><day>01</day><month>11</month><year>2015</year></date> 
<dailyData> 
<contactsReceived>0</contactsReceived> 
<averageAHT>0</averageAHT> 
</dailyData> 
</forecastData> 
</ctForecast> 
<ctForecast> 
<ctID>95</ctID> 
<forecastData> 
<date><day>02</day><month>11</month><year>2015</year></date> 
<dailyData> 
<contactsReceived>594.81653</contactsReceived> 
<averageAHT>387</averageAHT> 
</dailyData> 
</forecastData> 
</ctForecast> 

我已經嘗試了很多不同的東西,現在我只看到一個答案,就是讓一個存儲過程運行起來,這將傳輸數據到沒有dtminterval列的新表,但我想使用我已有的可用,這可能嗎?

親切的問候。

回答

1

,我發現自己的答案,但要使用此代碼:

SELECT 
[CTID] 
,[dDate] 
,[CO_Forecast] = Sum([CO_Forecast]) 
,AHT_Forecast = CASE WHEN Sum([CO_Forecast]) = 0 THEN 0 ELSE SUM(LOAD)/  Sum([CO_Forecast]) 
END 

FROM 
(
SELECT 
[ID] 
,[CTID] 
,[dDate] 
,[dtmInterval] 
,[CO_Forecast] 
,[AHT_Forecast] 
,LOAD = CO_Forecast * AHT_Forecast 

FROM    
[ProductionForecast].[dbo].[Forecast]) AS Sub 

GROUP BY 
[CTID] 
,[dDate] 
+0

大,你發現自己的答案(upvoted這個......)。現在,您應該將自己的答案標記爲已接受,以顯示此問題已關閉...... Thx – Shnugo