I have a table with two columns, date and number of pieces, just like this:
pieces Date
100 2022-01-01
200 2022-02-01
300 2022-03-01
and so on.
I want to sum the number of pieces in the way that I increment the newest month, something like this:
january - 100 pieces
february - 300 pieces
march - 600 pieces
How would I do that?
What I've tried so far is make individual selects with one sum up to a point and then union with another, up to another point, but it seems counter productive.
select
sum(pieces)
, 'january' as month
from
table
where
date <= '2022-01-31'
union
select
sum(pieces)
, 'february' as month
from
table
where
date <= '2022-02-28'
union
select
sum(pieces)
, 'march' as month
from
table
where
date <= '2022-03-31'