SQL-Abfrage

Hallo,

ich möchte in einer SQL-Abfrage alle Kunden bekommen, die in einem bestimmten Zeitraum das erste mal bestellt haben.

SELECT oxuser.oxcustnr AS Kundennummer, concat(oxuser.oxfname,' ',oxuser.oxlname) AS Name , min(oxorder.oxsenddate) AS Datum 
FROM oxuser, oxorder 
WHERE oxuser.oxid = oxorder.oxuserid AND min(oxorder.oxsenddate) >= '2000-01-01 00:00:00' AND min(oxorder.oxsenddate) <= '2014-10-22 14:04:19' 
GROUP BY Kundennummer

Leider bekomme ich : #1111 - Invalid use of group function

Kann mir jemand weiterhelfen?

Danke
Thoni

guck dir mal das erste Suchergebnis bei google an

SELECT oxuser.oxcustnr AS Kundennummer, CONCAT( oxuser.oxfname, ’ ', oxuser.oxlname ) AS Name, MIN( oxorder.oxsenddate ) AS Datum
FROM oxuser JOIN oxorder ON oxuser.oxid = oxorder.oxuserid
GROUP BY Kundennummer
HAVING MIN( oxorder.oxsenddate ) >= ‘2000-01-01 00:00:00’ AND MIN( oxorder.oxsenddate ) <= ‘2014-10-22 14:04:19’


SELECT distinct(oxuser.oxcustnr) AS Kundennummer, concat(oxuser.oxfname,' ',oxuser.oxlname) AS Name , min(oxorder.oxsenddate) AS Datum 
FROM oxuser, oxorder 
WHERE oxuser.oxid = oxorder.oxuserid 
AND (oxorder.oxsenddate BETWEEN '2000-01-01 00:00:00' AND '2014-10-22 14:04:19') 
GROUP BY Kundennummer

[QUOTE=jschindler;151472]


SELECT distinct(oxuser.oxcustnr) AS Kundennummer, concat(oxuser.oxfname,' ',oxuser.oxlname) AS Name , min(oxorder.oxsenddate) AS Datum 
FROM oxuser, oxorder 
WHERE oxuser.oxid = oxorder.oxuserid 
AND [B](oxorder.oxsenddate BETWEEN '2000-01-01 00:00:00' AND '2014-10-22 14:04:19')[/B] 
GROUP BY Kundennummer

[/QUOTE]

Damit fragt man doch nicht das erste Bestelldatum ab, oder?

Hat noch jemand eine Idee?

Bei jschindlers Beispiel bekomme ich die erste Bestellung im angegebenen Zeitraum.

Ich benötige jedoch nur die User, die im angegebenen Zeitraum das erste mal bestellt haben.

Ich hatte auch schon mal versucht, das ganze in Zwei Abfragen zu erstellen.
Zuerst werden alle erste Bestellungen der Kunden abgefragt und dann wird geguckt, ob die Bestellungen im angegebenen Zeitraum lagen.

Schon die erste Abfrage funktioniert nicht - ich bekomme nur einen Datensatz der absolut ersten Bestellung:

SELECT oxid, oxuserid, min( oxorder.oxsenddate ) AS Datum FROM oxorder WHERE oxorder.oxsenddate > '2000-01-01 00:00:00' GROUP BY 'oxuserid'

Mit oxuserid statt ‘oxuserid’ …
… werden mehr Datensätze angezeigt, aber nicht die ersten Bestellungen der User. :confused:

Probier’s mal so:

SELECT *
FROM oxuser as a
JOIN
(
SELECT min(oxorderdate) as oxorderdate, oxuserid as oxuserid
FROM oxorder as b WHERE oxorderdate >= '2000-01-01 00:00:00' AND oxorderdate <= '2014-10-22 14:04:19'
group by oxuserid
) b ON a.oxid = b.oxuserid

Datum ist anzupassen…

oxsenddate ist wann die Bestellung an den Kunden geschickt wurde

Danke - jetzt scheint es zu funktionieren.

Das mit dem Versanddatum ist schon OK - ich hatte mich falsch ausgedrückt.

Dann einfach ändern:

SELECT *
FROM oxuser as a
JOIN
(
SELECT min(oxsenddate) as oxsenddate, oxuserid as oxuserid
FROM oxorder as b WHERE oxsenddate >= '2000-01-01 00:00:00' AND oxsenddate <= '2014-10-22 14:04:19'
group by oxuserid
) b ON a.oxid = b.oxuserid

:wink:

[QUOTE=foxido.de;154214]Dann einfach ändern:
[/QUOTE]

Ich hatte mit meiner Anmerkung auf vanilla thunder reagiert. :smiley: