有人可以在SQL上为以下输入建议吗,我想连续生成当前季度和上一季度金额
输入
Fiscper Quarter_Amount
2017Q1 7$
2017Q2 8$
2017Q3 5$
2017Q4 3$
2018Q1 10$
输出
Output Current Quarter Amount Prior Quarter Amount
2017Q1 7$
2017Q2 8$ 7$
2017Q3 5$ 8$
2017Q4 3$ 5$
2018Q1 10$ 3$
答案 0 :(得分:0)
您想要lag()
:
select t.*, lag(Quarter_Amount) over (order by Fiscper) as Prior_Quarter_Amount
from table t;
但是,您也可以使用子查询:
select t.*, (select t1.Quarter_Amount
from table t1
where t1.Fiscper < t.Fiscper
order by t1.Fiscper desc
limit 1
) as Prior_Quarter_Amount
from table t;