Avatar of grogo21
grogo21 asked on

Using ROW_NUMBER()

Hello Can I use the Row_Number function with a select that joins tables?  Im having trouble figuring how to write the select using Row_Count using this select:

        SELECT c.ItemId, c.Title, a.Fname, a.Lname, c.ItemType, c.ImageId, c.Price
        from Catalog c, ItemCat i, Artist a
        where c.ItemId = i.ItemId And a.ArtistId = c.ArtistId And i.SubCatId = @subcatid

Thanks Very Much!
Visual Basic.NETMicrosoft SQL Server 2005SQL

Avatar of undefined
Last Comment
Nerdwood

8/22/2022 - Mon
Nerdwood

Are you meaning to use @@ROWCOUNT to find the number of records affected?
Nerdwood

Are you wanting something like this? What's the row count for?
SELECT ROW_NUMBER() OVER (ORDER BY c.ItemId) AS 'Row Number', c.ItemId, c.Title, a.Fname, a.Lname, c.ItemType, c.ImageId, c.Price
FROM Catalog c, ItemCat i, Artist a
WHERE c.ItemId = i.ItemId And a.ArtistId = c.ArtistId And i.SubCatId = @subcatid

Open in new window

ASKER
grogo21

Hello, I am trying to do something like below to implement paging of results:

SELECT  Description, Date
FROM     (SELECT  ROW_NUMBER() OVER (ORDER BY Date DESC)
             AS Row, Description, Date FROM LOG)
            AS LogWithRowNumbers
WHERE  Row >= 1 AND Row <= 10

Thanks
Your help has saved me hundreds of hours of internet surfing.
fblack61
Nerdwood

That's not working?
dqmq

Is there a problem? My favorite:

With MyCTE
AS
 (SELECT  ROW_NUMBER() OVER (ORDER BY Date DESC) Row, Description, Date FROM LOG)
SELECT * from MyCTE
WHERE  Row >= 1 AND Row <= 10


ASKER
grogo21

Im sorry, that is an example seen on this page http://davidhayden.com/blog/dave/archive/2005/12/30/2652.aspx

Im having trouble implementing ROW_NUMBER like he did in the link with the select statement I posted in my original post becuase it has joins in it.

Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Nerdwood

Try this:
SELECT c.ItemId, c.Title, a.Fname, a.Lname, c.ItemType, c.ImageId, c.Price
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY c.Price) AS Row, c.ItemId, c.Title, a.Fname, a.Lname, c.ItemType, c.ImageId, c.Price
    FROM (Catalog c
        INNER JOIN ItemCat i ON c.ItemId = i.ItemId)
        INNER JOIN Artist a ON c.ArtistId = a.ArtistId
    WHERE i.SubCatId = @subcatid
)
WHERE Row BETWEEN 1 AND 10

Open in new window

ASKER CERTIFIED SOLUTION
Nerdwood

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Nerdwood

Excellent... good to know it worked! :)