SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get a distinct value

    Hi

    I have a table which contains towns and municipalities in Spain.

    I want users to select a distinct geonameid which corresponds to a town or municipality.

    If the municipality asciiname is the same as a town name, i just want the municipality.

    This query is incorrect as it returns both the town and municipalities.

    select distinct geonameid, asciiname from geo_import where (feature_code = 'ADM3' OR feature_code = 'PPL') AND province_id = '2509951' order by asciiname asc

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    how can you tell the difference between a town and a municipality?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A municipality has the feature_code ADM3
    A town has the feature_code PPL

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    could you give an example of where the municipality is the same as the town?

    i'd like to see the entire rows
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the slow reply.

    If the records have the same asciiname I just want the municipality.

    They are separate rows in the table

    geonameid
    asciiname
    feature_code

    1 calasparra ADM3 (the municipality record)
    2 calasparra PPL (the town record)

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    untested:
    Code:
    SELECT geonameid
         , asciiname 
      FROM geo_import AS muni
     WHERE feature_code = 'ADM3' 
       AND province_id = '2509951'
    UNION ALL
    SELECT geonameid
         , asciiname 
      FROM geo_import AS city
    LEFT OUTER
      JOIN geo_import AS muni
        ON muni.asciiname = city.asciiname
     WHERE city.feature_code = 'PPL' 
       AND city.province_id = '2509951'
       AND muni.asciiname IS NULL
    ORDER 
        BY asciiname asc
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •