简体   繁体   中英

How to store row_number() results in same table column

Here is the original table.

  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    

which i get the results from row_number() function in Duplicate column

My query is

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

These are the results:

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    

Now I want to store the result of row_number values into DuplicateName column as Demo_<Row_number()> result wherever row_number() is > 1 in same table column DuplicateName

Desired result

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  

use subquery

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

Do you just want to concatenate the result of row_number() to a string?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM