Thursday, March 5, 2015

Best SQL Challenege: TSQL Query Maximum Concurrent Connections (Participants)

Does this get you what you need?



DECLARE @phoneCalls TABLE (PhoneID INT, PhoneNumber VARCHAR(12), Callstarttime DATETIME, CallEndtime DATETIME)
INSERT INTO @phoneCalls (PhoneID, PhoneNumber, Callstarttime, CallEndtime) VALUES
(1 , '111-111-1111', '2013-04-01 05:13:03.000', '2013-04-01 05:13:03.000'),
(1 , '222-222-2222', '2013-04-01 05:15:12.000', '2013-04-01 05:16:52.000'),
(2 , '333-333-3333', '2013-04-01 05:17:29.000', '2013-04-01 05:24:08.000'),
(2 , '444-444-4444', '2013-04-01 05:21:50.000', '2013-04-01 05:22:31.000'),
(2 , '555-555-5555', '2013-04-01 05:22:41.000', '2013-04-01 05:23:11.000'),
(2 , '666-666-6666', '2013-04-01 05:23:20.000', '2013-04-01 05:23:46.000')

DECLARE @numbers TABLE (number INT)
WHILE (SELECT COUNT(*) FROM @numbers) < 24 INSERT INTO @numbers (number) VALUES ((SELECT COUNT(*) FROM @numbers)+1)

SELECT PhoneID, COUNT(*) AS calls, number AS HOUR
FROM @phoneCalls pc
INNER JOIN @numbers n
ON n.number BETWEEN DATEPART(HOUR,pc.Callstarttime) and DATEPART(HOUR,pc.CallEndtime)
GROUP BY PhoneID, number





Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question.


No comments:

Post a Comment