I am trying to make a query that selects all the drivers that were fined in the same place where another driver was fined.
SELECT DISTINCT(c.IdSofer), s.NumeSofer
FROM Soferi s
INNER JOIN contraventii c ON c.IdSofer= s.IdSofer
HAVING (SELECT DISTINCT IdLocContr FROM Contraventii c
INNER JOIN Soferi s ON c.IdSofer=s.IdSofer
INNER JOIN Localitati l ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer='Maneta Gheorghe' AND l.DenLoc='Pocreaca' AND l.Jud='IS' GROUP BY IdLocContr) = c.IdLocContr
GROUP BY s.NumeSofer, c.IdLocContr, c.IdSofer;
This is what I tried but I get the error message
ORA-01427: single-row subquery returns more than one row.
If I run only only the SELECT statement between parentheses it works fine. It shows the id of cities (two cities) where this driver was fined. But if I try to run the code as I wrote here I get this error.
EDIT
This is the SQL from my comment...
SELECT DISTINCT(c.IdSofer)
,s.NumeSofer
FROM Soferi s INNER JOIN contraventii c
ON c.IdSofer= s.IdSofer
GROUP BY s.NumeSofer
,c.IdLocContr
,c.IdSofer
HAVING (SELECT DISTINCT IdLocContr
FROM Contraventii c INNER JOIN Soferi s
ON c.IdSofer=s.IdSofer INNER JOIN Localitati l
ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer = 'Maneta Gheorghe'
AND l.DenLoc = 'Pocreaca'
AND l.Jud = 'IS'
GROUP BY IdLocContr) = c.IdLocContr
HAVING
clause must follow aGROUP BY
. In the SQL you posted,HAVING
appears beforeGROUP BY
. – Abrawhere
clause I suspect. Definitely not a typicalhaving
. – shawnt00