Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let say table having 2 columns i.e. State and City. Values are like:

State: S1, S2, S3
City: C1, C2, C1

So the output should be State: S1 & S3

What I have tried:

I tried self inner join but did not work.
Posted
Updated 8-Apr-21 23:54pm
Comments
Patrice T 9-Apr-21 0:27am    
Show your code.
mohit Gupta 2021 9-Apr-21 0:44am    
select State from TableState A
INNER JOIN TableState B
ON A.City = B.City
Patrice T 9-Apr-21 0:49am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

I may as well put my punt in here as well as your own code wasn't too far off from a solution. With a minor addition to the ON clause and the addition of DISTINCT you get the results you are after
SQL
select distinct a.state
from TableState a
inner join TableState b on a.city = b.city and a.[state] <> b.[state]
That and a.[state] <> b.[state] also handles the two cities with the same name in the same state scenario.

The fun starts when you also want to know which cities are in multiple states as you will then start to get results like
state	city	state	city
state1	city3	state3	city3
state3	city3	state1	city3
Both rows are stating the same thing. But that's not what you asked for so I'll not start digging too deep!
 
Share this answer
 
Comments
Maciej Los 9-Apr-21 8:38am    
5ed!
The simple approach should work:
SQL
SELECT DISTINCT
    State
FROM
    TableState As a
WHERE
    Exists
    (
        SELECT 1
        FROM TableState As b
        WHERE b.City = a.City
        And b.State != a.State
    )
;
 
Share this answer
 
First you need to find which City names exists more than once.
SQL
SELECT  City
FROM    TableState
GROUP BY City
HAVING COUNT(DISTINCT State) > 1

The distinct is needed in case you have the same city name more than once in a state.

Then join this as a subquery to find which states the cities belong to.
 
Share this answer
 
SQL
SELECT city,
STUFF((
SELECT ', ' + cast(state as varchar(max) )
FROM #temp
WHERE (city = a.city)
FOR XML PATH (''))
,1,2,'') AS state
FROM #temp a
GROUP BY city
 
Share this answer
 
Comments
CHill60 9-Apr-21 7:06am    
Nicely formats the results but unfortunately does not cater for the 2 cities of same name in same state scenario
Arfat M 9-Apr-21 7:45am    
Thank you.... To avoid duplicates ->SELECT distinct ', ' + cast(state as varchar(max) )
CHill60 9-Apr-21 9:22am    
Not quite. If you consider this sample data
declare @TableState table ([state] nvarchar(50), [city] nvarchar(50));
insert into @TableState ([state],[city]) values('state1','city1'),('state1','city2'),('state1','city3'),
('state2','city1'),
('state3','city3'),
('state4','city4'),('state4','city4'),('state5','city1'),
('state5','city2');
then the expected results are
city1 - state1, state2, state5
city2 - state1, state5
city3 - state1, state3
but your code returns
city	state
city1	state1, state2, state5
city2	state1, state5
city3	state1, state3
city4	state4
Arfat M 14-Apr-21 8:07am    
Kindly check the below code its correct only:
SELECT city,
STUFF((
SELECT distinct  ', ' + cast(state as varchar(max) )
FROM #temp
WHERE (city = a.city)
FOR XML PATH (''))
,1,2,'') AS state
FROM #temp a
GROUP BY city
CHill60 16-Apr-21 10:49am    
As per my comment above. Returns state4 from my sample data - this is where state4 has two cities of the same name, but the requirement is to list states have a common city.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900