例如,如果我的表看起来像:
ITEM DAY PRICEapple 2011-01-07 1.25orange 2011-01-02 1.00apple 2011-01-01 1.00orange 2011-01-03 2.00apple 2011-01-08 1.00apple 2011-01-10 1.50
我希望我的视图看起来像WHERE item =’apple’:
DAY PRICE CHANGE2011-01-01 1.00 (null)2011-01-07 1.25 0.252011-01-08 2.00 0.752011-01-10 1.50 -0.50
编辑:
相当于我正在寻找的查询会在Oracle中看起来像(没试过这个,但我认为这是正确的):
SELECT day,price,price - LAG( price,1 ) OVER ( ORDER BY day ) AS change FROM mytable WHERE item = 'apple'它与另一个相同,但只使用字段而不是rowID.这完全符合您的要求:
CREATE table Prices ( day DATE,price float);INSERT INTO Prices(day,price) VALUES(date('Now','localtime','+1 day'),0.5);INSERT INTO Prices(day,'+0 day'),1);INSERT INTO Prices(day,'-1 day'),2);INSERT INTO Prices(day,'-2 day'),7);INSERT INTO Prices(day,'-3 day'),8);INSERT INTO Prices(day,'-4 day'),10);SELECT p1.day,p1.price,p1.price-p2.price FROM Prices p1,Prices p2,(SELECT t2.day AS day1,MAX(t1.day) AS day2 FROM Prices t1,Prices t2 WHERE t1.day < t2.day GROUP BY t2.day) AS prevWHERE p1.day=prev.day1 AND p2.day=prev.day2
如果要添加WHERE item =’apple’位,则将其添加到两个WHERE子句中.
总结以上是内存溢出为你收集整理的创建一个SQLite视图,其中行取决于前一行全部内容,希望文章能够帮你解决创建一个SQLite视图,其中行取决于前一行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)