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 > Database Server Software > MySQL > How can I check cardinalities

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-05-04, 04:08
JohnStrecker JohnStrecker is offline
Registered User
 
Join Date: Sep 2004
Posts: 39
How can I check cardinalities

Hi!

I am not even sure "cardinality" is the correct name...

I have a "Cities" table with a "Country_ISO" field and a "Countries" table with a "Country_ISO" field as well. How can I (as easily as possible) check that there is indeed ONE and ONE ONLY country for each City.

Looking forward to your inspired guidance

Have great day

John in sunny Brussels
Reply With Quote
  #2 (permalink)  
Old 11-05-04, 04:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you can do this by selecting those cities which have more than one country, and seeing if any results come back
Code:
select cityname, count(distinct Country_ISO)
  from Cities
group by cityname
having count(distinct Country_ISO) > 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-05-04, 10:44
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello John,

You can add "or count(distinct Country_ISO) = 0" or replace "count(distinct Country_ISO) > 1" by "count(distinct Country_ISO) <> 1" (count() will not be negative) to Rudy's query to also get cities that are related to no country :

Code:
select cityname, count(distinct Country_ISO)
  from Cities
group by cityname
having count(distinct Country_ISO) <> 1
If you get no result, then each city is well related to one and only one country.

Regards,

RBARAER
Reply With Quote
  #4 (permalink)  
Old 11-05-04, 18:44
JohnStrecker JohnStrecker is offline
Registered User
 
Join Date: Sep 2004
Posts: 39
Thank you but I have a little doubt: I don't see the "countries" table in your code. I believe the code you gave me will indeed tell me which CITY in the "citites" table have no - or too many - Country_ISO code

but

will it tell me which cities - throught their Country_ISO code - have NO CORRESPONDING record in the "countries" code (which is what I am after)

Say for instance I have "London" with country_ISO = "UK" but in the "countries" table the Country_ISO for Great Britain is "GB"... How do I find out that London has no corresponding "country"?

I am asking because I have had the case, using ISO codes for the cities and HSBC's home code for the countries ... so I have got to check them all...

Thank you for your interest

John in Brussels late at night.
Reply With Quote
  #5 (permalink)  
Old 11-05-04, 18:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
oh, that's easy
Code:
select Cities.cityname
     , Cities.Country_ISO
  from Cities
left outer
  join Countries
    on Cities.Country_ISO
     = Countries.Country_ISO  
 where Countries.Country_ISO is null
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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