I have three tables described below:
Area (Id, Description)
City(Id, Name)
Problem(Id, City, Area, Definition):
City references City (Id), Area references Area (Id)
I want to find the most frequent value of Area(Description) that appears in Problem for each City (Name).
Example:
Area
Id Description
1 Support
2 Finance
City
Id Name
1 Chicago
2 Boston
Problem
Id City Area Definition
1 1 2 A
2 1 2 B
3 1 1 C
4 2 1 D
Desired Output:
Name Description
Chicago Finance
Boston Support
Here's what I have tried with no success :
SELECT Name,
Description
FROM
(SELECT *
FROM Problem AS P,
City AS C,
Area AS A
WHERE C.Id = P.City
AND A.Id = P.Area ) AS T1
WHERE Description =
(SELECT Description
FROM
(SELECT *
FROM Problem AS P,
City AS C,
Area AS A
WHERE C.Id = P.City
AND A.Id = P.Area ) AS T2
WHERE T1.Name = T2.Name
GROUP BY Description
ORDER BY Count(Name) DESC LIMIT 1 )
GROUP BY Name,
Description
Thanks!