2011-06-27 97 views
1

我有一個樣本輸入表作爲ROW_NUMBER模擬在SQL Server 2000

Declare @input TABLE(Name VARCHAR(8)) 
INSERT INTO @input(Name) values('Aryan') 
INSERT INTO @input(Name) values('Aryan') 
INSERT INTO @input(Name) values('Joseph') 
INSERT INTO @input(Name) values('Vicky') 
INSERT INTO @input(Name) values('Jaesmin') 
INSERT INTO @input(Name) values('Aryan') 
INSERT INTO @input(Name) values('Jaesmin') 
INSERT INTO @input(Name) values('Vicky') 
INSERT INTO @input(Name) values('Padukon') 
INSERT INTO @input(Name) values('Aryan') 
INSERT INTO @input(Name) values('Jaesmin') 
INSERT INTO @input(Name) values('Vick') 
INSERT INTO @input(Name) values('Padukon') 
INSERT INTO @input(Name) values('Joseph') 
INSERT INTO @input(Name) values('Marya') 
INSERT INTO @input(Name) values('Vicky') 

另外我有一個符合表作爲下

declare @t table(n int) 
insert into @t select 1 union all select 2 union all 
select 3 union all select 4 union all select 5 union all 
select 6 union all select 7 union all select 8 union all 
select 9 union all select 10 union all select 11 union all 
select 12 union all select 13 union all select 14 union all 
select 15 union all select 16 union all select 17 union all 
select 18 union all select 19 union all select 20 

在SQL Server 2005中,如果我做的

Select rn, name from (
    select ROW_NUMBER()over (order by Name) as rn , * from @input) x 
    where rn % 2 <> 0 

我得到的輸出爲

rn name 
1 Aryan 
3 Aryan 
5 Jaesmin 
7 Jaesmin 
9 Joseph 
11 Padukon 
13 Vick 
15 Vicky 

Bu我只限於Sql server 2000.我怎樣才能得到相同的輸出?

我試圖與

SELECT name, (SELECT COUNT(*) FROM @input AS i2 WHERE i2.Name <= i1.Name) As rn 
FROM @input AS i1 

但輸出是錯誤的

name rn 
Aryan 4 
Aryan 4 
Joseph 9 
Vicky 16 
Jaesmin 7 
Aryan 4 
Jaesmin 7 
Vicky 16 
Padukon 12 
Aryan 4 
Jaesmin 7 
Vick 13 
Padukon 12 
Joseph 9 
Marya 10 
Vicky 16 
+0

@marc_s:作爲寫@Maximilian Mayerl - 它是更多鈔票)。 – TcKs

+0

理貨桌有什麼意義? – gbn

+0

根據@Maximilians的回答,您需要向'@ Input'添加一列或將數據插入到另一個臨時表/表變量中,因爲SQL Server無法訂購例如名字='Aryan'的四行 –

回答

2

使用以下查詢:

SELECT t1.name, t.n 
FROM 
(
    SELECT a.name, a.c, (SELECT COUNT(*) FROM @input AS i2 WHERE i2.Name <= a.Name) [rn] 
    FROM 
    (
     SELECT i.name, count(*) c 
     FROM @input i 
     GROUP BY i.name 
    )a 
)t1 
JOIN @t t ON t.n <= t1.rn 
WHERE t.n > t1.rn - t1.c 

它產生所需的輸出:

name  n 
-------- ----------- 
Aryan 1 
Aryan 2 
Aryan 3 
Aryan 4 
Jaesmin 5 
Jaesmin 6 
Jaesmin 7 
Joseph 8 
Joseph 9 
Marya 10 
Padukon 11 
Padukon 12 
Vick  13 
Vicky 14 
Vicky 15 
Vicky 16 
+1

嗨,它像一個魅力..但你能請解釋代碼 – aditi

3

聲明你的表變量作爲

Declare @input TABLE(_id int identity(1, 1), Name VARCHAR(8)) 

然後reqrite查詢作爲

Select _id, name 
from @input 
where _id % 2 <> 0 
+0

如果我有一個Tally表(不干擾現有的@input表模式),那麼我該如何做,例如聲明@t table(n int) 插入@t select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9工會全選10工會全選11工會全部 選12工會全選13工會全選14工會全部 選擇15工會全選16工會全選17工會全部 選擇18工會全選19工會全選20 – aditi

+0

對不起,我不明白你在問什麼,你能否重新提出這個問題? –

+0

這是[重要](http://support.microsoft.com/kb/273586)使用INSERT ... SELECT ... ORDER BY來填充此臨時表,而不是'SELECT ... INTO ... ORDER BY'。 –