Select count / duplicates

后端 未结 5 836
悲&欢浪女
悲&欢浪女 2021-01-18 00:53

I have a table with all U.S. zip codes. each row contains the city and state name for the zip code. I\'m trying to get a list of cities that show up in multiple states. This

相关标签:
5条回答
  • 2021-01-18 01:00
    SELECT City, Count(City) As theCount
    FROM (Select City, State From tblCityStateZips Group By City, State) As C
    GROUP By City
    HAVING COUNT Count(City) > 1
    

    This would return all cities, with count, that were contained in more than one state.

    Greenville 39
    Greenwood 2
    GreenBriar 3
    etc.

    0 讨论(0)
  • 2021-01-18 01:08

    First group on state and city, then group the result on city:

    select City
    from (
      select State, City
      from ZipCode
      group by State, City
    ) x
    group by City
    having count(*) > 1
    
    0 讨论(0)
  • 2021-01-18 01:10

    You probably should have created a separate table for zip codes then to avoid the duplication.

    You want to look into the GROUP BY Aggregate.

    0 讨论(0)
  • 2021-01-18 01:11

    Try using a select distinct

    SELECT DISTINCT city, state FROM table GROUP BY city
    
    0 讨论(0)
  • 2021-01-18 01:16

    Will this do the trick

    Select CityName, Count (Distinct State) as StateCount
    From CityStateTable
    Group by CityName
    HAVING Count (Distinct State) > 1
    
    0 讨论(0)
提交回复
热议问题