?
Solved

Counting Number of Occurrences in Date Range

Posted on 2007-03-27
6
Medium Priority
?
553 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

862 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