Solved

sql query with hour group by

Posted on 2011-02-28
2
401 Views
Last Modified: 2012-06-27
hi all,
i am trying to write a query where i am trying to get a result like this.
locationid storeid date              time totalin totalout
1234         abc      12/01/2011   11     5         2
1234         abc      12/01/2011   16     4         1

i am attaching my data how it is store in.

can someone help me out.


 arjaytelecom20101116-1-.csv
0
Comment
Question by:romeiovasu
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility


select locationId, storeid, date , datepart (hour ,CONVERT(datetime, date+' time' ) ), sum(in)  , sum( out )
from tableName
group by locationId, storeid, date , datepart (hour ,CONVERT(datetime, date+' time' ) )
0
 
LVL 4

Expert Comment

by:incerc
Comment Utility
Hi,

In SQL Server you can use Datepart(<part>, <date>) to obtain the hour part from a time field :
http://msdn.microsoft.com/en-us/library/ms174420.aspx

Your query should be something like :

SELECT locationid, storeid, date, datepart(hh, time), sum(totalin), sum(totalout)
FROM mytable
GROUP BY  date, datepart(hh, time), locationid, storeid


0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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

12 Experts available now in Live!

Get 1:1 Help Now