Add number to New Column if the row starts at AAA with 1 in the Same Filename

Hello Experts,

I have SQL table columns:


If the Filename starts with AAA in row then 1, if there are more than AAA in rows with the same filename then starts with 1 then 2 then 3 in NewColumn.

Please see attach file.

Thank you for all your help.
Queennie LAsked:
Who is Participating?

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

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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try like:

;with cte as
	select *, ROW_NUMBER() over (partition by FileName, DataName order by DataName, UniqueID) idx from yourTable
Select UniqueID, FileName, DataName,
case when DataName = 'AAA' then idx else null end NewColumn
from cte
ORder By UniqueID

Open in new window

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
Queennie LAuthor Commented:
@Ryan Chong,

I will test your code. I will let you know.

Thank you again for your help.
Queennie LAuthor Commented:

OMG! It is working.

Thank you and Thank you again and again for your help.

I really appreciate it for you intelligence and expertise.
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

From novice to tech pro — start learning today.