Donate

Add Row Number Dynamically To Query Results In T-SQL

Aloha fellow developers!

I have this scenario wherein I need to display row numbers in my result set given that the rows don't have a ID column and each row number represents the month number of a specific month.
SELECT 
 A.fsyear_num as 'Fiscal Year',
 [MonthName]
FROM CTE_GrossProfit_SalesByClass_Current_FSYear A
Add Row Number Dynamically To Query Results In T-SQL
I know that row number is the solution but I need to tweak this to handle my requirement. After reading the forums and docs, I found out that adding a select statement with COUNT() inside the Over By clause is the solution.
SELECT 
 A.fsyear_num as 'Fiscal Year',
 ROW_NUMBER() Over(Order BY (Select Count(*))) AS MonthNum,
 [MonthName]
FROM CTE_GrossProfit_SalesByClass_Current_FSYear A
ORDER BY MonthNum ASC

Add Row Number Dynamically To Query Results In T-SQL

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Test Or Check Dead Pixels And Backlight Bleed On Your Monitor Using Dead Pixel Checker In Windows 11

How To Insert Or Add Emojis In Microsoft Teams Status Message