If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Getting records that only exist once.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-14-04, 07:10
phillzee phillzee is offline
Registered User
 
Join Date: Dec 2004
Posts: 5
Getting records that only exist once.

Heres one that I'm too dumb to fathom out - have spent hours on it so any help would be greatly received :-)

I have a database that lists company names. What I need to do is only select those companies that appear in the table once. - bear in mind a distinct wont work for this.

So far I have tried this:

select total,num_dels,company from nominput where (select count(company) from nominput) = 1

But this doesn't return a thing - is this the right way to do it ?

Many Thanks
Phil
Reply With Quote
  #2 (permalink)  
Old 12-14-04, 07:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select total
     , num_dels
     , company 
  from nominput 
 where company in 
     ( select company
         from nominput
       group by company
       having count(*) = 1 )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-14-04, 10:57
phillzee phillzee is offline
Registered User
 
Join Date: Dec 2004
Posts: 5
Many thanks - works great :-)

I guess I need to do some more reading up on group by, I dont understand why it works but it does so thanks very much, greatly appreciated.

Phil
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On