Count of transaction

Hi All,

I am new in SQL Server and creating some SQL script that should give total transaction count per hour for a day. I need simply hour and count result. My table contains DateCreated column and unique ID column.

Please help. Thanks in advance.

September 7th, 2015 4:37pm

Hi,

Please try below sql query;

SELECT DATEPART(hh, DateCreated) AS Hour, count(ID) IDCount
FROM [dbo].[YourTableName]
WHERE DateCreated >= '2015-09-07'
GROUP BY DATEPART(hh, DateCreated)
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 4:46pm

Slight modification of the suggested solution:

SELECT cast(DateCreated as DATE) as [Date], DATEPART(hour, DateCreated) AS [Hour],

count(ID) as Transactions FROM [dbo].[YourTableName] GROUP BY CAST(DateCreated as DATE), DATEPART(hour, DateCreated)

September 7th, 2015 4:58pm

Thanks all,

I am able to see result now. But need some more clear. Can I get result as expected

Current Result    
Date Hour Transactions
9/7/2015 16 350
9/7/2015 13 272
9/7/2015 9 258
9/7/2015 4 300
     
Expected Result    
Date Hour Transactions
9/7/2015        4PM-5PM 350
9/7/2015        1PM-2PM 272
9/7/2015        9AM-10AM 258
9/7/2015        4AM-5AM 300


Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 5:26pm

Try

;with cte as (SELECT cast(DateCreated as DATE) as [Date],

dateadd(hour,DATEPART(hour, DateCreated),'19000101') AS [HourOnly], count(ID) as Transactions FROM [dbo].[YourTableName] GROUP BY CAST(DateCreated as DATE), DATEPART(hour, DateCreated)) select [Date], REPLACE(convert(varchar(20),HourOnly, 100),7),':00','')+'-' +

REPLACE(convert(varchar(20),dateadd(hour,1,HourOnly), 100),7),':00',''), Transactions from cte


September 7th, 2015 5:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics