And a stored procedure ?
Main Topics
Browse All TopicsHello,
Consider this table structure / data :
Column : Connexions DateRecorded
Row1 : 10 1/1/2007 11:12:45 pm
Row2 : 11 1/1/2007 11:17:12 pm
Row3 : 13 1/1/2007 11:17:59 pm
Row4 : 12 1/1/2007 11:25:11 pm
Row5 : 11 1/1/2007 11:26:13 pm
Row6 : 10 1/1/2007 11:28:52 pm
Row7 : 9 1/1/2007 11:28:59 pm
Row8 : 8 1/1/2007 11:41:00 pm
Row9 : 7 1/1/2007 11:47:15 pm
Based on that table, I want to retrieve all the data from 11:12 to 12h00 (the getDate() returns 12:00:00), group by minute to get this :
Column : Connexions DateRecorded
Row1 : 10 1/1/2007 11:12
Row2 : 10 1/1/2007 11:13
Row3 : 10 1/1/2007 11:14
Row4 : 10 1/1/2007 11:15
Row5 : 10 1/1/2007 11:16
Row6 : 13 1/1/2007 11:17
Row7 : 13 1/1/2007 11:18
Row8 : 13 1/1/2007 11:19
Row9 : 13 1/1/2007 11:20
Row10 : 13 1/1/2007 11:21
Row11 : 13 1/1/2007 11:22
Row12 : 13 1/1/2007 11:23
Row13 : 13 1/1/2007 11:24
Row14 : 12 1/1/2007 11:25
Row15 : 11 1/1/2007 11:26
Row16 : 11 1/1/2007 11:27
Row17 : 10 1/1/2007 11:28
Row18 : 10 1/1/2007 11:29
Row19 : 10 1/1/2007 11:30
Row20 : 10 1/1/2007 11:31
Row21 : 10 1/1/2007 11:32
Row22 : 10 1/1/2007 11:33
Row23 : 10 1/1/2007 11:34
Row24 : 10 1/1/2007 11:35
Row25 : 10 1/1/2007 11:36
Row26 : 10 1/1/2007 11:37
Row27 : 10 1/1/2007 11:38
Row28 : 10 1/1/2007 11:39
Row29 : 10 1/1/2007 11:40
Row30 : 8 1/1/2007 11:41
Row31 : 8 1/1/2007 11:42
Row32 : 8 1/1/2007 11:43
Row33 : 8 1/1/2007 11:44
Row34 : 8 1/1/2007 11:45
Row35 : 8 1/1/2007 11:46
Row36 : 7 1/1/2007 11:47
Please note that we do a MAX for grouping, so we keep the MAXIMUM value of the grouped records for the same minute. The complexity is to fill the record for each minute that is not on the table.
If it's not possible with a single SQL query, a stored proc is OK ;)
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
yes, that is what I want to say. now, clearly this is true for what you are trying to do here, that does not mean that ANY kind of processing will be more efficient in client side code, usually the procedure is more efficient.
however, a process like this one (filling the holes), is simpler and faster using client code.
The other way you could do it:
1. Create a table containing every single minute in the day call AllMinutes. It goes from 00:00 to 23:59
2. Use the following SQL to get 'single' metric per minute:
SELECT
MAX(Connexions) As Connections,
CONVERT(VARCHAR(5),DateRec
FROM YourTable
WHERE YEAR(DateRecorded) * 10000 + MONTH(DateRecorded) * 100 + DAY(DateRecorded) = 20070101
Then do an outer join on your 'every single minute' table to get something close:
SELECT M.MinuteRecorded, D.Connections
FROM AllMinutes M
LEFT OUTER JOIN
(
SELECT
MAX(Connexions) As Connections,
CONVERT(VARCHAR(5),DateRec
FROM YourTable
WHERE YEAR(DateRecorded) * 10000 + MONTH(DateRecorded) * 100 + DAY(DateRecorded) = 20070101
) D
ON M.MinuteRecorded = D.MinuteRecorded
Note that:
-The field MinuteRecorded in the table AllMinutes needs be CHAR(5) for this to work.
-The outer join will have NULLS in it - we want to fix this, maybe I'll think of something when I come back from lunch
-This only works for a single day.
A temp table should do reasonably well in doing that task; IMO, it could even perform better than sending all details to the client for processing there. For example, please try code below.
SET NOCOUNT ON
-- generate sample data (in temp table, for testing only)
IF OBJECT_ID('tempdb.dbo.#dat
DROP TABLE #data
CREATE TABLE #data (
dummy VARCHAR(20),
connexions INT,
dateRecorded DATETIME
)
INSERT INTO #data VALUES('Row1 :' , 10 , '1/1/2007 11:12:45 pm')
INSERT INTO #data VALUES('Row2 :' , 11 , '1/1/2007 11:17:12 pm')
INSERT INTO #data VALUES('Row3 :' , 13 , '1/1/2007 11:17:59 pm')
INSERT INTO #data VALUES('Row4 :' , 12 , '1/1/2007 11:25:11 pm')
INSERT INTO #data VALUES('Row5 :' , 11 , '1/1/2007 11:26:13 pm')
INSERT INTO #data VALUES('Row6 :' , 10 , '1/1/2007 11:28:52 pm')
INSERT INTO #data VALUES('Row7 :' , 9 , '1/1/2007 11:28:59 pm')
INSERT INTO #data VALUES('Row8 :' , 8 , '1/1/2007 11:41:00 pm')
INSERT INTO #data VALUES('Row9 :' , 7 , '1/1/2007 11:47:15 pm')
-- main logic, input
DECLARE @startDate DATETIME
DECLARE @endDate DATETIME
-- set start and end date to capture;
-- modify this code as needed to input/set starting values
SET @startDate = '01/01/2007 11:00:23 pm'
SET @endDate = '01/02/2007 12:00:45 am'
--************************
-- main logic, processing [code should not need modified]
SET NOCOUNT ON
-- make sure @startDate and @endDate are on an even minute
SET @startDate = CONVERT(VARCHAR(30), @startDate, 100)
SET @endDate = CONVERT(VARCHAR(30), @endDate, 100)
IF OBJECT_ID('tempdb.dbo.#min
DROP TABLE #mins
CREATE TABLE #mins (
dateRecorded DATETIME NOT NULL,
connexions INT NULL,
UNIQUE CLUSTERED (dateRecorded, connexions)
)
WHILE @startDate <= @endDate
BEGIN
INSERT INTO #mins VALUES(@startDate, NULL)
SET @startDate = DATEADD(MINUTE, 1, @startDate)
END --WHILE
UPDATE mins
SET connexions = data.MaxConnexions
FROM #mins mins
INNER JOIN (
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(30), dateRecorded, 100), 100) AS dateRecorded,
MAX(connexions) AS MaxConnexions
FROM #data
GROUP BY CONVERT(DATETIME, CONVERT(VARCHAR(30), dateRecorded, 100), 100)
) AS data ON mins.dateRecorded = data.dateRecorded
UPDATE mins
SET connexions = (
SELECT connexions
FROM #mins mins2
WHERE dateRecorded = (
SELECT MAX(mins3.dateRecorded)
FROM #mins mins3
WHERE mins3.dateRecorded < mins.dateRecorded
AND mins3.connexions IS NOT NULL ) )
FROM #mins mins
WHERE connexions IS NULL
SET NOCOUNT OFF
SELECT *
FROM #mins
Business Accounts
Answer for Membership
by: angelIIIPosted on 2006-12-16 at 07:19:35ID: 18151736
you should do that in the client application, there it will be much easier (usually) and much more performant.
I would NOT try to solve that in sql