0
I’m trying to solve the problem to find the state with the largest number of cities starting with saint/saint. For that I realized this view:
CREATE VIEW SANT AS (
SELECT E.NOME ESTADOS, M.NOME CIDADES, COUNT('CIDADES') AS CNT FROM ESTADO E, MUNICIPIO M
WHERE E.CODESTADO = M.CODESTADO
AND M.NOME LIKE 'Santa%' OR M.NOME LIKE 'Santo%'
GROUP BY M.NOME, E.NOME
);
after that, I have to sum with Count and take the maximum value obtained. but when I do:
select estados, sum(cnt) as soma from sant
where soma = (select max('soma') from sant)
group by estados
of the error because the sum.... and when I do
select estados, sum(cnt) as soma from sant
where cnt= (select max(cnt) from sant)
group by estados
the value returned is 8 for all states.
I was able to solve the problem with another view and a select, but I know that if only a select aligned would be easier the database would have to spend a lower cost. The question is how could I do it?
follows below as I got the resolution.
CREATE VIEW SOMA_SANT AS (
SELECT ESTADOS, SUM(CNT) SOMA FROM SANT
GROUP BY ESTADOS
);
SELECT ESTADOS FROM SOMA_SANT
WHERE SOMA = (SELECT MAX(SOMA) FROM SOMA_SANT)
Select * from Sant order by cnt desc , the view already totals.
– Motta
Thus returns the number of cities with that name per state. for example: Indian saint in Tocantins has 4. but I want the state with the largest number of cities with these saint names. so I need to do the sum per state and then take the max.
– vinicius
Take M.NAME CITIES from view , try so.
– Motta
Keep telling it the same way
– vinicius
CREATE VIEW SANT AS ( SELECT E.NAME STATES, COUNT('CITIES') AS CNT FROM STATE E, MUNICIPIO M WHERE E.CODESTADO = M.CODESTADO AND M.NAME LIKE 'Santa%' OR M.NAME LIKE 'Santo%' GROUP BY E.NAME );
– Motta