在 Sql Server 中设置出现次数
Setting occurrence number in Sqlserver
任何人都可以检查我的 sql 查询,当使用 select 语句时,occurrence_number 列显示正确的值。
select t.[Employee Number], t.Document_Type, t.Document_Name,
row_number() over (partition by document_type
order by right(document_name, 6)
) as occurrence_number
from BluePrismProcesses.dbo.PID0147_DM t
工作
当我使用更新语句时,出现次数乱了。
update BluePrismProcesses.dbo.PID0147_DM
set Occurrence = R.occurrence_number
from (select t.[Employee Number], t.Document_Type, t.Document_Name,
row_number() over (partition by document_type
order by right(document_name, 6)
) as occurrence_number
from BluePrismProcesses.dbo.PID0147_DM t) R
where BluePrismProcesses.dbo.PID0147_DM.Document_Name = r.Document_Name
不工作
问题是您有一个具有相同 document_name
的重复行,因此 where
子句无法按您预期的方式工作。
您可以使用 common table expression 并像这样直接更新该 cte 来解决这个问题:
;with cte as (
select
t.[Employee Number]
, t.Document_Type
, t.Document_Name
, t.occurrence
, occurrence_number = row_number() over (
partition by document_type
order by right(document_name, 6)
)
from BluePrismProcesses.dbo.PID0147_DM t
)
update cte
set occurrence = occurrence_number
where (occurrence <> occurrence_number or occurrence is null);
rextester 演示:http://rextester.com/ZVO11388
returns:
+-----------------+-----------+---------------+---------------+------------+
| Employee Number | Doc_Count | Document_Type | Document_Name | Occurrence |
+-----------------+-----------+---------------+---------------+------------+
| 406453 | 2 | offer | 092735 | 1 |
| 406453 | 2 | offer | 092735 | 2 |
| 406453 | 2 | offer | 092848 | 3 |
| 406453 | 2 | offer | 092848 | 4 |
| 406453 | 2 | offer | 092848 | 5 |
+-----------------+-----------+---------------+---------------+------------+
任何人都可以检查我的 sql 查询,当使用 select 语句时,occurrence_number 列显示正确的值。
select t.[Employee Number], t.Document_Type, t.Document_Name,
row_number() over (partition by document_type
order by right(document_name, 6)
) as occurrence_number
from BluePrismProcesses.dbo.PID0147_DM t
工作
当我使用更新语句时,出现次数乱了。
update BluePrismProcesses.dbo.PID0147_DM
set Occurrence = R.occurrence_number
from (select t.[Employee Number], t.Document_Type, t.Document_Name,
row_number() over (partition by document_type
order by right(document_name, 6)
) as occurrence_number
from BluePrismProcesses.dbo.PID0147_DM t) R
where BluePrismProcesses.dbo.PID0147_DM.Document_Name = r.Document_Name
不工作
问题是您有一个具有相同 document_name
的重复行,因此 where
子句无法按您预期的方式工作。
您可以使用 common table expression 并像这样直接更新该 cte 来解决这个问题:
;with cte as (
select
t.[Employee Number]
, t.Document_Type
, t.Document_Name
, t.occurrence
, occurrence_number = row_number() over (
partition by document_type
order by right(document_name, 6)
)
from BluePrismProcesses.dbo.PID0147_DM t
)
update cte
set occurrence = occurrence_number
where (occurrence <> occurrence_number or occurrence is null);
rextester 演示:http://rextester.com/ZVO11388
returns:
+-----------------+-----------+---------------+---------------+------------+
| Employee Number | Doc_Count | Document_Type | Document_Name | Occurrence |
+-----------------+-----------+---------------+---------------+------------+
| 406453 | 2 | offer | 092735 | 1 |
| 406453 | 2 | offer | 092735 | 2 |
| 406453 | 2 | offer | 092848 | 3 |
| 406453 | 2 | offer | 092848 | 4 |
| 406453 | 2 | offer | 092848 | 5 |
+-----------------+-----------+---------------+---------------+------------+