Forum Discussion
cheid_4838
2 years agoHelper IV
Aggregation Error Message
I need to pull the company id related to the last billable stop in a series of stops(deliveries) where the following conditions are met: The order number is the same in both the stops table and...
- Anonymous2 years ago
Hi cheid_4838 ,
It looks like you're running into an issue with using an aggregate function ('MAX()') directly in the 'WHERE' clause, which is not permitted in SQL. Please update your SQL statement as below and check if it can work or not...
SELECT s.cmp_id AS ReceiverID FROM stops s (nolock) JOIN ( SELECT ord_hdrnumber, MAX(stp_sequence) AS max_sequence FROM stops WHERE stp_event IN ('LUL', 'LLD', 'DRL', 'HPL') GROUP BY ord_hdrnumber ) max_stops ON s.ord_hdrnumber = max_stops.ord_hdrnumber AND s.stp_sequence = max_stops.max_sequenceBest Regards
Anonymous
2 years agoNot applicable
Hi cheid_4838 ,
It looks like you're running into an issue with using an aggregate function ('MAX()') directly in the 'WHERE' clause, which is not permitted in SQL. Please update your SQL statement as below and check if it can work or not...
SELECT s.cmp_id AS ReceiverID
FROM stops s (nolock)
JOIN (
SELECT ord_hdrnumber, MAX(stp_sequence) AS max_sequence
FROM stops
WHERE stp_event IN ('LUL', 'LLD', 'DRL', 'HPL')
GROUP BY ord_hdrnumber
) max_stops
ON s.ord_hdrnumber = max_stops.ord_hdrnumber
AND s.stp_sequence = max_stops.max_sequence
Best Regards