Solved

sql query with hour group by

Posted on 2011-02-28
2
408 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
ID: 35000125


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
ID: 35000184
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

778 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