Returning the total with the date in a query with SUM

0

I need to return a sum of the value field of table m and along with it the date of the initial field of the field sale_date , being it has an aggregate function error:

SELECT 
    sum(m.value),
    t.sale_date 
from t_transaction t 
left join t_movement m on m.transaction_id =  t.id 
where t.paybox_id = 26

You would need to return the total with the date. The date is the same for all records ...

Note: without t.sale_date returns total value ...

    
asked by anonymous 17.01.2018 / 18:10

2 answers

2

Fields that are not in the aggregate functions should be reported in Group By , in their case t.sale_date . Without this field, it works because there are no others besides what is in the sum() function.

SELECT 
    sum(m.value), 
    t.sale_date 
from t_transaction t 
left join t_movement m on m.transaction_id = t.id 
where t.paybox_id = 26
group by t.sale_date

Select Syntax Documentation: link

    
17.01.2018 / 18:12
0

I think I understand what you want:

SELECT 
    sum(m.value), 
    MIN(t.sale_date) 
from t_transaction t 
left join t_movement m on m.transaction_id = t.id 
where t.paybox_id = 26

Is this right?

    
18.01.2018 / 03:02