-1

I am trying to get the SUM of a column in my MySQL query

SELECT pi.*
     , pr.EK2
     , pr.EK3
     , SUM(P_OrderTotal) as SUM_P_OrderTotal 
  FROM pixi pi 
  LEFT 
  JOIN konditionen pr 
    ON pi.P_EAN = pr.EAN 
 WHERE pi.P_OrderDate >= '2021-03-01' 
   AND pi.P_OrderDate <= '2021-03-31';

This gives me:

In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'table.pi.P_OrderNr'; this is incompatible with sql_mode=only_full_group_by

The query runs fine without the SUM(P_OrderTotal) as SUM_P_OrderTotal part. How would I solve this?

1
  • 1
    'How would I solve this?' - hard to say , you haven't told us what you are trying to achieve or provided sample data and desired output. and it's not obvious where p_order_total comes from.
    – P.Salmon
    Commented Mar 22, 2021 at 7:41

2 Answers 2

0

You must use a group by statement as the error message says. The GROUP BY statement is often used with aggregate functions (COUNT(), SUM(), AVG()) to group the result-set by one or more columns.

0

This will return 1 row due to aggregate which means the non-aggregate values are "random". Use a sub-query possible via common table expression:

SELECT pi.*
     , pr.EK2
     , pr.EK3
     , (select sum(P_OrderTotal) from ...) as SUM_P_OrderTotal
...

Not the answer you're looking for? Browse other questions tagged or ask your own question.