Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sql Select...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-03, 08:14
thomaskeegan thomaskeegan is offline
Registered User
 
Join Date: Apr 2003
Posts: 7
Red face Sql Select...

TABLE: TOWNS
=============================
COUNTY = TOWN
=============================
LIMERICK = ARDAGH
LONGFORD = ARDAGH
TIPPERARY = BALLINA
MAYO = BALLINA
WICKLOW = BALLINACLASH
TIPPERARY = BALLINACLOUGH
ROSCOMMOM = ATHLEAGUE
ROSCOMMON = ATHLONE
WESTMEATH = ATHLONE
CARLOW = HACKETSTOWN
WATERFORD = HALFWAYHOUSE
MEATH = HAYES
DUBLIN = HAZELHATCH
=============================
ok there's my table

I just don't know how to do it...

I just want to select the COUNTY and TOWN where the
TOWN appears more that once. Kinda like this. Can anyone
help? what is the SELECT statement?

SELECT RESULT
=============================
COUNTY = TOWN
=============================
LIMERICK = ARDAGH
LONGFORD = ARDAGH
TIPPERARY = BALLINA
MAYO = BALLINA
ROSCOMMON = ATHLONE
WESTMEATH = ATHLONE
=============================


I'm not totally new to SQL but I would like to learn more
advanced SQL, does anyone know goodf tutorial sites?


thanks
Reply With Quote
  #2 (permalink)  
Old 04-29-03, 08:28
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Sql Select...

It helps to break the problem down into parts:

1) Which TOWNs appear more than once.

Easy:

SELECT town FROM towns
GROUP BY town HAVING COUNT(*) > 1;

SELECT RESULT
=============================
TOWN
=============================
ARDAGH
BALLINA
ATHLONE
=============================

2) Get all records where TOWN appears in result of (1):

SELECT county, town
FROM towns
WHERE town IN
( SELECT town FROM towns
GROUP BY town HAVING COUNT(*) > 1
);
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 04-29-03, 08:29
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,560
you need a subquery for the towns --
Code:
select county, town from towns where town in ( select town from towns group by town having count(*) > 1 )
rudy
http://r937.com/
Reply With Quote
  #4 (permalink)  
Old 04-29-03, 08:30
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,560
darn, beat me by a minute...
Reply With Quote
  #5 (permalink)  
Old 04-29-03, 08:32
thomaskeegan thomaskeegan is offline
Registered User
 
Join Date: Apr 2003
Posts: 7
Cheers

I knew how to do

SELECT town FROM towns GROUP BY town HAVING COUNT(*) > 1

but my problem was that I not to sure how to do subqueries.

The book on SQL I have is crap. Sams Teach Yourself in 21 Days. Do you or anyone know any good sites on subqueries?

Thanks andrewst.
Reply With Quote
  #6 (permalink)  
Old 04-29-03, 08:59
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Quote:
Originally posted by thomaskeegan
Do you or anyone know any good sites on subqueries?
Thanks andrewst.


Not really - my SQL studying days are long in the past (pre-WWW). I refer to on-line Oracle manuals for syntax of new/obscure features.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #7 (permalink)  
Old 04-29-03, 09:32
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,560
there are several good tutorial sites in my SQL Links --

http://r937.com/links.cfm?links=sql

sorry, the links are uncategorized, you'll just have to scroll through them

categorization is on my To Do list but not near the top...


rudy
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

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