Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Best Way to query db based on Datetime fld?

Author  Topic 

gregger
Starting Member

19 Posts

Posted - 2004-02-01 : 13:50:16
Greetings all,

I would like to create a query for sql server that will allow me to count the number of hits in a 24 hour period.

I am tracking the website stats on our site and I would like to display the last 24 hours. With that in mind, what would be the best way to run execute a sql statement based on this criteria.

1. I assume its best to use "group by order by" in my statement.
2. I would like to return 24 results.

Time Frame Page Impressions
---------- ----------------
12AM - 12:59AM n (number of page impressions)
1AM - 1:59AM n (number of page impressions)
... ...
11PM - 11:59PM n (number of page impressions)

So how would I create a sql statement that would group the data for the past 24 hours and return two fields: Hour, Impressions

My datetime field records the date in this format: 2/1/2004 1:43:25 PM

Here is a sql statement that I used to query the stats table for a range of days. Maybe it will help in understanding what I am trying to accomplish.

SELECT COUNT(*) AS DailyCount, PageAccessDate FROM websiteStats WHERE (PageAccessDate >= '1/1/2004') AND (PageAccessDate <= '2/1/2004') GROUP BY PageAccessDate ORDER BY PageAccessDate

Any suggestions would be greatly appreciated.

Gregg

SamC
White Water Yakist

3467 Posts

Posted - 2004-02-01 : 15:09:29
Here's a proc I wrote to do something similar.....


CREATE PROCEDURE dbo.QR_CourseLoginLog
@CallerAdminID INT ,
@CourseID INT ,
@StartDate DATETIME = NULL ,
@FinishDate DATETIME = NULL
AS
SET NOCOUNT ON
-- This routine relies on the GROUP BY to return a result set grouped by hours
-- Note that the Convert(varchar(8), LastLogin, 112) converts LastLogin to date only (no time part)
-- Converting this back to datetime returns that date at midnight.
-- We then add just the hour back to that date and voila! datetime with hour granularity..
-- GROUP BY then groups all date/hour together, and we can count'em using count(*) as Total
-- Parameterizing the granularity would require some thought. hour, day, week, month would be interesting.

DECLARE @ClientID INT
SELECT @ClientID = ClientID FROM dbo.Admins WHERE AdminID = @CallerAdminID

-- The BETWEEN date operator requires that we round the dates to midnights to properly bracket the condition
SET @StartDate = CAST(CONVERT(VARCHAR, @StartDate, 101) AS DATETIME) -- Resets time to 00:00:00
SET @FinishDate = CAST(CONVERT(VARCHAR, DATEADD(dd, 1, @FinishDate), 101) AS DATETIME) -- Resets time to 00:00:00 of NEXT DAY

SELECT DATEADD(hh, DatePart(hour, LD.LoginDate), Convert(datetime, Convert(varchar(8), LD.LoginDate, 112))) as [Date & Time],
Count(*) as [User Login Count]
FROM dbo.LoginData LD
INNER JOIN dbo.Users U ON U.UserID = LD.UserID
WHERE LD.LoginDate BETWEEN @StartDate AND @FinishDate
AND U.ClientID = @ClientID
AND LD.CourseID = @CourseID
GROUP BY DATEADD(hh, DatePart(hour, LD.LoginDate), Convert(datetime, Convert(varchar(8), LD.LoginDate, 112)))
ORDER BY [Date & Time] DESC

SET NOCOUNT OFF
GO


Comments on improving this welcomed...
Go to Top of Page
   

- Advertisement -