Question

i have a query that is below:

SELECT
    o.ordernum,
    o.SCHEDATTEMPT,
    o.lastcontactdate, 
    MAX(OE.NextContactDate) AS NextContactDate
FROM orderevents OE
FULL outer join orde_ o 
ON o.ORDERID=oe.OrderId
WHERE  O.lastcontactdate is not null and SCHEDULED is null and DROPTIME is null      
ORDER by O.LASTcontactdate

When i execute this query i get the error Column 'orde_.ORDERNUM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause..

What am i doing wrong in this query?

Was it helpful?

Solution

The error message says it all, but its meaning is easier to understand if you see it.

Using an aggregate function in your select list (i.e. MAX(OE.NextContactDate)), aggregate functions need to contain all entries in your select list...

SELECT
    MAX(o.ordernum),        -- technically contained in an aggregate function - setting aside that it's probably not what you want otherwise
    MAX(o.SCHEDATTEMPT),    -- same
    MAX(o.lastcontactdate), -- same
    MAX(OE.NextContactDate) AS NextContactDate
FROM orderevents OE
FULL outer join orde_ o 
ON o.ORDERID=oe.OrderId
WHERE  O.lastcontactdate is not null and SCHEDULED is null and DROPTIME is null
ORDER by MAX(o.lastcontactdate)

...or entries in your select list that are not contained by aggregate functions need to be in a GROUP BY clause:

SELECT
    o.ordernum,
    o.SCHEDATTEMPT,
    o.lastcontactdate, 
    MAX(OE.NextContactDate) AS NextContactDate
FROM orderevents OE
FULL outer join orde_ o 
ON o.ORDERID=oe.OrderId
WHERE  O.lastcontactdate is not null and SCHEDULED is null and DROPTIME is null
GROUP BY o.ordernum, o.SCHEDATTEMPT, o.lastcontactdate -- added GROUP BY clause
ORDER by o.lastcontactdate

I suspect you really want the second fix - to add a GROUP BY clause to your query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top