Solved

Sql Query Help - grouping?

Posted on 2014-03-05
6
326 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Row insertion failed. Array 5 46
Access SQL Server instance by NONE-admin Windows user 12 27
SQL Query 2 31
SQL - Simple Pivot query 8 13
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
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

840 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