-3
I have the following query
with a subquery
in the clause FROM
and I intend to get the same result but using this subquery
in the clause WHERE
.
SELECT base.nome as Aquario, base.localizacao, base.nome_cientifico
FROM (SELECT A.id_aquario, A.nome,A.localizacao, E.id_especie,E.nome_cientifico,count(1) as quantidade
FROM Aquario A INNER JOIN Especime Espe ON Espe.id_aquario = A.id_aquario
INNER JOIN Especie E ON E.id_especie = Espe.id_especie
GROUP BY A.id_aquario, E.id_especie) base
WHERE NOT EXISTS(SELECT 1
FROM Especime Espe2, Especie E2
WHERE Espe2.id_aquario = base.id_aquario AND E2.id_especie <> base.id_especie AND E2.id_especie = Espe2.id_especie
GROUP BY E2.id_especie
HAVING count(1) > base.quantidade);
The purpose of this query is to indicate the scientific names, location and name of the aquarium, of the species in the largest number in this aquarium.
Tables required:
-- A (name,Num2, local)
insert into A values ('Favela',1,'WE');
insert into A values ('Lamosa',2,'NA');
insert into A values ('Luz',3,'S0');
-- C (num5,num3,name2,num4)
insert into C values (2,1,'Cao',9);
insert into C values (1,2,'Gato',8);
insert into C values (1,3,'Golfinho',7);
insert into C values (3,4,'Peixe',11);
-- D (Num5, Num2, perce)
insert into D values(1,1,100);
insert into D values(2,1,100);
insert into D values(1,3,10);
insert into D values(3,3,20);
insert into D values(2,2,60);
insert into D values(3,1,50);
insert into D values(2,3,70);
insert into D values(3,2,40);
insert into D values(2,1,0);
Dude, you gotta tell me what the point of
query
right. If you keep to yourself the information you think someone will be struggling to help you?– Sorack
I would have, but it’s not showing up :/ And I tried to edit it but it’s not showing up either
– Força Chape
Is already corrected
– Força Chape
The statement "The purpose of this query is to indicate the scientific names, location and name of the aquarium, of the species in the largest number in this aquarium." is the same as http://answall.com/questions/169108/70 which was already duplicated?
– Bacco
What I want is to change the code above to the WHERE clause , I do not think it is equal , the answer that it is not what I want
– Força Chape
It would be good to explain the difference then, because if the answer fits in that, it is duplicated in any way. The fact that the answer does not meet does not necessarily justify another posting. It would be very clear to highlight the different requirements (actual requirements), so avoid qq misunderstanding or closure.
– Bacco
Yes, it is written in the question and the title , the requirements is that there is no subquery’s in the FROM clause but in the WHERE clause , it is different from the other question
– Força Chape
And another, in the
INNER JOIN
cansubquery
? It’s not very clear– Sorack
Well, it seems to me that the problem lies in the formulation of the first question already. However, it is suggested that in the next questions we should detail better and more clearly to avoid a series of similar posts, which can even help you obtain more complete solutions from the beginning, and at the same time take better advantage of the time of those who answer.
– Bacco
@Sorack The only requirement to change the above code is only if the FROM clause has no subquery.
– Força Chape
@Bacco Ok I’m beginning to understand better how to ask the questions
– Força Chape
You don’t want to view this case?
– Sorack
@Sorack view despite being better for this case , the ideal was to just use the " ideas " that are in the code above
– Força Chape