Solved

Sql Query Help - grouping?

Posted on 2014-03-05
6
329 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
[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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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