2014-02-19 33 views
0

喜鍵添加新行我有一個問題如何跨兩個coloumns從一系列

SELECT tt.Nr_V, 
     tt.journalnumber_end, 
     tt.journalnumerstart 
FROM transactiontable tt 


SalesTransaction 

Nr_V JournalnumberStart JournalNumberEnd 

100  1001002    1003000 

101  1003001    1004000 etc.. 


OutPutTable 

Nr_V JournalNumber 

100 1001002 

100  1001003 

100  1001004 

etc.. 

100  1003000 etc.. 



101  1004000 
+0

你正在使用哪個數據庫? –

+0

MS企業2012 – user3310846

回答

0

可以使用CTE遞歸

此之間我的數據關鍵coloumn就是一個例子

create table r (id int, st int, ed int) 
insert into r values(1, 5,7), (2 ,8,12) 

with nums as (
    select r.id , r.st no 
    from r 
    union all 
    select nums.id,nums.no +1 from nums join r 
    on r.id = nums.id and r.ed > no 
) 
select * from nums 
order by id, no 

看到http://sqlfiddle.com/#!6/77a21/8

0

嗨,我發現,自動從分鐘STA產生一個Int rt到最大開始在一個while循環和加入之間的開始和結束 - 使它爲我!謝謝你的幫助!我真的很喜歡你們的幫助 - 但我找到了自己的方式。祝你有美好的一天。