» »

sql max count

sql max count

boss-tech ::

Spodaj najdem prenočišče z največ gosti. v select sem dal count (število gostov) + naziv prenočišča.
Kako lahko zgoraj pri max dobim še naziv? Ker spodnja koda mi vrže error (Error code -1, SQL state 42Y35: Column reference 'NAZIV' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions.
Line 2, column 1)

#prenocisce z najvec gosti
select max(cnt),naziv from
(
select count(naziv)as cnt, naziv from prenocisca
inner join sobe on sobe.pre_id_prenocisce = prenocisca.id_prenocisce
inner join gosti on gosti.sob_id_soba = sobe.id_soba
group by naziv
)f

WarpedGone ::

#prenocisce z najvec gosti
select naziv
from
(
select count(naziv) as cnt,
naziv
from prenocisca,
sobe,
gosti
where sobe.pre_id_prenocisce = prenocisca.id_prenocisce
and gosti.sob_id_soba = sobe.id_soba
group by naziv
order by 1 desc
)f
where rownum < 2
Zbogom in hvala za vse ribe

boss-tech ::

Order by ne pusti... ko ga odstranim pa dobim: Column 'ROWNUM' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'ROWNUM' is not a column in the target table.

WarpedGone ::

Potem pač Order by prestavi nivo višje.
Očitno tvoja baza ne pozna ROWNUM funkcije.
Zbogom in hvala za vse ribe

dice7 ::

poskusi s tem

SELECT COUNT(p.naziv), p.naziv FROM prenocisca p
INNER JOIN sobe s ON s.pre_id_prenocisce = p.id_prenocisce
INNER JOIN gosti g ON g.sob_id_soba = s.id_soba
GROUP BY p.naziv
ORDER BY COUNT(p.naziv) DESC
LIMIT 1;

Zgodovina sprememb…

  • spremenil: dice7 ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Sql poizvedba

Oddelek: Programiranje
111467 (971) zgubar
»

Podatkovne baze - SQL enostavne poizvedbe

Oddelek: Programiranje
72063 (1656) galu
»

SQL poizvedba

Oddelek: Programiranje
163275 (2620) awy
»

sql select stavek

Oddelek: Programiranje
121466 (1368) zavajon
»

MYSQL vprašanje

Oddelek: Programiranje
131797 (1412) MrBrdo

Več podobnih tem