Solved

SQL help on a complicate query

Posted on 2013-01-18
2
295 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
[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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

615 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