当前位置:首页 » 编程语言 » sql取上一值
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql取上一值

发布时间: 2022-12-27 01:55:19

⑴ MS sql问题,如何将下行值为0则取上一行的值

只有一列没法改,至少要有一个不重复的ID列,然后用游标来改,参考以下示例:
declare @t table (id int,val int)
insert @t
select 1,0 union all
select 2,0 union all
select 3,0 union all
select 4,0 union all
select 5,4 union all
select 6,0 union all
select 7,0 union all
select 8,0 union all
select 9,87 union all
select 10,0 union all
select 11,0

declare @id int,@val int,@preval int
declare cursor_@t cursor for select * from @t
open cursor_@t
fetch next from cursor_@t into @id,@val
set @preval=@val
while @@fetch_status=0
begin
if @val=0
update @t set val=@preval where id=@id
else
set @preval=@val
fetch next from cursor_@t into @id,@val
end

close cursor_@t
deallocate cursor_@t

select * from @t

以上在SQL2000中测试通过。

⑵ SQL 如何取出数据库中一列的所有值.....

1、首先需要输入名称和密码登陆服务器。

⑶ 如何获取SQL查询当前数据上一条和下一条的记录

方法一:x0dx0a查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions];x0dx0a查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions];

⑷ sql 如何取上一月的数据

你要转换成date类型的,然后用date-1就行了。
a.date=DateAdd(month, -1, b.date)这样试试

⑸ SQL如何获取上一条..下一条..首尾记录...

获得上一条的id :select max(id)as id from [表] where id<"[你的要查的id]" order by [.....]

获得下一条的id :select min(id)as id from [表] where id>"[你的要查的id]" order by [.....]

很笨的办法但是很直观·
不知道你是什么数据库··根据不同的数据库有很多不同的写法··
比如 mysql 中的 limit 或者 mssql 中的 top
写法多了去啦··呵呵··上面举个例子罢了··希望对你有帮助

⑹ 2021-01-02-Mysql(SQL题如果为null 则取上一条不为null的值)

首先把不为null的数据取出来放在数组中,接着对增加一个字段,如果为null,那么就为0否则为1 ,接着原表开窗(sum()) 取数组中arr[rn-1] 的数据

首先排除为null的,把时间lead上移.如果原表的时间在这个区间,那么就取这个区间的时间
发现join重复数据,采用了过滤为null的值,能否采用其他方法

⑺ sql 查找上一条记录中的值

SELECT 金额 FROM mjs WHERE id IN (SELECT
CASE
WHEN SIGN(id - 3) > 0
THEN MIN(id) WHEN SIGN(id - 3) < 0
THEN MAX(id) END AS id
FROM mjs WHERE id <> 3 GROUP BY SIGN(id - 3) ORDER BY SIGN(id - 3)) ORDER BY id ASC;