Skip to main content
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
cheid_4838
Contributor

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:
  1. The order number is the same in both the stops table and order header table.
  2. The stops must have billable codes LUL, LLD, DRL, and HPL in the stops event column (s.stp_event).
  3. Since I want the company id related last billable stop I want the MAX stop sequence (s.stp_sequence) number which identifies each stop (1, 2, 3, etc.).

I am having trouble getting the aggregation to work in the statement below. Am I missing something with this logic. Your help is greatly appreciated.

 

(SELECT cmp_id from stops s (nolock) where s.ord_hdrnumber = o.ord_hdrnumber and s.stp_event in ('LUL','LLD','DRL', 'HPL') and s.stp_sequence = MAX(s.stp_sequence)) as ReceiverID

 

Error Message: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

1 ACCEPTED SOLUTION
Anonymous
Not 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

View solution in original post

2 REPLIES 2
Anonymous
Not 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

marcinkozak1995
New Contributor

Error Aggregation Handler (EAH) - 7F2F

Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (8,767)