⑴ 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;