Select Top 1 from a nested CTE

Hi,

This is my first post so I hope I am not violating any Rules. :slight_smile:

I have a Common Table Expression that returns 2 Columns, the 1st one uses ROW_NUMBER and has row numbers (rn) in such a way that they Can duplicate

SQL

I then use a 2nd Nested CTE with the Reference to the 1st one, but I want to retrieve only the 1st Row of the First CTE where RN = 1.

Is there a way to do it?

Show us your current query

1 Like

How to post a T-SQL question on a public forum | spaghettidba


;WITH cte_base AS (
    SELECT ROW_NUMBER() ... AS row_num, FeeType
    FROM ...
),
cte_nested AS (
    SELECT TOP (1) *
    FROM cte_base
    WHERE row_num = 1 
    ORDER BY FeeType
)
1 Like

Hi, Thanks for taking the time, ScottPletcher's solution worked for me. Will make sure to post my query from next time.

Thanks for pointing me to the thread. Ill use that from my next post

Thanks Scott, this is exactly what I was looking for. :slight_smile: