Update with row count based on a qualifyer

Let's say I have table with these values:

ORDRNMBR,LNITMSEQ,ITEMNMBR,QTY,FLAG
1000,0,WIDGET,12,1
1000,0,BOXCUTTER,24,1
1000,0,CAMERA,1,0
1000,0,LUNCHBOX,2,1

Flag=1 means add to order. When I am done processing these are rows I want to see
1000,1,WIDGET,12,1
1000,2,BOXCUTTER,24,1
1000,3,LUNCHBOX,2,1

The camera is left off the order and the LNITMSEQ column now has a row number indicating the line counter on the order for the items that are to be included on the order. How do I structure update statement to accomplish this by updating the value for LNITMSEQ?
LVL 1
rwheeler23Asked:
Who is Participating?
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.

Scott PletcherSenior DBACommented:
I think this will work, but can't test it as I don't have useable sample data.  Of course there's no guarantee to the exact seq nums assigned unless you can sort on a sequential value, such as an identity or insert datetime.  The values will be sequential and will start at 1, but other than that, no guarantees based on what's seen so far.


DELETE FROM dbo.table_name
WHERE FLAG = 0

;WITH cte_prep AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY ORDRNMBR ORDER BY LNITMSEQ) AS row_num
    FROM dbo.table_name
    WHERE FLAG = 1
)
UPDATE cte_prep
SET LNITMSEQ = tn.row_num
0

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
Aneesh RetnakaranDatabase AdministratorCommented:
DECLARE @i INT = 0 
UPDATE urTable SET @i+= 1, LNITMSEQ = @i WHERE FLAG = 1 
SELECT * FROM urTable WHERE flag = 1 

Open in new window

0
rwheeler23Author Commented:
Thank you both for your input. Both of these get me going in the right direction. I am optimizing old code and this little snippet is part of a very large process. There are hundreds of thousands of records in this table with another flag field to indicate if the sales button was clicked. So the where clause starts with an order number and then looks at two flag fields. If the record qualifies it gets sent off to another table with the correct line number to be turned into an order to be processed in an ERP solution.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.