Solved

SQL Query - Group by day

Posted on 2011-03-16
17
582 Views
Last Modified: 2012-08-13
Hi,

I'm trying to figure out in my head how to write a query, but I can't seem to think up the logic.

What I want to do is group a days incidents up and count them. then I want to list the data by

Top incidents by day. if that makes sense. example.

Day 1 12/10/2010  - 45 incidents
Day 2 12/11/2010 - 30 incidents
Day 3  12/12/2010- 15 incidents

etc


If anyone knows what this logic would look like I'd appreciate it.

attached is a pic of the table

 sql table
0
Comment
Question by:TheWebGuy38
  • 6
  • 5
  • 5
  • +1
17 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility

You need to group by day on Datestamp field ? right?

try this
Select X.DateStamp ,X.incidents
From (
Select DateStamp , count(*) as incidents from IncidentReport
Group by DateStamp ) X
order by X.incidents desc


OR TRY THIS

Select X.DateStamp ,X.incidents
From (
Select DateStamp , count(*) as incidents from IncidentReport
Group by DateStamp ) X
order by X.incidents asc
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
You can think of something like this:
Select ROWNUM, IncidentDate, Count(IncidentDate) from YourTable Group by IncidentDate order by IncidentDate
0
 

Author Comment

by:TheWebGuy38
Comment Utility
so something like this?

SELECT *, ROWNUM, TimeOccured AS Expr1, COUNT(TimeOccured) AS Expr2
FROM  IncidentReport
GROUP BY TimeOccured
ORDER BY TimeOccured

Although it's having a problem recognizing ROWNUM
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
Try:

SELECT *, ROW_Number, TimeOccured AS Expr1, COUNT(TimeOccured) AS Expr2
FROM  IncidentReport
GROUP BY TimeOccured
ORDER BY TimeOccured
0
 

Author Comment

by:TheWebGuy38
Comment Utility
same thing, having a problem at ROW_Number

what is rownum anyways? a variable to pass data back?
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
It puts a serial number to your rows (1, 2, 3, ...):

OK, let's try this please:

SELECT *, ROW_NUMBER() OVER (ORDER BY TimeOccured) AS n, TimeOccured AS Expr1, COUNT(TimeOccured) AS Expr2
FROM  IncidentReport
GROUP BY TimeOccured
ORDER BY TimeOccured
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility

order by will not work with gorup by

Select ROW_NUMBER() OVER (ORDER BY TimeOccured) AS n,X.TimeOccured ,x.incedences
SELECT TimeOccured ,  TimeOccured AS Expr1, COUNT(TimeOccured) AS incedences
FROM  IncidentReport
GROUP BY TimeOccured ) X
ORDER BY X.TimeOccured
0
 

Author Comment

by:TheWebGuy38
Comment Utility
err,, no ROW_NUMBER is don't think is anything
and I'm passing it into a dataset, will this syntax work like that?
0
Zoho SalesIQ

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

 

Author Comment

by:TheWebGuy38
Comment Utility
trying it this way, still blowing up
SELECT TimeOccured
FROM  (SELECT TimeOccured, COUNT(*)
               FROM   IncidentReport
               GROUP BY DateStamp)
ORDER BY incidents
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
try thia

SELECT X.TimeOccured,X.incidents
FROM  (SELECT TimeOccured, COUNT(*) as incidents
               FROM   IncidentReport
               GROUP BY DateStamp) X
ORDER BY X.incidents
0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
What about this:
Select TimeOccured, Count(TimeOccured)
From IncidentReport
Group by TimeOccured
Order by TimeOccured
0
 

Author Comment

by:TheWebGuy38
Comment Utility
ok, getting somewhere

this executes

SELECT TimeOccured, COUNT(TimeOccured) AS IncidentCount
FROM  IncidentReport
GROUP BY TimeOccured
ORDER BY IncidentCount DESC

but something is wrong, I think maybe it's not getting the full day, or maybe I have to include the day/time and not just the date
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
Comment Utility
try this

SELECT convert(varchar(10),TimeOccured, 101), COUNT(TimeOccured) AS IncidentCount
FROM  IncidentReport
GROUP BY convert(varchar(10),TimeOccured, 101)
ORDER BY IncidentCount DESC
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
If you are working in SQL Server 2008, try this.
SELECT CONVERT(DATE,TimeOccured) AS TimeOccured, 
         COUNT(TimeOccured)        AS IncidentCount 
    FROM IncidentReport 
GROUP BY CONVERT(DATE,TimeOccured) 
ORDER BY IncidentCount DESC

Open in new window

If SQL Server 2005 or less, try this.
SELECT DATEADD(dd,0,DATEDIFF(dd,0,TimeOccured)) AS TimeOccured, 
         COUNT(TimeOccured)                       AS IncidentCount 
    FROM IncidentReport 
GROUP BY DATEADD(dd,0,DATEDIFF(dd,0,TimeOccured)) 
ORDER BY IncidentCount DESC

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
Comment Utility
Set the field TimeOccured datatype to Date.
0
 

Author Comment

by:TheWebGuy38
Comment Utility
OH YA!!!!!!!!!!

pratima, you the man!!!

this works great!

SELECT TOP (100) CONVERT(varchar(10), TimeOccured, 101) AS TimeOccuredDay, COUNT(TimeOccured) AS IncidentCount
FROM  IncidentReport
GROUP BY CONVERT(varchar(10), TimeOccured, 101)
ORDER BY IncidentCount DESC


I only have one last questio. I'm trying to pull IncidentReportID

but I try this

SELECT TOP (100) CONVERT(varchar(10), TimeOccured, 101) AS TimeOccuredDay, COUNT(TimeOccured) AS IncidentCount, IncidentReportID
FROM  IncidentReport
GROUP BY CONVERT(varchar(10), TimeOccured, 101)
ORDER BY IncidentCount DESC


but it blows up. is this possible to do?

0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
SELECT TOP (100) CONVERT(varchar(10), TimeOccured, 101) AS TimeOccuredDay, COUNT(TimeOccured) AS IncidentCount, IncidentReportID
FROM  IncidentReport
GROUP BY CONVERT(varchar(10), TimeOccured, 101),IncidentReportID

ORDER BY IncidentCount DESC
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

11 Experts available now in Live!

Get 1:1 Help Now