You can create a CTE
with the possible intervals within the times you have determined and so cross the data you already have. For the ranges use the clause WITH
as follows:
DECLARE @inicio DATETIME = '2019-05-16 08:00:00';
DECLARE @fim DATETIME = '2019-05-16 09:00:00';
WITH intervalos AS (
SELECT 1 AS id,
@inicio AS inicio,
DATEADD(MINUTE, 15, @inicio) AS fim
UNION ALL
SELECT i.id + 1 AS id,
i.fim AS inicio,
DATEADD(MINUTE, 15, i.fim) AS fim
FROM intervalos i
WHERE i.fim < @fim
)
SELECT *
FROM intervalos i
Which will result in the following:
id | inicio | fim
---------------------------------------------------
1 | 2019-05-16 08:00:00.000 | 2019-05-16 08:15:00.000
2 | 2019-05-16 08:15:00.000 | 2019-05-16 08:30:00.000
3 | 2019-05-16 08:30:00.000 | 2019-05-16 08:45:00.000
4 | 2019-05-16 08:45:00.000 | 2019-05-16 09:00:00.000
With this table you only need to perform one JOIN
and will have the desired result.
WITH
APPLIES TO: SQL Server
(from 2008 onwards), Banco de Dados SQL do Azure
, SQL Data Warehouse do Azure
and Parallel Data Warehouse
Specifies a temporary named result set, known as a CTE
(common table expression). It is derived from a simple query and defined in the execution scope of a single instruction SELECT
, INSERT
, UPDATE
, DELETE
or MERGE
. This clause can also be used in a CREATE VIEW statement as part of the instruction SELECT
that defines it. A common table expression can include references to itself. This is what we call the common recursive table expression.
Syntax
[ WITH <common_table_expression> [ ,...n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,...n ] ) ]
AS
( CTE_query_definition )
Arguments
expression_name
Is a valid identifier for the common table expression. expression_name
must be different from the name of any other common table expression defined in the same clause WITH <common_table_expression>
, but expression_name
may be the same as the name of a base table or display. Any reference to expression_name
query uses the common table expression, not the base object.
column_name
Specifies a column name in the common table expression. Duplicate names are not allowed in a single definition of CTE
. The number of column names specified shall correspond to the number of columns in the result set of the CTE_query_definition
. The list of column names will be optional only if separate names are provided for all the resulting columns in the query definition.
CTE_query_definition
Specifies an instruction SELECT
whose result set populates the common table expression. The statement SELECT
of CTE_query_definition
must meet the same requirements as for the creation of an exhibition, with the exception that a CTE
cannot define another CTE
. For more information, see the Comments and CREATE VIEW (Transact-SQL)
.
If more than one CTE_query_definition
is defined, the definitions of consultation may be joined by one of these two sets of operators: UNION ALL
, UNION
, EXCEPT
or INTERSECT
.
What the
SGBD
used?– Sorack
Microsoft sql server management studio 2012
– Erick Bessa
If it were in Postgresql you could use the generate_series (Set Returning Function) function. I do not know if there is something similar in Microsoft SQL Server.
– anonimo
The closest example I found was this (https://www.sqlservercentral.com/forums/topic/group-data-into-15-minute-intervals)
– Erick Bessa
How are the Date and Time columns declared? Which table do they belong to?
– José Diz