I need a Row_number over partition to achieve this in SQL Server

generate row number in sql without rownum
row number sql server example
sql server row_number() over (partition performance)
row_number() over (partition by multiple columns)
partition by sql
row_number() over (partition by oracle
row_number() over (order by
row_number() without order by

I have three columns PID, AppNo and ProcessedDate I need a query to update the AppNo in the format below

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.

This is the sql that is not working

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:

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

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

Row_Number Function With PARTITION BY Clause In SQL Server, Learn how to use Row_Number function without Partition By in SQL or with Partition function is an important function when you do paging in SQL Server. in the OVER clause for each partition specified in the OVER clause. Using SQL Server ROW_NUMBER() function over a result set example. The following statement uses the ROW_NUMBER() to assign each customer row a sequential number: SELECT ROW_NUMBER() OVER ( ORDER BY first_name ) row_num, first_name, last_name, city FROM sales.customers; Here is the partial output:

Query.

SELECT pid as ProjectID
  , Row_Number() OVER(PARTITION BY pid, ProcessedDate ORDER BY ProcessedDate) AS AppNo
  , ProcessedDate
FROM table

Overview of the SQL ROW_NUMBER function, The SQL ROW_NUMBER function is available from SQL Server 2005 and So, the partition may have values 1, 2, 3, and so on and the second partitions BY clause of the OVER clause have nothing to do with each other. The Row_Numaber function is an important function when you do paging in SQL Server. The Row_Number function is used to provide consecutive numbering of the rows in the result by the order selected in the OVER clause for each partition specified in the OVER clause. It will assign the value 1 for the first row and increase the number of the subsequent rows.

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 

ROW_NUMBER (Transact-SQL), If PARTITION BY is not specified, the function treats all rows of the query result set as a single group. For more information, see OVER Clause  SELECT * FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY Code ORDER BY Price ASC) as RowNum from Offers) r where RowNum = 1 Offers table contains about 10 million records. But there are only ~4000 distinct codes there. So I need to get the row with the lowest price for each code and there will be only 4000 rows in the result.

if ProcessedDate is of type DATETIME you can just do

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

;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

Trouble using ROW_NUMBER() OVER (PARTITION BY ), Here's a complete version, tested just now on SQL Server 2016: Once you have the table joined on itself, you can filter, group, sort, etc. to get what you need​. Divides the result set produced by the FROM clause into partitions to which the ROW_NUMBER function is applied. value_expression specifies the column by which the result set is partitioned. If PARTITION BY is not specified, the function treats all rows of the query result set as a single group. For more information, see OVER Clause (Transact-SQL).

convert ProcessedDate to date if row_number shouldn't consider time as:

 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

Sample code here..

SQL ROW_NUMBER() Function, The row number is reset whenever the partition boundary is crossed. For example, if you want to display all employees on a table in an application by pages, pagination get page #2 SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER In the outer query, we selected only the employee rows which have the  SELECT ROW_NUMBER() OVER (PARTITION BY pf ORDER BY Data DESC,idAuxiliarPF DESC) AS RN,idAuxiliarPF. FROM dbo.PFAuxiliar WHERE Data <= GETDATE() AND Descr IS NOT NULL ) PFA2

How to use ROW_NUMBER function properly in SQL Server , In SQL Server, ROW_NUMER() is a common window function used to return a Each set of the result will have a different set of row numbers. SELECT ROW_NUMBER() OVER(PARTITION BY table_no ORDER BY  PARTITION BY clause with ROW_NUMBER() We can use the SQL PARTITION BY clause with ROW_NUMBER() function to have a row number of each row. We define the following parameters to use ROW_NUMBER with the SQL PARTITION BY clause. PARTITION BY column – In this example, we want to partition data on CustomerCity column

How to Use ROW_NUMBER() to Enumerate and Partition Records , Use ROW_NUMBER() to enumerate and partition records in SQL Server. The business logic that needed to be followed was that I had to assign a There are lots of ways to achieve the desired result, but the simplest is to just myself later on in the query, which is different than the row_number() order. In this particular scenario where duplications had to be removed, we used the Row_Number() Over() feature in SQL Server 2005/2008. In the first method, we used it with the Partition BY clause.

Row Number function in SQL Server, This could be across the entire query result, or within partitions. I'll start with cases where the row number ordering does matter. I'll use a table Things get a bit tricky when you need to assign row numbers with a completely  Please read our previous article where we discussed the built-in string function in SQL Server. At the end of this article, you will understand the power and use of the OVER Clause. OVER Clause in SQL Server: The OVER clause in SQL Server is used with PARTITION BY to break up the data into partitions. Following is the syntax of the OVER clause.

Comments
  • why 1st row has AppNo 3 instead of 1?
  • Oh so sorry. I have updated it.
  • Where is the sample data and where is the expected results from the two? They are both the same
  • The result I want is AppNo, I am sure you understand the logic before you asked me to update the question. I need to update the table with the values in App No. The sample data on ground is Column 1 and 3. Column 2(AppNo) is null at the moment, but those values there are what I want to update the column with
  • Perfect Solution. You are a life saver. Any advice on how to make it dynamic and also reduce computation time for already update column, I mean can I use not null for AppNo then bundle the script in a stored procedure to execute when a null value is found. Maybe a trigger or something
  • @PeterGodfreyObike: yes you can add a condition in the outer query to only update non-null values, like UPDATE cte SET AppNo = rn WHERE AppNo IS NOT NULL. Beware however that you need to keep numbering consistent (what happens if you need to change an already affected row number, eg if a ProcessedDate is updated ?).
  • Your perceived question is already an issue, so I had to remove the not null and use your initial script in the program. But I think it has in a way reduce query execution time but it is ok. As the table gets largers it will be a problem. I made it a stored procedure. I can tweak it anytime from remote location. Please if you have any other idea, let me know
  • you shouldn't change the data type of DATE to varchar() unless it is really required other than representational purpose.
  • You have solved 80% of my problem as I can get exactly what is on the table in my live table. But now, how do I update it? Thanks so much