Solved

sql query group by hour

Posted on 2014-01-02
2
2,918 Views
Last Modified: 2014-01-06
I have a table named LogEntries that has the following structure




Logdatetime (datetime)

Entries (integer)
Exits (integer)


Here is some sample data


LogDateTime                        Entries      Exits
2013-12-20 08:30:00.000      0                 3      
2013-12-20 08:35:00.000      5                  2
2013-12-20 08:55:00.000      12               7
2013-12-21 09:20:00.000      10                 3      
2013-12-21 09:35:00.000      6                 2
2013-12-21 09:59:00.000      27               4
2013-12-23 17:20:00.000      6                1
2013-12-23 17:48:00.000      6                2
2013-12-23 17:59:00.000      7               3


Trying to write a query that will return sum of entries and exists by date and hour of date

using example above I am looking for following output

Logdate               Hour                 entries    exits

2013-12-20               8                       17                 12      
2013-12-21               9                       43              9
2013-12-23              17                    19              6
0
Comment
Question by:johnnyg123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39752756
Yes, it is possible:
select CAST(LogDateTime AS Date) as LogDate
, datepart(hh, LogDateTime) as Hour
, Sum(Entries) as Entries
, Sum(Exits) as Exits
FROM Table1
Group By CAST(LogDateTime AS Date)
, datepart(hh, LogDateTime)

Open in new window

0
 
LVL 8

Expert Comment

by:Surrano
ID: 39753359
Using dateadd:
select dateadd(hour, datediff(hour, 0, logdatetime), 0) as loghour,
  sum(entries) as entries, sum(exits) as exits)
from LogEntries 
-- where <any condition>
group by dateadd(hour, datediff(hour, 0, logdatetime), 0) 
order by dateadd(hour, datediff(hour, 0, logdatetime), 0);

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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