Microsoft SQL Server
--
Questions
--
Followers
Top Experts
I have a stored procedure that aggregates data hourly, but would like to also split the data half hourly, I'm not sure the best approach would be and welcome any pointers:
	@DateTimeToFilter DATETIME
BEGIN
	SET NOCOUNT ON;
	
	WITH H ( [Hour] ) AS
	(	SELECT  7 		UNION 
		SELECT  8 		UNION 
		SELECT  9 		UNION 
		SELECT  10		UNION 
		SELECT  11		UNION 
		SELECT  12		UNION 
		SELECT  13		UNION 
		SELECT  14		UNION 
		SELECT  15		UNION 
		SELECT  16		UNION 
		SELECT  17		UNION 
		SELECT  18		UNION 
		SELECT  19
	)
	SELECT	
		H.[Hour], COUNT(T.BookingID) AS NoOfUsers
	FROM	
		H LEFT JOIN tbl_Visitors T ON H.[Hour] = DATEPART(HOUR, T.BookedInTime) AND
			((DATEDIFF(dd, T.BookedInTime, @DateTimeToFilter) = 0) AND 
			(DATEDIFF(mm, T.BookedInTime, @DateTimeToFilter) = 0) AND 
			(DATEDIFF(yy, T.BookedInTime, @DateTimeToFilter) = 0))
	GROUP BY 
		H.[Hour];
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
No big changes, instant results.
SELECT  DATEDIFF(HOUR, '2000-01-01', create_date) % 24 ,
        COUNT(*)
FROM    sys.tables T
GROUP BY DATEDIFF(HOUR, '2000-01-01', create_date) % 24;
SELECT  DATEDIFF(MINUTE, '2000-01-01', create_date) % 30 ,
        COUNT(*)
FROM    sys.tables T
GROUP BY DATEDIFF(MINUTE, '2000-01-01', create_date) % 30;






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
2014-06-05 14:00:00
2014-06-05 16:45:00
2014-06-05 16:45:00
Regards,
UNION does a "distinct sort" so the in the final result no 2 rows are the same. UNION ALL does not do this step.
below you should use UNION ALL because 7 is different to 8 is different to 9 ... you do not need the extra work the UNION does
	(	SELECT  7 		UNION 
		SELECT  8 		UNION 
		SELECT  9 		UNION 
		SELECT  10		UNION 
		SELECT  11		UNION 
		SELECT  12		UNION 
		SELECT  13		UNION 
		SELECT  14		UNION 
		SELECT  15		UNION 
		SELECT  16		UNION 
		SELECT  17		UNION 
		SELECT  18		UNION 
		SELECT  19
--If 
select dateadd( hour, datediff( hour, 0, getdate()), 0 )
--rounds to the hour, and
select dateadd( minute, datediff( minute, 0, getdate()), 0 )
--rounds to the minute, try this:
select dateadd( minute, ( datediff( minute, 0, getdate()) / 30 ) * 30, 0 )
--The divide by 30 should truncate as its integer maths.
HTH
 David
PS Assumes that your times are in a datetime or similar, replace getdate() with the tablealias.columname ...

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Microsoft SQL Server
--
Questions
--
Followers
Top Experts
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.