How do I use ROW NUMBER

0 votes

Can I use the syntax ROW_NUMBER() to get the following​

SELECT max(ROW_NUMBER() OVER(ORDER BY CustomerId)) FROM Customers

but it didn't work

  1. To get ROW_NUMBER() using a given piece of information, ie. if I have a name and I want to know what row the name came from.

I assume it would be something similar to what I tried for #1

SELECT ROW_NUMBER() OVER(ORDER BY CustomerId) From Users WHERE CustomerName='John'

but this also did not work

Feb 18, 2022 in Database by Neha
• 9,060 points
369 views

1 answer to this question.

0 votes

You can use the query as:

SELECT COUNT(*) FROM Users 

to get the count.

The primary key of the row should be used to identify a particular row. 


If you wanted to return Row_Number() in your main query,

SELECT ROW_NUMBER() OVER (Order by Customer_Id) AS RowNumber, Field1, Field2, Field3
FROM Users

Then when you want to go 4 rows back then you can take the current row number and use the following query to determine the row with currentrow -4

SELECT users.Customer_Id
FROM (SELECT ROW_NUMBER() OVER (ORDER BY Customer_id) AS Row, Customer_Id
     FROM Users ) users 
WHERE Row = CurrentRow - 4   
answered Feb 21, 2022 by Vaani
• 7,020 points

Related Questions In Database

0 votes
1 answer

How do I enable Oracle automatically on systemboot?

To enable the database service to start ...READ MORE

answered Oct 5, 2018 in Database by Frankie
• 9,830 points
508 views
0 votes
1 answer

How do i install MongoDB on ubuntu?

Hey @Aryaa, follow these steps: Import Public Key sudo ...READ MORE

answered Mar 15, 2019 in Database by Avinash
594 views
0 votes
1 answer

How do I install MSSQL Server on Linux?

Hey @Rajni, to install MSSQL Server on ...READ MORE

answered Mar 17, 2019 in Database by Rakesh
464 views
0 votes
1 answer

How do I install MariaDB on Windows?

Hwy @Mike, install the latest stable version ...READ MORE

answered Mar 22, 2019 in Database by Nitin
1,558 views
0 votes
0 answers

Equivalent of Oracle's RowID in SQL Server

What does SQL Server's RowID resemble in ...READ MORE

Aug 30, 2022 in Database by Kithuzzz
• 38,010 points
557 views
0 votes
1 answer

Calculate Time Intersection to Correlate Sequences of Independent Events

I think this solution requires a CROSS JOIN implementation. ...READ MORE

answered Oct 26, 2018 in Power BI by Upasana
• 8,620 points
583 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,020 points
329 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,060 points
805 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,020 points
600 views
0 votes
1 answer

How do I perform an IF THEN statement in an SQL SELECT?

The CASE statement is the closest to IF in ...READ MORE

answered Feb 16, 2022 in Database by Vaani
• 7,020 points
1,253 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP