Solved

how i can count record by datetime group by day view per hours

Posted on 2012-12-27
9
469 Views
Last Modified: 2012-12-27
i have the following table
select reqport,count(message)
   FROM [radiomix].[dbo].[chat]
    GROUP BY reqport
       , YEAR(datetimein)
       , MONTH(datetimein)
       , DAY(datetimein)

but i want to group by day per hours + ???

i want to show like 1:00 AM 2:00Am ...... 11:59 PM
and each hours the number of message
0
Comment
Question by:afifosh
  • 5
  • 3
9 Comments
 
LVL 15

Expert Comment

by:dbbishop
Comment Utility
Add DATEPART(hh, datetimein) to the grouping.
0
 
LVL 1

Author Comment

by:afifosh
Comment Utility
i have added it but i want to get a format like AM PM
0
 
LVL 9

Expert Comment

by:sognoct
Comment Utility
can't test, but try this :

SELECT  count(0) as nMessages,  convert(nvarchar, datetimein, 103) as currDate, RIGHT(CONVERT(VARCHAR,datetimein, 100),7) as currHour 
FROM [radiomix].[dbo].[chat] 
GROUP BY convert(nvarchar, datetimein, 103), RIGHT(CONVERT(VARCHAR,datetimein, 100),7)

Open in new window

0
 
LVL 15

Expert Comment

by:dbbishop
Comment Utility
If you are wanting to see the hours across the report, try this:
SELECT	reqport,
		SUM(CASE WHEN DATEPART(hh, datetimein) = 0 THEN 1 ELSE 0 END) AS 'Midnight', 
		SUM(CASE WHEN DATEPART(hh, datetimein) = 1 THEN 1 ELSE 0 END) AS '1:00 AM',
		SUM(CASE WHEN DATEPART(hh, datetimein) = 2 THEN 1 ELSE 0 END) AS '2:00 AM',
-- additional times here		
		SUM(CASE WHEN DATEPART(hh, datetimein) = 23 THEN 1 ELSE 0 END) AS '11:00 PM'
FROM	radiomix.dbo.chat
GROUP BY reqport,
		YEAR(datetimein),		
		MONTH(datetimein),		
		DAY(datetimein)

Open in new window

It isn't the cleanest, and you may be able to play around and use the PIVOT function, but seeing as how your time (I'm sure) contains minutes and seconds, it would still take some work.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:dbbishop
Comment Utility
The above won't give you the date. Use
SELECT	reqport,
	CAST(datetimein AS DATE) AS Date,
	SUM(CASE WHEN DATEPART(hh, datetimein) = 0 THEN 1 ELSE 0 END) AS 'Midnight', 
	SUM(CASE WHEN DATEPART(hh, datetimein) = 1 THEN 1 ELSE 0 END) AS '1:00 AM',
	SUM(CASE WHEN DATEPART(hh, datetimein) = 2 THEN 1 ELSE 0 END) AS '2:00 AM',
-- additional times here		
	SUM(CASE WHEN DATEPART(hh, datetimein) = 23 THEN 1 ELSE 0 END) AS '11:00 PM'
FROM	dbo.chat
GROUP BY reqport,
	CAST(datetimein AS DATE)

Open in new window

0
 
LVL 1

Author Comment

by:afifosh
Comment Utility
dbbishop
Msg 243, Level 16, State 1, Line 1
Type DATE is not a defined system type.
Msg 243, Level 16, State 1, Line 1
Type DATE is not a defined system type.

 sognoct your code now work :D

i want to count the message for example
MIDNIG. TO 1:00AM
1:00AM TO 2:00AM


TO

11:00PM TO 11:59PM

FOR EACH REQPORT

AND THE GROUP SHOULD BE
0
 
LVL 15

Expert Comment

by:dbbishop
Comment Utility
Are you using SQL Server 2008?
0
 
LVL 15

Accepted Solution

by:
dbbishop earned 500 total points
Comment Utility
The code I provided would give you smething in the format of
reqport	Date	Midnight	1:00 AM	2:00 AM	11:00 PM
abcd	2012-12-01	4	1	0	0
xyz	2012-12-01	1	0	0	0
abcd	2012-12-02	0	0	1	0
xyz	2012-12-03	0	0	0	0

Open in new window

(with better formatting). I've tested it. Obviously, if you code the remaining times in the CASE statement, it will give you all 24 hours across the report.

If you want each in a separate row, try:
SELECT reqport,  
CAST(datetimein AS DATE) AS Date,
 CASE WHEN DATEPART(hh, datetimein) = 0 THEN 'Midnight' 
		WHEN DATEPART(hh, datetimein) = 12 THEN 'Noon' 
		WHEN DATEPART(hh, datetimein) BETWEEN 1 AND 11 THEN DATENAME(HH, datetimein) + ':00 AM' 
		WHEN DATEPART(hh, datetimein) BETWEEN 13 AND 23 THEN DATENAME(HH, datetimein) + ':00 PM' 
END AS HOUR, 
	COUNT(*)
FROM chat
GROUP BY reqport,  
CAST(datetimein AS DATE),
CASE WHEN DATEPART(hh, datetimein) = 0 THEN 'Midnight' 
		WHEN DATEPART(hh, datetimein) = 12 THEN 'Noon' 
		WHEN DATEPART(hh, datetimein) BETWEEN 1 AND 11 THEN DATENAME(HH, datetimein) + ':00 AM' 
		WHEN DATEPART(hh, datetimein) BETWEEN 13 AND 23 THEN DATENAME(HH, datetimein) + ':00 PM' 
END

Open in new window

If you are not running SQL Server 2008, which it appears you are, since you've put this question in the SQL Server 2008 zone, try changing CAST(datetimein AS DATE) to CONVERT(VARCHAR(10), datetimein, 112)
0
 
LVL 1

Author Comment

by:afifosh
Comment Utility
SELECT	reqport,
		YEAR(datetimein) as years,		
		MONTH(datetimein) as months,		
		DAY(datetimein) as days,
		SUM(CASE WHEN DATEPART(hh, datetimein) = 0 THEN 1 ELSE 0 END) AS 'Midnight', 
		SUM(CASE WHEN DATEPART(hh, datetimein) = 1 THEN 1 ELSE 0 END) AS '1:00 AM',
		SUM(CASE WHEN DATEPART(hh, datetimein) = 2 THEN 1 ELSE 0 END) AS '2:00 AM',
-- additional times here
	    SUM(CASE WHEN DATEPART(hh, datetimein) = 22 THEN 1 ELSE 0 END) AS '10:00 PM',		
		SUM(CASE WHEN DATEPART(hh, datetimein) = 23 THEN 1 ELSE 0 END) AS '11:00 PM'

FROM	radiomix.dbo.chat
GROUP BY reqport,
		YEAR(datetimein),		
		MONTH(datetimein),		
		DAY(datetimein)
ORDER BY
		YEAR(datetimein) DESC,		
		MONTH(datetimein) DESC,		
		DAY(datetimein) DESC
		

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now