Select count / duplicates - sql

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 wouldn't be a problem if there weren't X amount of zip codes in the same city...
So basically, I just want to the city in a state to count as 1 instead of it counting the city/state 7 times because there are 2+ zip codes in that city/state...
I'm not really sure how to do this. I know I need to use count but how do I tell the mysql to only count a given city/state combo as 1?

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.

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

Will this do the trick
Select CityName, Count (Distinct State) as StateCount
From CityStateTable
Group by CityName
HAVING Count (Distinct State) > 1

Try using a select distinct
SELECT DISTINCT city, state FROM table GROUP BY city

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.

Related

Show 'MULTIPLE_VALUES' instead of LISTAGG if there are > 1 records after GROUP BY

Imagine a table below:
I want to get the total population in each country but also I'd like to see the name of a city if that city is the only city in the country.
I could run something like
select
min(Country),
listagg(City) within group as City,
sum(Population) as Population
from table1
group by Country
but what i want is ('MULTIPLE' is just an example of text I'd like to see instead of the list of cities)
How can I do that?
I haven't been able to find any solution and my only idea is to use CASE with COUNT but it won't work
P.S. Sorry for the formatting
Just count the cities or compare min and max city:
select
country,
case when min(city) = max(city) then min(city) else 'multiple' as city,
sum(population) as population
from cities
group by country
order by country;
Assuming you won't have same city appearing twice
CODE 1
WITH list_of_cities_ as (
SELECT
COUNTRY,
city,
count(city) over (partition by country) AS TOTAL_Cities,
SUM(POPULATION) over (partition by country) AS TOTAL_POPULATION
FROM table_
)
select
DISTINCT
country,
case when TOTAL_Cities > 1 THEN 'Multiple' Else city end as city,
TOTAL_POPULATION
from list_of_cities_
CODE 2
IF you are interested in getting the list of all cities, instead of keyword
"Multiple"
https://dbfiddle.uk/?rdbms=postgres_9.5&fiddle=68efe08ab72ef63afa5813925e7f99e0
SELECT
COUNTRY,
STRING_AGG(City,',') as LIST_OF_CITIES,
SUM(POPULATION) AS TOTAL_POPULATION
FROM table_
GROUP BY 1

How do I find a duplicate in SQL

I have a query that selects 3 columns. Each row row should be a unique combination of county, city,and zip. However, I have reason to believe I'm getting a duplicate somewhere. How do I find the duplicate ? COUNT() ?? This in MS SQL Server . Any help would be most appreciated. --Jason
SELECT COUNTY, CITY, ZIP
FROM MoratoriumLocations
WHERE MoratoriumID=20
ORDER BY County
You coul use group by and having
SELECT COUNTY, CITY, ZIP
FROM MoratoriumLocations
WHERE MoratoriumID=20
GROUP BY COUNTY, CITY, ZIP
HAVING COUNT(1) >1
ORDER BY County
If you want to get the full row details you can use a sub query in combination with the group by and having statements
SELECT x.*
FROM MoratoriumLocations x
INNER JOIN(
SELECT COUNTY, CITY, ZIP
FROM MoratoriumLocations
WHERE MoratoriumID=20
GROUP BY COUNTY, CITY, ZIP
HAVING COUNT(1) >1
) dups ON dups.County = x.County
AND dups.City = x.City
AND dups.Zip = x.Zip
See Preben's answer for how to find dups.
To avoid dups altogether consider creating an unique index.
I would suggest window functions:
SELECT ml.*
FROM (SELECT ml.*, COUNT(*) OVER (PARTITION BY County, City, Zip) as cnt
FROM MoratoriumLocations ml
WHERE MoratoriumID = 20
) ml
ORDER BY cnt DESC, County, City, Zip;
This will show the complete rows with duplicates, which can help you understand them better.

how to find difference between no_of_value and no_of_distinct columns values?

Let be the number of CITY entries in STATION, and let be the number of distinct CITY names in STATION; query the value of from STATION. In other words, find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
Input Format
The STATION table is described as follows:
enter image description here
where LAT_N is the northern latitude and LONG_W is the western longitude.
Use distinct in count function.
select count(city) - count(distinct city)
from station
SELECT count(city) - count(DISTINCT city) FROM station;
Do not forget to add semicolon ';' after the query
You could use having for filtering the resul on aggregated function
select city, count(*), count(distinct city)
from station
group by city
having count(*) <> count(distinct city)
If I understand correctly:
select count(city) - count(distinct city)
from station;
You would do this to get the number of duplicated values in the table. I might be more interested in the list of cities and the number of duplicates:
select city, count(*) - 1 as numdups
from station
group by city
having count(*) > 1;

SQL find repeated values

I need to identify rows where a certain value is repeated. Here is a sample table:
COUNTRY CITY
Italy Milan
Englad London
USA New York
Canada London
USA Atlanta
The query should return...
COUNTRY CITY
Englad London
Canada London
...because London is repeated. Thank you in advance for your help.
The easiest way is to use a subquery that counts the number of times each city appears (and filter to those values that appear more than once):
SELECT * FROM Cities
WHERE City in
(
SELECT City FROM Cities
GROUP BY City
HAVING COUNT(*) > 1
)
If your DBMS supports windowed aggregates.
SELECT COUNTRY,
CITY
FROM (SELECT COUNTRY,
CITY,
COUNT(*) OVER (PARTITION BY CITY) AS Cnt
FROM Cities) T
WHERE Cnt > 1
SQL Fiddle
select country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(1) > 1
)
Try it here: http://sqlfiddle.com/#!3/e9b1a/1
Or if the same city & country combo appears twice and you're only interested where the countries are different:
select distinct country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(distinct country) > 1
)
Try it here: http://sqlfiddle.com/#!3/2dfaa/2
This one works. Got it from my wife (she finally had time to look into this). Thought you might be interested.
SELECT * FROM Cities
WHERE City in ( select city
from (SELECT City,
count(distinct country)
FROM Cities
GROUP BY City
HAVING count(distinct country) > 1) a )

Counting occurrences of distinct multiple columns in SQL

I'm trying to count the occurrences of a distinct set of cities and countries in a user table.
The table is set out similar to:
userid city country
------ --------- --------------
1 Cambridge United Kingdom
2 London United Kingdom
3 Cambridge United Kingdom
4 New York United States
What I need is a list of every city, country pair with the number of occurrences:
Cambridge, United Kingdom, 2
London, United Kingdom, 1
New York, United States, 1
Currently I run an SQL query to get the distinct pairs:
$array = SELECT DISTINCT city, country FROM usertable
then read it into an array in PHP, and loop through the array, running a query to count each occurrences for each row in the array:
SELECT count(*) FROM usertable
WHERE city = $array['city']
AND country = $array['country']
I'm assuming my scant grasp of SQL is missing something - what would be the correct way to do this, preferably without the intervention of PHP?
select city, country, count(*)
from usertable
group by city, country
What you need is a group by:
Select city, country, count(*) as counter
from usertable
group by city, country
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT concat(city, country) FROM tablename
) as baseview;
if you want city and country preformated, or
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT city, country FROM tablename
) as baseview;
if not.