Solved

Sql Query Help - grouping?

Posted on 2014-03-05
6
322 Views
Last Modified: 2014-03-05
Hi

If i use the following:
	  SELECT *
  FROM UsageLogs
WHERE BranchId = 51 AND MachineType = 1 AND ReportedDate between cast(getdate() - 1 as date) and cast(getdate() as date)

Open in new window


I get this:
sql06-07 etc represents the hours of 6am to 7am
What i want to do is return these results so they are grouped to give the count

i.e.
06-07 = 0
10-11 = 3
11-12 = 2

How would i do this with SQL?
0
Comment
Question by:websss
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:regmigrant
ID: 39905950
it would be easier if you describe the table but something along the lines of

Select id, branch_id, machine_id, reported_date, sum(06-07), sum(10-11).... etc
from usagelogs
where branched...
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 39905983
So you need in result only sum as id and machin_id are differenct you are not able to group by that
you will get data at BranchId = 51 AND MachineType = 1 level as they are same

try this for two columns line below , if it works add all columns in sum

Select  branch_id, MachineType , sum(06-07), sum(07-08) From geLogs

WHERE BranchId = 51 AND MachineType = 1 AND ReportedDate between cast(getdate() - 1 as date) and cast(getdate() as date)
group by branch_id, MachineType
0
 

Author Comment

by:websss
ID: 39906015
Yes this worked
Select  BranchId, MachineType , sum(06-07) as [6-7], sum(07-08) as [7-8] From [UsageLogs]
WHERE BranchId = 54 AND MachineType = 1 AND ReportedDate between cast(getdate() - 14 as date) and cast(getdate() as date)
group by BranchId, MachineType

Open in new window



Ive set it for the last 14 days

How would I filter the results by day

i.e. i need to show the amounts for each day (mon, tues, wed etc)
but use the last 14 days data
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39906025
you need it day wise meanse 1 ,2 ,3 days right

Select  branch_id, MachineType , Day(ReportedDate ) , sum(06-07), sum(07-08) From geLogs

WHERE BranchId = 51 AND MachineType = 1 AND ReportedDate between cast(getdate() - 14 as date) and cast(getdate() as date)
group by branch_id, MachineType ,Day(ReportedDate)
0
 

Author Comment

by:websss
ID: 39906041
Thanks
This query
Select  BranchId, MachineType ,Day(ReportedDate) as Day, sum(06-07) as [6-7], sum(07-08) as [7-8] From [UsageLogs]
WHERE BranchId = 54 AND MachineType = 1 AND ReportedDate between cast(getdate() - 14 as date) and cast(getdate() as date)
group by BranchId, MachineType ,Day(ReportedDate)

Open in new window


gets this result (ignore the minus values)
que
I wanted the Day Column Rows to show as Monday, Tuesday etc
Also, what if there is data on Monday the 1st and Monday the 7th? will this be included in the one MONDAY ?
0
 

Author Comment

by:websss
ID: 39906095
Got it!

Select  BranchId, MachineType ,datename(dw,ReportedDate)ReportedDate, sum(06-07) as [6-7], sum(07-08) as [7-8] From [UsageLogs]
WHERE BranchId = 54 AND MachineType = 1 AND ReportedDate between cast(getdate() - 14 as date) and cast(getdate() as date)
group by BranchId, MachineType ,datename(dw,ReportedDate)

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

912 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

17 Experts available now in Live!

Get 1:1 Help Now