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 in Database by Neha
• 6,260 points
33 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 by Vaani
• 5,040 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
193 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
273 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
189 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,028 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
282 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 in Database by Vaani
• 5,040 points
12 views
0 votes
1 answer

What is a stored procedure?

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

answered Feb 4 in Database by Neha
• 6,260 points
77 views
0 votes
2 answers

Case in Select Statement

I think these could be helpful for ...READ MORE

answered Feb 4 in Database by Neha
• 6,260 points
46 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 in Database by Vaani
• 5,040 points
35 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 in Database by Vaani
• 5,040 points
37 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP