Solved

SQL help on a complicate query

Posted on 2013-01-18
2
278 Views
Last Modified: 2013-01-18
I have a table that contains vehicle information for owners.  The table has an ownerid to map vehicles to owners.  What I need is a query that will tell me how many owners have more than 3 vehicles.  I don't need the details, just the count.  So for example:

Vehicle Table
VID       OID
1           100
2           100
3           200
4           300
5           300
6           300
7           300
8           400
9           400
10         400
11         500
12         500
13         500
14         500
15         500
16         500

The SQL would return 2 - OID 300 and OID 500 have more than 3 vehicles.  Can anyone help me with this?  We are using MS SQL Server 2005.
0
Comment
Question by:dyarosh
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38793760
select OID, count(VID)
from table
group by OID
having count(VID) >= 3


or are you looking for

select count(OID) from (
      select OID
      from table
      group by OID
      having count(VID) >= 3
) a

and you can also try

select count(OID) from (
      select OID, row_number() over (partition by OID order by OID) rn
      from table
) a
where rn >= 3
0
 

Author Closing Comment

by:dyarosh
ID: 38793772
Thank you.  I knew I had to use a Group BY but didn't know where.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

18 Experts available now in Live!

Get 1:1 Help Now