• Status: Solved
• Priority: High
• Security: Public
• Views: 46
• Last Modified:

# TSQL How can I transform my data set from Daily level granularity to 1/4 Hour granularity. Examples are within.

HOO.xlsxPlease review the attached example dataset.

I need to be able to convert the sheet of store hours of operation to individual rows that represent one row per 15 minute interval.

Example from Worksheet Store 1001 is open from 00:00 to 00:00 which is 24 hours a day or 96 1/4 hour intervals.

So I would need SQL to return a dataset that basically states that Store 1001 Saturday 00:00 00:15 the next row would be Store 1001 Saturday 00:015 to 00:30 for all 96 intervals of the day on every day of the week.

Example 2 from Worksheet Store 1002 is open from 07:00 until 23:00

I would need SQL to return a dataset that basically states that Store 1002 Saturday 07:00 07:15 the next row would be Store 1001 Saturday 07:015 to 07:30 for all 64 intervals of the day on every day of the week.

Is there a method that would allow me to leverage SQL with the data I have available in the worksheet which comes from a table on SQL Server to return every 15 minute interval that a store is open?

Thank you in advance experts for any assistance you can provide.
0
Ryan Simmons
Asked:
1 Solution

Microsoft SQL Server Developer, Architect, and AuthorCommented:
Don't know the answer off the top of my head, as the SQL Calendar Table article I wrote was based on day, but I was at PASS Summit 2015 where Steve Wake did a presentation on the calendar table concept down to minutes.  His presentation is here, and if you dig into it there's an excellent chance you'll have your answer.

btw Your question is missing the attachments.

Good luck.
Jim
0

Director of OperationsCommented:
You can use CTE to accomplish this, adding 15 minutes to the start time until you hit the end.

``````DECLARE @start Time = '8:00 AM'
DECLARE @end Time = '5:00 PM'
;WITH CTE
AS
(SELECT @start opening
UNION ALL
SELECT DateAdd(MINUTE,15,opening) FROM CTE WHERE opening < @end)
SELECT opening AS "Quarter Hours" FROM CTE
``````

Results:
``````08:00:00.0000000
08:15:00.0000000
08:30:00.0000000
08:45:00.0000000
09:00:00.0000000
09:15:00.0000000
09:30:00.0000000
09:45:00.0000000
10:00:00.0000000
10:15:00.0000000
10:30:00.0000000
10:45:00.0000000
11:00:00.0000000
11:15:00.0000000
11:30:00.0000000
11:45:00.0000000
12:00:00.0000000
12:15:00.0000000
12:30:00.0000000
12:45:00.0000000
13:00:00.0000000
13:15:00.0000000
13:30:00.0000000
13:45:00.0000000
14:00:00.0000000
14:15:00.0000000
14:30:00.0000000
14:45:00.0000000
15:00:00.0000000
15:15:00.0000000
15:30:00.0000000
15:45:00.0000000
16:00:00.0000000
16:15:00.0000000
16:30:00.0000000
16:45:00.0000000
17:00:00.0000000
``````
1

Director of OperationsCommented:
Alternatively, if you're looking to group transactions or something like that into quarter hour increments, you can convert the datetime value to the appropriate quarter hour (if you were planning on doing something like ordering transactions where time > x and < y)

``````SELECT CAST(FLOOR(CAST([DateTimeColumn] AS float)*(24*4))/(24*4) AS smalldatetime) AS "Quarter Hour"
FROM Table
``````

Example:
``````CREATE TABLE #temp (DateTimeColumn datetime)

INSERT INTO #temp (DateTimeColumn)
VALUES (GETDATE()), (DATEADD(MINUTE,14,GETDATE())) ,(DATEADD(MINUTE,53,GETDATE())),(DATEADD(MINUTE,642,GETDATE())),(DATEADD(MINUTE,342,GETDATE())),(DATEADD(MINUTE,224,GETDATE())),(DATEADD(MINUTE,178,GETDATE())),(DATEADD(MINUTE,196,GETDATE()))

SELECT DateTimeColumn,CAST(FLOOR(CAST(DateTimeColumn AS float)*(24*4))/(24*4) AS smalldatetime) AS "Quarter Hour"
FROM #temp

DROP TABLE #temp
``````

Results:
``````2018-02-09 10:50:50.357	2018-02-09 10:45:00
2018-02-09 11:04:50.357	2018-02-09 11:00:00
2018-02-09 11:43:50.357	2018-02-09 11:30:00
2018-02-09 21:32:50.357	2018-02-09 21:30:00
2018-02-09 16:32:50.357	2018-02-09 16:30:00
2018-02-09 14:34:50.357	2018-02-09 14:30:00
2018-02-09 13:48:50.357	2018-02-09 13:45:00
2018-02-09 14:06:50.357	2018-02-09 14:00:00
``````

(PS There is no attached data set)
1

Senior DBACommented:
I don't see sample data, but it likely wouldn't matter, it'll almost certainly follow the standard, best-practice approach, which is:

SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, <datetime_in_table_to_group_by>) / 15 * 15, 0) As TimeInterval,
SUM(...), COUNT(...), SUM(...)
FROM dbo.<table_name>
GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, <datetime_in_table_to_group_by>) / 15 * 15, 0)
ORDER BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, <datetime_in_table_to_group_by>) / 15 * 15, 0)

Why is this approach "best practice"?  Because it's consistent and easy to adjust:
To total by hour,  use:
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, <datetime_in_table_to_group_by>) / 60 * 60, 0)
For 5 minutes:
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, <datetime_in_table_to_group_by>) / 5 * 5, 0)
Etc.
1

Topic AdvisorCommented:
Please attach your samples

You say it comes from another table, so would be good to see the table definition too please :)
1

Workforce Management Analyst, Murphy USAAuthor Commented:
Experts,

I fixed the intial question so you can see the upload. I am going through your suggestions today.

Ryan
0

Workforce Management Analyst, Murphy USAAuthor Commented:
It appears that using CTE is the best solution available.  I believe using a temp table will let me expand the HOO dataset from daily level to Qtr Hourly level.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.