How do I use ROW_NUMBER()?

前端 未结 13 2377
青春惊慌失措
青春惊慌失措 2020-11-28 02:35

I want to use the ROW_NUMBER() to get...

  1. To get the max(ROW_NUMBER()) --> Or i guess this would also be the count of all rows
相关标签:
13条回答
  • 2020-11-28 03:09

    If you need to return the table's total row count, you can use an alternative way to the SELECT COUNT(*) statement.

    Because SELECT COUNT(*) makes a full table scan to return the row count, it can take very long time for a large table. You can use the sysindexes system table instead in this case. There is a ROWS column that contains the total row count for each table in your database. You can use the following select statement:

    SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name') AND indid < 2
    

    This will drastically reduce the time your query takes.

    0 讨论(0)
  • 2020-11-28 03:09

    Need to create virtual table by using WITH table AS, which is mention in given Query.

    By using this virtual table, you can perform CRUD operation w.r.t row_number.

    QUERY:

    WITH table AS
    -
    (SELECT row_number() OVER(ORDER BY UserId) rn, * FROM Users)
    -
    SELECT * FROM table WHERE UserName='Joe'
    -
    

    You can use INSERT, UPDATE or DELETE in last sentence by in spite of SELECT.

    0 讨论(0)
  • 2020-11-28 03:13

    If you absolutely want to use ROW_NUMBER for this (instead of count(*)) you can always use:

    SELECT TOP 1 ROW_NUMBER() OVER (ORDER BY Id)   
    FROM USERS  
    ORDER BY ROW_NUMBER() OVER (ORDER BY Id) DESC
    
    0 讨论(0)
  • 2020-11-28 03:15

    Though I agree with others that you could use count() to get the total number of rows, here is how you can use the row_count():

    1. To get the total no of rows:

      with temp as (
          select row_number() over (order by id) as rownum
          from table_name 
      )
      select max(rownum) from temp
    2. To get the row numbers where name is Matt:

      with temp as (
          select name, row_number() over (order by id) as rownum
          from table_name
      )
      select rownum from temp where name like 'Matt'

    You can further use min(rownum) or max(rownum) to get the first or last row for Matt respectively.

    These were very simple implementations of row_number(). You can use it for more complex grouping. Check out my response on Advanced grouping without using a sub query

    0 讨论(0)
  • 2020-11-28 03:18

    SQL Row_Number() function is to sort and assign an order number to data rows in related record set. So it is used to number rows, for example to identify the top 10 rows which have the highest order amount or identify the order of each customer which is the highest amount, etc.

    If you want to sort the dataset and number each row by seperating them into categories we use Row_Number() with Partition By clause. For example, sorting orders of each customer within itself where the dataset contains all orders, etc.

    SELECT
        SalesOrderNumber,
        CustomerId,
        SubTotal,
        ROW_NUMBER() OVER (PARTITION BY CustomerId ORDER BY SubTotal DESC) rn
    FROM Sales.SalesOrderHeader
    

    But as I understand you want to calculate the number of rows of grouped by a column. To visualize the requirement, if you want to see the count of all orders of the related customer as a seperate column besides order info, you can use COUNT() aggregation function with Partition By clause

    For example,

    SELECT
        SalesOrderNumber,
        CustomerId,
        COUNT(*) OVER (PARTITION BY CustomerId) CustomerOrderCount
    FROM Sales.SalesOrderHeader
    
    0 讨论(0)
  • 2020-11-28 03:19
    SELECT num, UserName FROM 
     (SELECT UserName, ROW_NUMBER() OVER(ORDER BY UserId) AS num
      From Users) AS numbered
    WHERE UserName='Joe'
    
    0 讨论(0)
提交回复
热议问题