Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
SELECT p1.ID, p1.Price,
(SELECT SUM(p2.Price) FROM Products p2 WHERE p1.ID >= p2.ID) as RunningTotal
FROM Products p1
WHERE RunningTotal <= 5000 


I found this query and would like to use it to update the records themselves
Any conversion from select to update


What I have tried:

update Products set p1.g = "ok" where (SELECT p1.ID, p1.Price,
(SELECT SUM(p2.Price) FROM Products p2 WHERE p1.ID >= p2.ID) as RunningTotal
FROM Products p1
WHERE RunningTotal <= 5000)
Posted
Updated 14-Aug-21 3:12am

If I understand the query correctly, you could also try
SQL
UPDATE Products
SET    g = "ok" 
WHERE  5000 > (SELECT SUM(p2.Price) 
               FROM   Products p2 
               WHERE   products.ID >= p2.ID)
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 14-Aug-21 16:15pm    
5ed.
Wendelius 16-Aug-21 23:04pm    
Thanks
Member 14064710 16-Aug-21 6:32am    
The code works fine but when using 25,000 records the display was slow, is there a way to be faster,
database is sqlite
thanks
Wendelius 16-Aug-21 23:05pm    
One thing to ensure is that you have proper indexing in place. For example check that ID field is indexed in products table.

For more information about indexes, see CREATE INDEX[^]
Probably, you should use an UPDATE JOIN rather than SELECT subqueries: SQL Server UPDATE JOIN Explained By Practical Examples[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900