我的Sales Table包含以下字段。
year month samount
要求是检查当月的销售额是否高于当月的上年销售额。我尝试使用以下sql,但似乎缺少一些比较。请告知
select year, month, samount, samount-lag(samount,12) over (order by year,month) as diff_cur_prev_sales from sales;
jdgnovmf1#
如果您需要比较相同月份和不同年份的销售额,您需要更改LAG函数。只需颠倒年份和月份列的顺序-
SELECT year, month, samount, prev_year_amount FROM (SELECT year, month, samount, lag(samount,12) over (order by month, year) as prev_year_amount FROM sales ) T WHERE samount > prev_year_amount;
1条答案
按热度按时间jdgnovmf1#
如果您需要比较相同月份和不同年份的销售额,您需要更改LAG函数。只需颠倒年份和月份列的顺序-