Total sum in mysql view
I have values for a table for each month of different years.
Entries:
entry_id
entry_date
entry_amount
Now I want a view that contains all the input values and the sum of the current year.
Entries_sum_view:
entry_id
entry_date
entry_amount
entry_cumulative_yearly_sum
where entry_cumulative_yearly_sum = SUM (all entries from January 01 of the year (entry_date) to entry_date)
Thanks Martin
+2
Martin
source
to share
1 answer
Found a solution:
SELECT e1.*, SUM(e2.entry_amount) AS cum_sum
FROM Entry e1, Entry e2
WHERE
e2.account_idfk = e1.account_idfk AND
e2.entry_period_end_date BETWEEN MAKEDATE(YEAR(e1.entry_period_end_date),1) AND e1.entry_period_end_date GROUP BY e1.entry_id
+3
Martin
source
to share