Solved

Counting Number of Occurrences in Date Range

Posted on 2007-03-27
6
528 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 61
How to place a condition in a filter criteria in t-sql (#2)? 10 56
sql query help 2 57
Need help how to find where my error is in UFD 6 40
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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