2016-03-26 21 views
0
SELECT nmemail as order_email, 
     dtorder, 
     vlOrder, 
     cohorts.cohortdate 
FROM factorderline 
     JOIN (SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate FROM factorderline GROUP BY cohort_email limit 5) cohorts 
ON order_email= cohort_email limit 5; 

ERROR: column "order_email" does not existPostgres SQL - Spalte existiert nicht

Was das Problem mit dieser Abfrage?

+0

err, mist, traurig über die zufällige Abstimmung zu schließen. –

Antwort

1

Das Problem ist höchstwahrscheinlich, dass die Definition des Spaltenalias zum Zeitpunkt der Auswertung des Joins nicht analysiert wurde. verwenden, um die tatsächlichen Spaltennamen statt:

SELECT nmemail as order_email, 
     dtorder, 
     vlOrder, 
     cohorts.cohortdate 
FROM factorderline 
JOIN (
    SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate 
    FROM factorderline 
    GROUP BY cohort_email limit 5 
) cohorts ON nmemail = cohort_email 
limit 5; 

Auch wenn limit verwendet, sollten Sie wirklich eine order by-Klausel verwenden.

Aus der Dokumentation:

When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows.

1

Das Problem ist, dass die Ausgangsspaltennamen können nicht in Joins verwendet werden.

Von the documentation:

An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.

Verwandte Themen