Solved

Counting Number of Occurrences in Date Range

Posted on 2007-03-27
6
522 Views
Last Modified: 2012-06-27
Sql Server 2005
Is there a straightforward way to calculate the number of occurrences in a given date range? Ex.
I have a Vehicle table V joined with VehicleRepairOrder table RO on V.VehicleId  = RO.VehicleId.

It's a one-to-many relationship and I need to find the number of vehicles with 3 or more repair orders in any given year (RO.DateOfRO is the field I'm trying to make the date calculations from)

Any input or suggestions would be appreciated.
0
Comment
Question by:ViceroyFizzlebottom
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18801483
Select V.VehicleID, count(RO.DateOfRO) as NumRepairs
From Vehicle V Inner Join VehicleRepairOrder RO on V.VehicleId  = RO.VehicleId
Where DateOfRO Between '1/1/2006' and '12/31/2006'
Group by V.VehicleID
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18801501
Above solution gives you the count for each vehicle.

to limit it to those w/ 3+ repairs, add:

HAVING count(RO.DateOfRO) >=3
0
 
LVL 6

Author Comment

by:ViceroyFizzlebottom
ID: 18801790
Thanks for the quick response first of all.
The solution you gave is in fact what I am using, but to clarify a bit; that solution gives the number of vehicles if the date range is explicitly given. I am more interested in something that for the entire history of vehicle repairs (in this case is 1999-Present).
The method I was (am) using and what you listed above gives the vehicles for that year, but if the same vehicle was also in 3 or more times in 2005 and I run the query for 2005, that vehicle will essentially be counted twice.

Sorry if that doesn't help explain my issue a little better.


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 250 total points
ID: 18801867
OK ... how about this?


Select distinct VehicleID From
(Select V.VehicleID, Year(RO.DateOfRO) as RepYear, count(RO.DateOfRO) as NumRepairs
From Vehicle V Inner Join VehicleRepairOrder RO on V.VehicleId  = RO.VehicleId
Group by V.VehicleID, Year(RO.DateOfRO)
HAVING count(RO.DateOfRO) >=3) VCount
0
 
LVL 6

Author Comment

by:ViceroyFizzlebottom
ID: 18802190
Ah beautiful. It's funny how simple and straightforward things are when someone shows you how to do it :)

Great job, I appreciate it DanielWilson
0
 

Expert Comment

by:riveter
ID: 25663767
OK
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

14 Experts available now in Live!

Get 1:1 Help Now