[英]I need a Row_number over partition to achieve this in SQL Server
I have three columns PID, AppNo and ProcessedDate I need a query to update the AppNo in the format below我有三列 PID、AppNo 和 ProcessedDate 我需要一个查询来更新以下格式的 AppNo
PID AppNo ProcessedDate
11 1 09/30/2019 18:21
3 1 09/25/2019 08:37
3 2 09/25/2019 08:37
11 1 09/25/2019 08:39
11 2 09/25/2019 08:40
7 1 09/26/2019 14:19
7 2 09/26/2019 14:20
7 3 09/26/2019 14:22
2 1 09/26/2019 14:23
11 1 09/26/2019 14:23
11 2 09/26/2019 14:24
11 3 09/26/2019 14:24
3 1 09/26/2019 14:24
For now the AppNo column is null.现在 AppNo 列是 null。
This is the sql that is not working这是不工作的 sql
SELECT AppNo, ProcessedDate,pid
,Row_Number() OVER(PARTITION BY pid, ProcessedDate ORDER BY ProcessedDate) AS rn
select * FROM table
You seem to be looking to update your original table.您似乎正在寻找更新原始表格。 You can use
ROW_NUMBER()
in a CTE to rank records with groups having the same date (without time) and pid, ordered by date (with time) and then do the update on the fly:您可以在 CTE 中使用
ROW_NUMBER()
对具有相同日期(无时间)和 pid 的组的记录进行排名,按日期(有时间)排序,然后即时进行更新:
WITH cte AS (
SELECT
pid,
ProcessedDate,
AppNo,
ROW_NUMBER() OVER(PARTITION BY pid, CAST(ProcessedDate AS DATE) ORDER BY ProcessedDate) rn
FROM mytable
)
UPDATE cte SET AppNo = rn
Demo on DB Fiddle DB Fiddle 上的演示
Original data:原始数据:
PID | AppNo | ProcessedDate --: | ----: | :--------------- 11 | null | 09/30/2019 18:21 3 | null | 09/25/2019 08:37 3 | null | 09/25/2019 08:37 11 | null | 09/25/2019 08:39 11 | null | 09/25/2019 08:40 7 | null | 09/26/2019 14:19 7 | null | 09/26/2019 14:20 7 | null | 09/26/2019 14:22 2 | null | 09/26/2019 14:23 11 | null | 09/26/2019 14:23 11 | null | 09/26/2019 14:24 11 | null | 09/26/2019 14:24 3 | null | 09/26/2019 14:24
After running the query:运行查询后:
PID | AppNo | ProcessedDate --: | ----: | :--------------- 11 | 1 | 09/30/2019 18:21 3 | 1 | 09/25/2019 08:37 3 | 2 | 09/25/2019 08:37 11 | 1 | 09/25/2019 08:39 11 | 2 | 09/25/2019 08:40 7 | 1 | 09/26/2019 14:19 7 | 2 | 09/26/2019 14:20 7 | 3 | 09/26/2019 14:22 2 | 1 | 09/26/2019 14:23 11 | 1 | 09/26/2019 14:23 11 | 2 | 09/26/2019 14:24 11 | 3 | 09/26/2019 14:24 3 | 1 | 09/26/2019 14:24
Query.询问。
SELECT pid as ProjectID
, Row_Number() OVER(PARTITION BY pid, ProcessedDate ORDER BY ProcessedDate) AS AppNo
, ProcessedDate
FROM table
convert ProcessedDate
to date if row_number shouldn't consider time as:如果 row_number 不应将时间考虑为:则将
ProcessedDate
转换为日期:
SELECT AppNo, ProcessedDate,pid
,Row_Number() OVER(PARTITION BY pid, convert (date,ProcessedDate)
ORDER BY ProcessedDate) AS rn
from @T
You can write an Update as:您可以将更新编写为:
with CTE as(
SELECT ProcessedDate,pid
,Row_Number() OVER(PARTITION BY pid, convert (date,ProcessedDate)
ORDER BY ProcessedDate) AS rn
from Test
)
update CTE
set AppNo = rn
and test the result as:并将结果测试为:
Select AppNo ,
Row_Number() OVER(PARTITION BY pid, convert (date,ProcessedDate)
ORDER BY ProcessedDate) AS rn,
ProcessedDate,
pid
from Test
you must use partition just with date part like this:您必须仅使用带有日期部分的分区,如下所示:
SELECT AppNo, ProcessedDate,pid
,Row_Number() OVER(PARTITION BY pid, CONVERT(VARCHAR(10), ProcessedDate, 111) ORDER BY ProcessedDate) AS rn
select * FROM table
if ProcessedDate
is of type DATETIME
you can just do如果
ProcessedDate
是DATETIME
类型,你可以这样做
SELECT
pid,
ROW_NUMBER() OVER (
PARTITION BY pid, convert(date, ProcessedDate)
ORDER BY pid, ProcessedDate
) AppNo,
ProcessedDate
FROM table
if ProcessedDate
is of type VARCHAR
you can just do如果
ProcessedDate
是VARCHAR
类型,你可以这样做
;WITH
t as (
SELECT pid, convert(datetime, ProcessedDate, 120) ProcessedDate
from table
)
SELECT
pid,
ROW_NUMBER() OVER (
PARTITION BY pid, convert(date, ProcessedDate)
ORDER BY pid, ProcessedDate
) AppNo,
ProcessedDate
FROM t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.