如何将row_number()结果存储在同一表列中

时间:2019-05-24 08:02:33

标签: sql sql-server-2008-r2

这是原始表。

  DocumentID    FileName    Folder  OwnerOrg    DuplicateName   Identifire
D001    abc.sldasm  \\abc\\ Demo        
D002    abc.sldprt  \\abc\\ Demo        
D003    abc.sldprt  \\abc_another\\ Demo        
D005    abc.sldprt  \\pqr_another\\ Demo        
D006    kfy.sldasm  \\kfy\\ Demo        
D007    kfy.sldprt  \\abc\\ Demo        
D008    kfy.sldprt  \\abc_another2\\    Demo        
D009    kfy.sldprt  \\xyz_another\\ Demo        
D010    kfy.sldprt  \\kfy\\ Demo        
D011    pny.sldasm  \\pny\\ Demo        
D012    pny.sldasm  \\pny_another1\\    Demo        
D013    pny.slddrw  \\abc\\ Demo        
D014    pny.sldasm  \\abc_another\\ Demo        
D015    pny.sldasm  \\xyz_another\\ Demo        
D016    pny.sldasm  \\pny_another2\\    Demo        
D017    stu.sldprt  \\abc_another\\ Demo        
D018    xyz.sldprt  \\abc\\ Demo        
D019    xyz.sldprt  \\abc_another\\ Demo        
D020    xyz.sldprt  \\xyz_another\\ Demo    

我从重复列中的row_number()函数获取结果

我的查询是

Select * 
From
    (Select 
         Row_Number() Over (Partition By FileName Order By Folder) As Duplicate,
         * 
     From 
         Documents) t1
Where 
    t1.Duplicate > 1 
Order By 
    FileName;

这些是结果:

Duplicate   DocumentID  FileName    Folder              OwnerOrg    DuplicateName   Identifire
-----------------------------------------------------------------------------------------------
2           D003        abc.sldprt  \\abc_another\\     Demo    
3           D005        abc.sldprt  \\pqr_another\\     Demo    
2           D008        kfy.sldprt  \\abc_another2\\    Demo    
3           D010        kfy.sldprt  \\kfy\\             Demo    
4           D009        kfy.sldprt  \\xyz_another\\     Demo    
2           D011        pny.sldasm  \\pny\\             Demo    
3           D012        pny.sldasm  \\pny_another1\\    Demo    
4           D016        pny.sldasm  \\pny_another2\\    Demo    
5           D015        pny.sldasm  \\xyz_another\\     Demo    
2           D019        xyz.sldprt  \\abc_another\\     Demo    
3           D020        xyz.sldprt  \\xyz_another\\     Demo    

现在,我想将row_number的结果存储到DuplicateName列中,作为Demo_<Row_number()>在同一表列DuplicateName中的row_number()大于1的结果

所需结果

Duplicate   DocumentID  FileName    Folder              OwnerOrg    DuplicateName   Identifire
-----------------------------------------------------------------------------------------------
2           D003        abc.sldprt  \\abc_another\\     Demo    Demo_2  
3           D005        abc.sldprt  \\pqr_another\\     Demo    Demo_3  
2           D008        kfy.sldprt  \\abc_another2\\    Demo    Demo_2  
3           D010        kfy.sldprt  \\kfy\\             Demo    Demo_3  
4           D009        kfy.sldprt  \\xyz_another\\     Demo    Demo_4  
2           D011        pny.sldasm  \\pny\\             Demo    Demo_2  
3           D012        pny.sldasm  \\pny_another1\\    Demo    Demo_3  
4           D016        pny.sldasm  \\pny_another2\\    Demo    Demo_4  
5           D015        pny.sldasm  \\xyz_another\\     Demo    Demo_5  
2           D019        xyz.sldprt  \\abc_another\\     Demo    Demo_2  
3           D020        xyz.sldprt  \\xyz_another\\     Demo    Demo_3  

2 个答案:

答案 0 :(得分:0)

使用子查询

select * from ( your query
               ) a where a.row_number>1

答案 1 :(得分:0)

您是否只想将row_number()的结果连接到字符串?

Select t1.*,
       replace('Demo_[n]', '[n]', seqnum) as identifire
From (Select Row_Number() Over (Partition By FileName Order By Folder) As seqnum,
           t1.* 
      From Documents
     ) t1
Where t1.seqnum > 1 
Order By FileName;