如何将 row_number() 结果存储在相同的 table 列中
How to store row_number() results in same table column
这里是原文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
我从重复列 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()>
结果,只要 row_number()
在同一 table 列中 > 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
使用子查询
select * from ( your query
) a where a.row_number>1
您只想将 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;
这里是原文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
我从重复列 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()>
结果,只要 row_number()
在同一 table 列中 > 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
使用子查询
select * from ( your query
) a where a.row_number>1
您只想将 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;