我在 mysql 中有一个表格,其中包含日期、收入列,以及当月每天产生的收入我需要将其转换为格式日期,“截至该日期的收入总和”
我可以用
select max(date) as date, sum(revenue) as total_revenue_to_date from table where dayofmonth(date)<=1
union
select max(date) as date, sum(revenue) as total_revenue_to_date from table where dayofmonth(date)<=2
.......
等,但想以更好的格式编写它。
有人有想法么?
asnwered:最短,最容易遵循:
SELECT fulldate,
(SELECT SUM(margin) FROM fact_agg_margin_live_daily d2 WHERE d1.fulldate>=d2.fulldate) AS margin
FROM fact_agg_margin_live_daily d1