Solved

Counting Number of Occurrences in Date Range

Posted on 2007-03-27
6
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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