using Row_Number and Partition. a little confused

Suppose I have a small table with names. Each person belongs to either a soccer club, or a tennis club. Soccer club members have a ClubId of 2, and tennis club members have an Id of 1.
So here is my question. See my sql statement at the bottom that does the partition? Does the partition only act on the "Id" column, or does it act on all 4 columns.

INSERT INTO Clubs VALUES (1,1,'Mary','Lakelo')
INSERT INTO Clubs VALUES (2,1,'Tommy','Lakelo')
INSERT INTO Clubs VALUES (3,2,'Mary','Snazzleer')
INSERT INTO Clubs VALUES (4,2,'Sam','Closio')
INSERT INTO Clubs VALUES (5,2,'Chad','Greyson')
INSERT INTO Clubs VALUES (6,2,'Bill','Nacy')

select *, ROW_NUMBER()
OVER (
 PARTITION BY
 Id,
 ClubId,
 FirstName,
 LastName

ORDER BY
 Id,
 ClubId,
 FirstName,
 LastName) RN
 FROM Clubs
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
PARTITION BY in the context of a windowing function like ROW_NUMBER, RANK, or DENSE_RANK is the same as a GROUP BY, so the 'partition only act on' four columns Id, ClubId, FirstName, and LastName, which kinda defeats the purpose as Id appears to be unique.

If you want the row number within club types (tennis=1, soccer=2), then only use ClubId.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
As  is rn will be one. As Jim said,  ID is probably the only column you want to include in PARTITION. What you include in PARTITION needs to be constant for the windowing function,  and restart s whenever the expressions give a different result.
PortletPaulEE Topic AdvisorCommented:
The relevant documentation for this is in the OVER() clause

where the definition is:
PARTITION BY
Divides the query result set into partitions. The window function is applied to each partition separately and computation restarts for each partition.
For ROW_NUMBER() it is used to restart counting when the partition values change
(but note  it is the ORDER BY that determines which row within the partition that get 1)

Perhaps think of PARTITION BY as being similar to using GROUP BY, but:
in GROUP BY the columns you use define the number of rows
in PARTITION BY the columns you choose define when you restart a calculation

I think you would find this query more useful:

SELECT
      *
    , ROW_NUMBER() OVER (PARTITION BY ClubId
                         ORDER BY FirstName, LastName) RN
FROM Clubs

 (re)start counting for each clubid, with 1 being determined by the ORDER

| Id | ClubId | FirstName |  LastName | RN |
|----|--------|-----------|-----------|----|
|  1 |      1 |      Mary |    Lakelo |  1 |
|  2 |      1 |     Tommy |    Lakelo |  2 |
|  6 |      2 |      Bill |      Nacy |  1 |
|  5 |      2 |      Chad |   Greyson |  2 |
|  3 |      2 |      Mary | Snazzleer |  3 |
|  4 |      2 |       Sam |    Closio |  4 |

Open in new window

       
details:
    CREATE TABLE Clubs
        ([Id] int, [ClubId] int, [FirstName] varchar(80), [LastName] varchar(80))
    ;
    
    INSERT INTO Clubs VALUES (1,1,'Mary','Lakelo');
    INSERT INTO Clubs VALUES (2,1,'Tommy','Lakelo');
    INSERT INTO Clubs VALUES (3,2,'Mary','Snazzleer');
    INSERT INTO Clubs VALUES (4,2,'Sam','Closio');
    INSERT INTO Clubs VALUES (5,2,'Chad','Greyson');
    INSERT INTO Clubs VALUES (6,2,'Bill','Nacy');
    
    
    
**Query 1**:

    SELECT
          *
        , ROW_NUMBER() OVER (PARTITION BY ClubId
                             ORDER BY FirstName, LastName) RN
    FROM Clubs
    ORDER BY ClubId, FirstName, LastName
    

**[Results][2]**:
    | Id | ClubId | FirstName |  LastName | RN |
    |----|--------|-----------|-----------|----|
    |  1 |      1 |      Mary |    Lakelo |  1 |
    |  2 |      1 |     Tommy |    Lakelo |  2 |
    |  6 |      2 |      Bill |      Nacy |  1 |
    |  5 |      2 |      Chad |   Greyson |  2 |
    |  3 |      2 |      Mary | Snazzleer |  3 |
    |  4 |      2 |       Sam |    Closio |  4 |
**Query 2**:

    
    
    SELECT
          ClubId
        , COUNT(*) AS CN
    FROM Clubs
    GROUP BY ClubId

**[Results][3]**:
    | ClubId | CN |
    |--------|----|
    |      1 |  2 |
    |      2 |  4 |

  [1]: http://sqlfiddle.com/#!6/17eb7/3
  [2]: http://sqlfiddle.com/#!6/17eb7/3/0
  [3]: http://sqlfiddle.com/#!6/17eb7/3/1

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.