2011-07-25 65 views
1

我列缺少ID看起來像下面如何在表中查找

SID101 
SID102 
SID103 
SID105 
SID107 

在上述標準我需要找到錯過SID號。訂購時錯過了SID104和SID 106。

我怎樣才能找到錯過的id號碼。任何人都可以幫我找到它。

在此先感謝。

+0

@cularis:感謝格式化:-) – Shine

+0

是否有一個表*全部* sid?還是缺少只是意味着有差距? – wonk0

+0

我會建議在代碼中做這樣的事情,如果它是關於找到差距。 – Jacob

回答

0

這很困難。隨着

SELECT COUNT(*),MAX(CAST(REPLACE(y.name,'SID','') AS INT)) AS col_max FROM 
sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id 
WHERE x.name='<TABLE_NAME>' 

你應該知道,有多少缺少列(即COUNT(*)是5和col_max爲107)

當你有一個表,其中包含有從1到最大所有可能的ID只有一個列(即100,101,102,103,104,...,132),那麼你可以做

SELECT * FROM (
    SELECT CAST(REPLACE(y.name,'SID','') AS INT) AS col_id FROM 
    sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id 
    WHERE x.name='<TABLE_NAME>' 
) a 
RIGHT JOIN <TABLE_IDS> b ON a.col_id=b.id 
WHERE a.col_id IS NULL AND b.id<=(
    SELECT MAX(CAST(REPLACE(y.name,'SID','') AS INT)) AS col_max FROM 
    sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id 
    WHERE x.name='<TABLE_NAME>' 
) 

編輯:對不起,我剛纔看到的,這些值不列名,但值。我的解決方案會尋找失蹤列名

-1
declare @t table(s varchar(20)) 

insert @t values ('SID101'),('SID102'),('SID103'),('SID105'),('SID107'); 

with cte as 
(
    select substring(t.s, 4, len(t.s)) [i] 
    from @t t 
) 

select 'SID' + cast(t1.i + 1 as varchar(20)) 
from cte t1 
join cte t2 on t2.i > t1.i 
    and not exists(
     select 1 
     from cte c3 
     where c3.i > t1.i and c3.i < t2.i 
    ) 
where t2.i <> t1.i + 1 

輸出:

----------------------- 
SID104 
SID106 
+1

@downvoter中沒有SID丟失的記錄,請注意? –

+0

我沒有downvote,但我猜測這是因爲你有相同的問題的2solutions –

+0

@ t-clausen.dk,我收到downvote當我只發佈第一個解決方案。這些解決方案適用於不同的情況,所以我發佈了2個答案,而不是一個答案。 –

0

像這樣的東西應該工作:

DECLARE @i INT; 
SET @i = 100; 

CREATE TABLE #idsToCheck (checkId varchar(100)); 

WHILE (@i < 200) 
    BEGIN 
     INSERT INTO #idsToCheck VALUES ('SID' + CONVERT(varchar(100), @i)); 
     SET @i = @i + 1; 
    END 

SELECT * FROM #idsToCheck itc 
    LEFT OUTER JOIN MainTable mt ON itc.checkId = mt.realId 
    WHERE mt.realId = NULL 

DROP TABLE #idsToCheck 

...其中MainTable是包含SID101SID102你的表,等等列值,並且MainTable.realId是包含這些ID的列。根據您想要檢查的SID來修改while循環條件中的@i初始值和編號。

1

如果表中包含長度大於1項以上的差距,您可以使用此查詢:

declare @t table(s varchar(20)) 

insert @t values ('SID101'),('SID102'),('SID103'),('SID105'),('SID108'); 

with cte as 
(
    select substring(t.s, 4, len(t.s)) [i] 
    from @t t 
) 
select 'SID' + cast(m.number as varchar(20)) 
from master..spt_values m 
left join cte c on c.i = m.number 
where [Type] = 'P' 
    and m.number >= (select min(i) from cte) 
    and m.number <= (select max(i) from cte) 
    and c.i is null 

輸出:

----------------------- 
SID104 
SID106 
SID107 
0

聲明@St INT 聲明@end詮釋

 set @st = CAST((select RIGHT(max(data),4) from orderno)as int) 
    set @end = CAST((select RIGHT(min(data),4) from orderno)as int) 

    create table #temp(data int) 

    while(@St <= @end) 
    begin 
insert into #temp values(@St) 
set @St = @St +1 
    end 

    select * from orderno 
    select * from #temp 
select data from #temp where data not in (select cast(RIGHT(data,4))