Solved

Sql Query Help - grouping?

Posted on 2014-03-05
6
332 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

636 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