我要减去表上一列的两个值:
表格名称:统计信息
-|article|year|amount| -|-------|----|------| -|art1 |2018|289 | -|art2 |2018|4512 | -|art1 |2017|117 | -|art2 |2017|1230 | ...
我使用了这个查询,但是没有用:
-SELECT -(CASE WHEN year = strftime('%Y', 'now') THEN s.amount END ) AS yearM, -(CASE WHEN year = strftime('%Y', 'now')-1 THEN s.amount END ) AS yearM1, -(yearM - yearM1) AS Delta -FROM statistic AS s
yearM和yearM1列包含值,但Delta列为空
我找不到问题所在!
答案 0 :(得分:0)
您需要自我加入:
select s1.amount as yearM, s2.amount as yearM1, (s1.amount-s2.amount) as Delta
from statistic s1, statistic s2
where s1.year=strftime('%Y', 'now')
and s2.year=strftime('%Y', 'now')-1
and s1.article=s2.article;