如何获取上一行值 [英] How to get previous row value

查看:42
本文介绍了如何获取上一行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 SELECT 语句的前一个结果行获取值

How to get a value from previous result row of a SELECT statement

如果我们有一个名为 cardevent 的表并且有行 [ID(int) , Value(Money) ] 并且我们有一些行,例如

If we have a table called cardevent and has rows [ID(int) , Value(Money) ] and we have some rows in it, for example

ID --Value

1------70 
1------90
2------100
2------150
2------300 
3------150 
3------200 
3-----250 
3-----280

等等...

如何制作一个查询,获取每一行的 ID、值和前一行值,其中数据出现如下

How to make one Query that get each row ID,Value and the previous Row Value in which data appear as follow

ID --- Value ---Prev_Value

1 ----- 70 ----------  0 
1 ----- 90 ---------- 70
2 ----- 100 --------  90 
2 ------150 -------- 100
2 ------300 -------- 150
3 ----- 150 -------- 300 
3 ----- 200 -------- 150 
3 ---- 250 -------- 200 
3 ---- 280 -------- 250

等等.

那么谁能帮我找到解决此类问题的最佳解决方案?

So can anyone help me to get the best solution for such a problem ?

需要查询帮助

推荐答案

您必须将表与自身连接,我不确定这是否是 100% 合法的 SQL,但我没有 SQL-Server 来尝试此操作目前,但试试这个:

You would have to join the table with itself, I'm not sure if this is 100% legitimate SQL, but I have no SQL-Server to try this at the moment, but try this:

select (ID, Value) from table as table1 join
inner join table as table2
on table1.ID = (table2.ID -1)

这篇关于如何获取上一行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆