this is my table look like
i have to show data Part Name wise
Part Name Time Incoming Outgoing Transfer
--------- ------------ -------- -------- ---------
Sales 9:00 - 9:30 1 4 6
Sales 9:30 - 10:00 4 5 7
Sales 10:30 - 11:00 8 7 2
Michel 9:00 - 9:30 2 40 16
Michel 9:30 - 10:00 5 15 17
i have to show data party wise and time wise
i am not very good in complex sql development but got a help from one guy. his sql look like
DECLARE @StartTime datetime = '2015-06-09 09:00:00',
@EndTime datetime = '2015-06-09 18:00:00',
@Interval int = 30 -- this can be changed.
DECLARE @PartyIntervals TABLE (
Party1Name VARCHAR(500),
StartRange DATETIME,
EndRange DATETIME)
DECLARE @CSVIntervals TABLE (
Party1Name VARCHAR(500),
Incoming INT,
StartRange DATETIME)
;WITH cSequence AS
(
SELECT
@StartTime AS StartRange,
DATEADD(MINUTE, @Interval, @StartTime) AS EndRange
UNION ALL
SELECT
EndRange,
DATEADD(MINUTE, @Interval, EndRange)
FROM cSequence
WHERE DATEADD(MINUTE, @Interval, EndRange) < @EndTime
)
INSERT INTO @PartyIntervals
SELECT Party1Name,
StartRange,
EndRange
FROM (SELECT DISTINCT Party1Name FROM tridip_Interval) A, cSequence ORDER BY Party1Name;
--SELECT * FROM @PartyIntervals ORDER BY Party1Name
INSERT INTO @CSVIntervals
SELECT Party1Name,
count(*) as incoming,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='I'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name
SELECT P.Party1Name,
P.StartRange,
P.EndRange,
IsNull(Incoming , 0) AS Incoming
FROM @PartyIntervals P LEFT OUTER JOIN
@CSVIntervals A ON A.Party1Name = P.Party1Name AND P.StartRange = A.StartRange
ORDER BY P.Party1Name,P.StartRange
this calculation is not clear
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
why 48 is used ?
see this part too
INSERT INTO @CSVIntervals
SELECT Party1Name,
count(*) as incoming,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='I'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name
here only incoming related data is getting inserted in CSVIntervals
table but i need to also insert outgoing and transfer data for all part in same sql. this is not getting clear how to add more sql to insert outgoing and transfer related data?
if anyone could understand my requirement then please help me to form the sql as a result i will get my desire result.
thanks