Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 complex sql query

Author  Topic 

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2012-09-07 : 13:49:23
Hi,

I have to write a query and i am not sure how to go about it. I have a table customer with fields customerid and amount (decimal) and based on parameters I have to update the amount of each customer.
The total amount can't exceed $XX.XX (eg $250.000).
If user decides that he wants to buy all the amount that each user has than the total he buys can't exceed $250.000. So that means in the example below that he can buy from customers 1-3 all the amount but from the last customer he can only buy 130,000 has 20,000+20,000+30,000+130,000=250 and if from the last customer he would buy 200,000 the total will exceed the limit of 250.
How can I write the stored procedure?
Example:
ID AMOUNT AmountPurchased
1 20,000
2 20,000
3 30,000
4 200,00

Thanks

chadmat
The Chadinator

1974 Posts

Posted - 2012-09-07 : 14:16:15
Your math is off

-Chad
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 14:20:04
What information does the customer provide when they want to buy? Just the amount, or do they specify which ID's they want to buy from?

If they can specify the ID's, do they have to buy in the order of the ID's? In other words, can the customer decided that s/he wants to buy from 4, then from 3, then from 2, and then from 1 (in which case 1 would get stopped).


Go to Top of Page

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2012-09-08 : 05:30:15
Yea forget about my math :)
It's random. The customer just decides that he wants to buy everything from the customers up to 250,000 and doesn't specify the customers (id).
Go to Top of Page
   

- Advertisement -