Autor Beitrag
motion
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 295

XP, Linux
D7 Prof
BeitragVerfasst: So 09.03.08 19:47 
In eine bestehende SELCT Abfrage (in einer Detail-Tabelle) brauche ich noch die Max und Min-Werte der letzten Einkaufspreise
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
SELECT
     ,DATUM
     , TEXT
     , EFFEKPREIS
     , REF0
     /*und jetzt neu die max/min Angaben*/
     , max(effekpreis), min(effekpreis)
FROM EINKAUFHISTORIE
where ref0=44879
order by  datum desc

Leider ist diese Angabe so syntaktisch nicht korrekt (Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).
Wie muss ich obiges SELECT modifizieren, damit ich die gewünschte Ausgabe bekomme?
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mo 10.03.08 10:44 
Ja die Fehlermeldung gibt dir ja schon den Hinweis drauf, was nicht stimmt.
Du kannst Min oder Max nur bei einer "aggregate function" oder in Verbindung mit einer GROUP BY Klausel nutzen und nicht in diesem Query.
Ich würde dir empfelne, dass du die MIN und MAX Werte in einem neuen Statement abfrägst.

_________________
In the beginning was the word.
And the word was content-type: text/plain.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mo 10.03.08 11:41 
Oder als Subquery/Join

_________________
Markus Kinzler.
motion Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 295

XP, Linux
D7 Prof
BeitragVerfasst: Mo 10.03.08 13:32 
Leider ist das eine Detail-Query einer Master-Detail Beziehung, welche von IBO automatisch bei Wechsel/Aktualisierung des Masters aktualisiert wird. Dadurch habe ich achf keinen direkten Zugriff auf die Where-Bedingung, die gesetzt wird (z.B. für eine Subquery). Ich brauche die Max/Min Werte bereits im Resultset dieser Query, um die Darstellung des Grids richtig zu steuern (die billigsten und teuersten EffEKpreise der letzten 2 Jahre sollen grün bzw. rot hervorgehoben werden) grüble ich jetzt noch, wie sich das am einfachsten und geschicktesten einflechten läßt.

Mal schauen ...
Wenn ich eine Lösung habe, poste ich sie hier.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mo 10.03.08 13:59 
Auch wenn man sich die Master/Detail-Abfrage hat automatisch erzeugen lassen, kann man diese manuell abändern/ergänzen.

_________________
Markus Kinzler.
motion Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 295

XP, Linux
D7 Prof
BeitragVerfasst: Mo 10.03.08 15:47 
user profile iconmkinzler hat folgendes geschrieben:
Auch wenn man sich die Master/Detail-Abfrage hat automatisch erzeugen lassen, kann man diese manuell abändern/ergänzen.

Zweifellos. Aber die Parameternamen für die Parameter sind mir hier nicht klar.
Der SELECT des SQL sieht so aus:
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
SELECT REF
     ,DATUM
     , TEXT
     , EFFEKPREIS
     , REF0
FROM EINKAUFHISTORIE

Der Masterlink heißt "EINKAUFHISTORIE.REF0=LAGERKARTE.REF"

Was da jetzt im Endeffekt zur Laufzeit für eine SQL-Anweisung draus wird, muss ich erst noch mal untersuchen. Ich brauche den Parameter für den FK, damit ich diesen in den Subqueries benutzen kann.
motion Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 295

XP, Linux
D7 Prof
BeitragVerfasst: Do 13.03.08 23:28 
So sieht jetzt meine Lösung aus ...
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
select eh.*,mm.* from einkaufhistorie eh
join (
select min(effekpreis) as MINI,max(effekpreis) as MAXI, min(ref0) as ref0temp from einkaufhistorie  where ref0=:ref
) mm
on mm.ref0temp=eh.ref0
where eh.ref0=:ref
order by eh.datum desc

Damit ist das Problem gelöst ...