Reputation: 75
I have a issue with my FOR XML clause in SQL. The table with the data in has interval as a colum (see example below), in the data output as of now i get the same date for every interval i got in the columns.
SQL Table:
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
I use this code in my FOR XML clause:
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')
The output i get is following:
<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>
Everything clamp is opening and closing the correct places, but i get the same date 96 times in a row which the server who needs this data cant read.
What i want it to output is following:
<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>
I have tried alot of different things and right now i only see one answer which is to get a stored procedure up and running which will transfer the data to a new table without the dtminterval column, but i would like to use what i already have available, is it possible ?
kind regards.
Upvotes: 2
Views: 31
Reputation: 75
I found the answer myself, it is this code to be used:
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]
Upvotes: 1