![]() |
Datenbank: MYSQL • Version: 5.1 • Zugriff über: Konsole
HAVING mit MAX (alias)
Hallo,
warum funktioniert folgende query nicht?
Code:
Idee ist, dass ich die Einträge finde, die in der Spalte anz (alias für count(wert)) den maximalen Wert haben.
SELECT id, count(wert) as anz
GROUP BY id HAVING (anz = MAX(anz)) Interessanterweise werden alle Werte gezeigt, wenn ich schreibe
Code:
anz hat aber für alle Datensätze einen Wert > 0 !?
HAVING (Max(anz) = 0)
|
AW: HAVING mit MAX (alias)
Keine Ahnung wie weit MySQL inzwischen mit Subselects ist, geht sowas?
Code:
HAVING (Count(*) = (Select MAX(ANZ) from (SELECT id, count(wert) as anz
from ?? GROUP BY id) )) |
AW: HAVING mit MAX (alias)
So sollte das eigentlich funktionieren
SQL-Code:
aber das geht auch:
SELECT * FROM (
SELECT id, count(wert) as anz GROUP BY id ) as tmp WHERE anz = MAX( anz );
SQL-Code:
SELECT * FROM (
SELECT id, count(wert) as anz GROUP BY id ) as tmp ORDER BY anz DESC LIMIT 1; |
AW: HAVING mit MAX (alias)
Hallo Sir Rufo,
so hab ich es auch schon versucht. Das Problem ist aber, dass mir immer nur ein Datensatz ausgegeben wird, obwohl mehrere den maximalen Wert haben. bei anz = Max(anz) muss die HAVING Clause benutzt werden, weil where keine Aggregatfunktionen erlaubt |
AW: HAVING mit MAX (alias)
Warum es nicht funktioniert, kann ich dir auch nicht sagen, aber ich habe mal eben ein paar Varianten durchprobiert, und folgendes scheint zu funktionieren:
SQL-Code:
select * from Tabelle join (select max(Spalte) as maximum from Tabelle) as tmp on maximum=Spalte
|
AW: HAVING mit MAX (alias)
Hallo Philip,
ich benötige ja das Maximum einer (alias Spalte), die wiederum eine Aggregatfunktion nutzt. |
AW: HAVING mit MAX (alias)
Hallo,
mir ist es nur auf diese Art gelungen (allerdings kein MySQL):
Code:
Mit Having scheint es nicht unbedingt zu gehe(?)
select id, anz from (
select id, count(wert) as anz from tabelle group by id ) a where anz = ( select max(anz) from ( select id, count(wert) as anz from tabelle group by id ) ) |
AW: HAVING mit MAX (alias)
Zitat:
Ich habe folgende Varianten anzubieten:
SQL-Code:
select
id, count(*) as anz, ( select max(anz) from (select count(*) as anz from Tabelle group by id) as tmp ) as maximum from Tabelle group by id having anz=maximum
SQL-Code:
select id, count(*) as anz
from Tabelle group by id having ( select max(anz) from (select count(*) as anz from Tabelle group by id) as tmp ) = anz |
AW: HAVING mit MAX (alias)
die von mir ursprünglich vorgeschlagene Lösung
Code:
funktioniert unter MSSQL.
SELECT id, count(*) as anz
from test GROUP BY id HAVING Count(*) = (Select MAX(ANZ) from (SELECT id, count(*) as anz from test GROUP BY id) i) Funktioniert sie unter MySQL nicht? |
AW: HAVING mit MAX (alias)
Zitat:
Zitat:
Der Weg führt imho nur über eine 'temporäre Tabelle', d.h. die Aggregat-Tabelle mit den Spalten id und Anz (also Count(*)). Davon möchstest Du die Einträge, deren Wert Anz am größten ist. Bei meiner Arbeit mit MSSQL ist mir aufgefallen, das die Verwendung temporärer Tabellen häufiger wesentlich performanter ist, leichter verständlich und schneller zum Ziel führt:
SQL-Code:
Kennt mySQL temporäre Tabellen oder Tabellenvariablen? ....*gugel* ja, geht
select Id, Count(*) as Anz into #temp from myTable group by Id
select * from #temp where Anz = (select max(Anz) from #temp)
SQL-Code:
Hier scheint es so zu sein, das eine Lösung mit einem Statement nur über den Klimmzug geht, die Aggregattabelle 'Select id, count(*)...' mehrfach zu verwenden. Schafft der Compiler es, dies zu erkennen und das Aggregat nur 1x durchzuführen?
create Temporary table Anzahl (id int, Anz int)
insert into Anzahl select id, count(*) from myTable select * from Anzahl where Anz = (select max(Anz) from Anzahl) DROP TABLE Anzahl |
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:03 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz