Solved

Sql Query Help - grouping?

Posted on 2014-03-05
6
320 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

20 Experts available now in Live!

Get 1:1 Help Now