Autor Beitrag
moloch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 451

Win 2000
D5 Prof
BeitragVerfasst: Mo 27.06.05 12:43 
Hallo,
ich habe eine Tabelle in der Zusammenhängene Sätze vorkommen.Damit meine ich dass Sie die gleiche ID haben.
also der Satz mit der ID 7 kommt z.B. 5 mal vor.
jetzt gibt es zu dem Satz noch eine Datumsspalte.

Ich möchte nun mit einer SQL den Satz herausbekommen der in der Datumsspalte das höchste Datum hat.
Kann da jemand helfen.

mfg
moloch
sango
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 236

WinXP Home
Delphi 2005 Pro
BeitragVerfasst: Mo 27.06.05 13:38 
ausblenden Quelltext
1:
SELECT *,MAX(datumsspalte) AS maxdate FROM tabelle WHERE id = 7 GROUP BY id HAVING datumsspalte = maxdate					

so in der richtung würd ich das probieren.
moloch Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 451

Win 2000
D5 Prof
BeitragVerfasst: Mo 27.06.05 14:51 
hey,
also ich will von allen gleichen ID's den Satz mit dem höchsten Datumswert haben. nicht nur von der ID 7. Ausserdem hat die SQL nicht funktioniert.

ich probier die ganze zeit sowas in der art:

ausblenden Quelltext
1:
2:
3:
select a.SatzID,a.VersionAb,a.Firma from DatenSatz a,(select SatzID,max(VersionAb) as MaxDatum group by SatzID) 
where a.SatzID=SatzID
and a.VersionAb=maxresult.maxdatum
sango
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 236

WinXP Home
Delphi 2005 Pro
BeitragVerfasst: Mo 27.06.05 15:05 
nicht gleich patzig werden....

also es geht auch ganz kompliziert. kommt nämlich drauf an ob deine mysql version überhaupt subqueries benutzen kann.

wir benutzen für sowas dieses:

ausblenden Quelltext
1:
2:
3:
CREATE TEMPORARY TABLE tmp (unique_id INT(10) DEFAULT '0' NOT NULL)
INSERT INTO tmp SELECT MAX(unique_id) FROM tabelle GROUP BY deine_text_id
SELECT * FROM tabelle, tmp WHERE tabelle.unique_id = tmp.unique_id

und das funktioniert.
wobei du als id ein eindeutiges feld nehmen musst, ist ja klar, also nicht dieses feld welches mehrmals vorkommt.
Wir gehen dabei nicht nach dem datum sondern nach der auto_increment Id.
aber das mal als ansatz. dieses funktioniert schonmal.-
moloch Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 451

Win 2000
D5 Prof
BeitragVerfasst: Di 28.06.05 07:24 
danke erstmal für deine hilfe.patzig sollte es nicht sein.

schau mal mit mssql krieg ich es so hin:

ausblenden Quelltext
1:
2:
3:
4:
SELECT a.Nr,a.Datum FROM Tabelle a,
(SELECT max(Datum) as Maxdatum,Nr FROM Tabelle GROUP BY Nr) maxresults
WHERE a.Nr = maxresults.Nr
AND a.Datum = maxresults.Maxdatum


das muss doch irgendwie ähnlich sein!?