當前位置:首頁 » 編程語言 » sql查補缺失數據怎麼查
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql查補缺失數據怎麼查

發布時間: 2022-07-11 08:55:18

⑴ 如何使用sql語句查詢連續編號中缺失的編號

declare@tbtable(myidint)
insert@
select*From@tb

SELECTnumberFROMmaster..spt_values
WHEREnumberBETWEEN(SELECTMIN(myid)FROM@tb)
AND(SELECTMAX(myid)FROM@tb)
ANDNOTEXISTS(SELECT*FROM@tbWHEREmyid=number)
ANDtype='p'

⑵ sqlserver 中,如何查詢把缺少數據自動填補出來,下圖中缺少2013-05-27 19:00:00.000 130.4560000000

  1. 問題分析:您要的結果是要每一小時一條記錄,補充添寫中間間隔一小時以上的記錄。並且不另增加記錄:

  2. 問題解決:找到每一條記錄時間加1小時在表中不存在的記錄,然後加一小時填入表中,不包括最後(最大的)的時間。

3.語句實現(兩種方案):

以下語句可以在每一個缺少的數據後加入一小時後填入,但間隔更大(超過2小時後就不行了):

insert into tablename

select fieldtime=dateadd(hh,1,fieldtime),fieldnum from tablename a

where not exists(select 1 from tablename b where dateadd(hh,1,a.fieldtime)=b.fieldtime)

and a.fieldtime!=(select max(fieldtime) from tablename)--去掉最後的時間

以下方案可以完成補充間隔數小時的記錄:將該語句循環執行,直到沒有記錄更改。

insert into tablename

select fieldtime=dateadd(hh,1,fieldtime),fieldnum from tablename a

where not exists(select 1 from tablename b where dateadd(hh,1,a.fieldtime)=b.fieldtime)

and a.fieldtime!=(select max(fieldtime) from tablename)--去掉最後的時間

while @@rowcount>0

select fieldtime=dateadd(hh,1,fieldtime),fieldnum from tablename a where not exists(select 1 from tablename b where dateadd(hh,1,a.fieldtime)=b.fieldtime) and a.fieldtime!=(select max(fieldtime) from tablename)

⑶ 在SQL資料庫中如何查詢某一年份類型所有案號中缺失的案號

首先要知道你的案號生成規則,比如按照你提供的案號,2015-XSI-0003-101,看這個案號,大概推測生成規則是
年-分類縮寫-卷宗編號-案件流水號
你提的問題是,是否缺少案號,就是說,分類縮寫、卷宗編號都不會缺少,至需要判斷某一年中的案子,是不是有缺少案號的情況。
分析
1、按照 年-分類縮寫-卷宗編號對案件進行分組計算count,然後取 年-分類縮寫-卷宗編號下的最大案號,用count值與最大案號進行比對,count值一定是小於等於最大案號的,如果等於,則表示這個 年-分類縮寫-卷宗編號下沒有缺失的案子,如果小於,則表示有缺失的案子,
創建臨時表1,存儲 年-分類縮寫-卷宗編號,最大編號的號碼這兩個欄位,准備下一步處理。
2、創建臨時表2,兩個欄位,第一個是 年-分類縮寫-卷宗編號,第二個是案號,寫一個mysql函數,或者用java代碼寫一個循環,從1開始到臨時表1中保存的最大案號,將記錄插入到臨時表2中,意思就是說,臨時表2中應該保存所有正確的案號,然後用主業務表跟臨時表2關聯,臨時表2中欄位為null的就是缺失的編號了。

⑷ SQL如何尋找缺失ID

Create Table TableName
(
ID int
)insert into TableName (ID) Values (1)
insert into TableName (ID) Values (2)
insert into TableName (ID) Values (3)
insert into TableName (ID) Values (4)
insert into TableName (ID) Values (9)
insert into TableName (ID) Values (10)
insert into TableName (ID) Values (11)
insert into TableName (ID) Values (19)
insert into TableName (ID) Values (20) Select Top 1 ID-1 From TableName Where ID-1 Not In ( Select ID From TableName ) Order By ID Desc--得到缺失ID中最大的一個,結果是:18Select Top 1 ID+1 From TableNamer Where ID+1 not in (Select ID From TableName) Order By ID Asc--得到缺失ID中最小的一個,結果是:5

⑸ sql 每天自動插入一條數據據,如有缺失,怎麼遍歷查看缺少哪一天數據呢

select A.[Date] from (
select Convert(varchar(10),getdate(),120) [Date]
union select Convert(varchar(10),getdate()-1,120)
union select Convert(varchar(10),getdate()-2,120)
union select Convert(varchar(10),getdate()-3,120)
union select Convert(varchar(10),getdate()-4,120)
union select Convert(varchar(10),getdate()-5,120)
union select Convert(varchar(10),getdate()-6,120)) A
left join [TABLE] B on A.[Date]=B.[Date]
where B.[Date] is null

⑹ 如何用sql語句查詢出兩張表中的缺失項

# select * from 表1 as t1 where not exists(select 1 from 表2 as t2 where t2.學號=t1.學號 and t2.試卷號=t1.試卷號)

⑺ 如何在sql查詢 兩個表指標缺少的部分

創建表及數據

createtabletable1
(aint,
bvarchar(3),
cvarchar(100))


createtabletable2
(avarchar(100))


insertintotable1values(2012,'001','計算機專業')
insertintotable1values(2012,'001','經濟專業')
insertintotable1values(2012,'001','新聞專業')
insertintotable1values(2012,'002','計算機專業')
insertintotable1values(2012,'002','經濟專業')
insertintotable1values(2012,'003','新聞專業')
insertintotable2values('計算機專業')
insertintotable2values('新聞專業')
insertintotable2values('地質專業')
insertintotable2values('經濟專業')

運行


selects2.*from
(selectdistinctt1.at1a,t1.b,t2.at2afromtable1t1,table2t2)s2
wherenotexists
(select1fromtable1s1wheres1.a=s2.t1aands1.b=s2.bands1.c=s2.t2a)

結果

⑻ sql查補缺失數據

有點復雜,你現在直接有34點的時間? 還是34點這樣的空缺時間還得重新生成呢?
要是現在直接有34點的時間的話。 那你就把 34點的表 跟2點表連接,然後寫好連接條件。到時候取數據的時候 取 2表中的數據就行了

⑼ sql查找缺少的最小數字

SELECTcoalesce(min(num1),count(1)+1)FROMTAB1//如果正好是順序排列,找出缺少的最後一個值
LEFTJOIN(
(SELECTmin(row_num)ASnum1//找出缺少的值
FROM(SELECTnum
,row_NUMBER()over(ORDERBYnumASC)ASrow_num
FROMTAB1)T1
WHEREt1.num!=t1.row_num)
)T2ON1=1;

如果TAB1中數字存在重復值,要先去重。 用(select distinct num from TAB1)TAB2 替換TAB1。


Good Luck!

⑽ SQL 怎麼填補空缺的數據

看你的需求,應該是一個應用軟體中的需求。

既然這樣資料庫中是不是應該有個日期表?
如果沒有建議建一張表,存放日期

tabledate:
2009-01
2009-02
2009-03
2009-04
...
2009-12

那麼向新表插入的時候可以以此日期表為准

insert into table2(id,date,amount)
select table1.id,table1.date,table1.amount
from tabledate left join table1 on tabledate.date=table1.date
order by table1.id,table1.date

恩,結果和你想要的是有一些差別。就是如果ID和余額如果與上一條記錄相同(注意上面的order by),那麼就為空。余額還有個特殊情況,就是為0的時候也是為null

那麼在插入的時候可以判斷如果為Null就插入一個特殊值
再根據這個特殊值寫幾個Update語句去更新。