Author |
Topic |
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 03:18:15
|
hiI HAVE created procedure with this procedure i will get only the greatest/highest amount for eg: bal 14 10.00002.00002.0000i have 7 in the table data i should get 7+7=14 this is correct (because least currency notes)CREATE procedure bal (@Amount money)as begindeclare @Note smallmoneyDECLARE @Notes TABLE (Note smallmoney NOT NULL)WHILE @Amount > 0BEGINSELECT @Note = MAX(FaceValue) FROM dbo.BankNotesWHERE FaceValue <= @Amount and Available > 0INSERT INTO @Notes (Note)SELECT @NoteUPDATE dbo.BankNotesSET Available = Available - 1WHERE FaceValue = @NoteSET @Amount = @Amount - @NoteENDSELECT * FROM @NOTESENDGOthanxs once again Sergiythanxsdatabase |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-17 : 03:33:07
|
Peter, Have you changed your name to "Sergiy"? Apparently, your logic was way too hi-fi for him/her to understand Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 03:37:20
|
No, haven't changed name. I think someone posted to him by message or email and he doesn't want to share with us.He rewrote the code with his own column names and table names, so I don't think so.I think the problem is that he does not know exactly what he wants.If he did, he could write the problem statement more clearly... If the problem is that he want the algorithm "reverted", that is, he wants all the lower bills first, why doesn't he tell us?And more valuable, why would he want that? Then all there is, is to return everything in 1 rupee bills!Above he writes about "7" bills. Is there anywhere in the world bills with the value of 7?Peter LarssonHelsingborg, Sweden |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-17 : 03:44:01
|
quote: Is there anywhere in the world bills with the value of 7?
At least not here in India But even that was handled in your previous SP, right?I mean the only change he has to make is to add an entry for Rs.7 in the @CashCounter table, Isn't it?Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 03:46:48
|
Seems right to me. But that case (with 7) only happens when the 10's are out.Peter LarssonHelsingborg, Sweden |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-17 : 03:55:56
|
I think this is just some kind of SQL puzzle, nothing to do with real-time scenario. Although his condition can be incorporated but that will unncessarily complicate the logic.Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 04:19:44
|
I got an idea for the experiment above. If he adds a new bit column named PREFERRED, and change the MAX query toSELECT TOP 1 @Note = FaceValue FROM dbo.BankNotesWHERE FaceValue <= @Amount and Available > 0ORDER BY Preferred DESC, FaceValue DESC he can easily switch between which notes that are to be taken first.Peter LarssonHelsingborg, Sweden |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-17 : 05:39:55
|
Ingenious indeed ! (Of course, if he prefers to add PREFERRED column to the table)I think this problem is somewhat similar to Dijkstra's shortest path problem or mimimum spanning tree problemHarsh AthalyeIndia."Nothing is Impossible" |
 |
|
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 05:44:35
|
hii did not change my name i just appreciated sergiy for the response/reply once againsee i will put in a brief note what i need if x person goes for shopping when he/she pays bill at cash counter his bill amount is 986.But the customer x has 1000 rupees in his hand and he pays the bill he should get back/balance amount 14 rupees back rightin this scenario the cash counter should give x person the least or less number of notes best probabilitieseg: in cash counter he has currency demoniation 1000,500,100,50,20,10,9,7,5,2,1 etc rupee currency noteswhere 10+2+2=14 or 2+2+2+2+2+2+2=14 logically this correct but actually cash counter should give customer 7+7=14 because this is the least/less currency demoniation notes(count)i should have a query which will fetch least/less number(count) of currency notes note: cash counter should give minimum number of currency notesi think we should use function/dynamic query array algorithm this my script / procedureCREATE procedure bal (@Amount money)as begindeclare @Note smallmoneyDECLARE @Notes TABLE (Note smallmoney NOT NULL)WHILE @Amount > 0BEGINSELECT @Note = MAX(FaceValue) FROM dbo.BankNotesWHERE FaceValue <= @Amount and Available > 0INSERT INTO @Notes (Note)SELECT @NoteUPDATE dbo.BankNotesSET Available = Available - 1WHERE FaceValue = @NoteSET @Amount = @Amount - @NoteENDSELECT * FROM @NOTESENDGOwhen i run this i get output>bal 1410.00002.00002.0000in my database i have different demoniation for 14 rupees best possibilities is 7+7=14 not 10+2+2=14 or 2+2+2+2+2+2+2=14because i get 3 or 7 currency notes (count) but 7+7=14 is correct because it returns 2 currency notes(count)thanks in advance all of u database |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 05:49:31
|
Why not 9 and 5? This is two notes (count) also...Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 06:05:51
|
Got it covered now. If 9 and 5 can be treated equally as 7 and 7, just remove the WHERE part with POWER.-- Prepare test dataDECLARE @Bills TABLE (Value INT)INSERT @BillsSELECT 1000 UNION ALLSELECT 500 UNION ALLSELECT 100 UNION ALLSELECT 50 UNION ALLSELECT 20 UNION ALLSELECT 10 UNION ALLSELECT 9 UNION ALLSELECT 7 UNION ALLSELECT 5 UNION ALLSELECT 2 UNION ALLSELECT 1 UNION ALLSELECT 0-- Stage the dataDECLARE @Amount INTSELECT @Amount = 14DECLARE @Stage TABLE (Bill1 INT, Bill2 INT, Bill3 INT, Bill4 INT, Bill5 INT)INSERT @StageSELECT TOP 1 b1.Value, b2.Value, b3.Value, b4.Value, b5.ValueFROM @Bills b1CROSS JOIN @Bills b2CROSS JOIN @Bills b3CROSS JOIN @Bills b4CROSS JOIN @Bills b5WHERE b1.Value + b2.Value + b3.Value + b4.Value + b5.Value = @AmountORDER BY SIGN(b1.Value) + SIGN(b2.Value) + SIGN(b3.Value) + SIGN(b4.Value) + SIGN(b5.Value), POWER(b1.Value, 2) + POWER(b2.Value, 2) + POWER(b3.Value, 2) + POWER(b4.Value, 2) + POWER(b5.Value, 2)-- Show the dataSELECT COUNT(*) Items, d.BillFROM ( SELECT Bill1 Bill FROM @Stage UNION ALL SELECT Bill2 FROM @Stage UNION ALL SELECT Bill3 FROM @Stage UNION ALL SELECT Bill4 FROM @Stage UNION ALL SELECT Bill5 FROM @Stage ) dWHERE d.Bill > 0 GROUP BY d.Bill Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 06:21:03
|
good i did not think of thisdatabase |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 06:28:37
|
I realize you didn't. That's why you ask for help in all places.Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 06:58:16
|
Here is another alternative of you want all possible solutions for change.-- Prepare test dataDECLARE @Bills TABLE (Value INT)INSERT @BillsSELECT 1000 UNION ALLSELECT 500 UNION ALLSELECT 100 UNION ALLSELECT 50 UNION ALLSELECT 20 UNION ALLSELECT 10 UNION ALLSELECT 9 UNION ALLSELECT 7 UNION ALLSELECT 5 UNION ALLSELECT 2 UNION ALLSELECT 1 UNION ALLSELECT 0-- Stage the dataDECLARE @Amount INTSELECT @Amount = 14DECLARE @Stage TABLE (Row INT IDENTITY(0, 1), Bill1 INT, Bill2 INT, Bill3 INT, Bill4 INT, Bill5 INT)INSERT @Stage ( Bill1, Bill2, Bill3, Bill4, Bill5 )SELECT b1.Value, b2.Value, b3.Value, b4.Value, b5.ValueFROM @Bills b1CROSS JOIN @Bills b2CROSS JOIN @Bills b3CROSS JOIN @Bills b4CROSS JOIN @Bills b5WHERE b1.Value + b2.Value + b3.Value + b4.Value + b5.Value = @AmountORDER BY SIGN(b1.Value) + SIGN(b2.Value) + SIGN(b3.Value) + SIGN(b4.Value) + SIGN(b5.Value), POWER(b1.Value, 2) + POWER(b2.Value, 2) + POWER(b3.Value, 2) + POWER(b4.Value, 2) + POWER(b5.Value, 2)DECLARE @Temp TABLE (Row INT, Bill INT, Items INT)INSERT @Temp ( Row, Bill, Items )SELECT d.Row, d.Bill, COUNT(*) ItemsFROM ( SELECT Row, Bill1 Bill FROM @Stage UNION ALL SELECT Row, Bill2 FROM @Stage UNION ALL SELECT Row, Bill3 FROM @Stage UNION ALL SELECT Row, Bill4 FROM @Stage UNION ALL SELECT Row, Bill5 FROM @Stage ) dWHERE d.Bill > 0 GROUP BY d.Row, d.BillDECLARE @Buffer TABLE (Row INT, Bill INT, Items INT)INSERT @buffer ( Row, Bill, Items )select t1.row, t1.bill, t1.itemsfrom @temp t1inner join @temp t2 on t2.bill = t1.bill and t2.items = t1.itemsgroup by t1.row, t1.bill, t1.itemshaving t1.row = min(t2.row)delete bfrom @buffer bwhere b.row in (select a.row from @buffer a group by a.row having sum(a.bill * a.items) <> @amount)select (SELECT COUNT(DISTINCT b.Row) FROM @Buffer b WHERE b.Row <= q.Row) Alternative, q.Items, q.Billfrom @Buffer qorder by (select count(*) from @buffer b where b.row = q.row) Peter LarssonHelsingborg, Sweden |
 |
|
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 07:02:57
|
hithanxs peter i am dry running this i think this is the solution i needed ,i will confrim it and let u knowthanxsdatabase |
 |
|
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 09:13:16
|
hithanxs peter i dry ran this Posted - 10/17/2006 : 06:05:51 i think there is a SMALL BUGcomment ----SELECT 100 UNION ALL give SELECT @Amount = 213 or 211 it does not retrun any value null data is retruned thanxsdatabase |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-17 : 10:03:38
|
That's because you now need 6 notes to give back.Peter LarssonHelsingborg, Sweden |
 |
|
oracle_corrgi
Yak Posting Veteran
98 Posts |
Posted - 2006-10-17 : 21:17:15
|
hi Peter Good morning my question i think there is a SMALL BUGcomment ----SELECT 100 UNION ALL give SELECT @Amount = 213 or 211 it does not retrun any value null data is retrunedis that output for some amount does not return eg: 950,900,850,800,213,312 etc whyone more question i have created procedure according to Posted - 10/17/2006 : 06:05:51 SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOALTER procedure oldfinbal (@Amount money)asBEGINDECLARE @Bills TABLE (Value INT)DECLARE @Stage TABLE (Bill1 INT, Bill2 INT, Bill3 INT, Bill4 INT, Bill5 INT)INSERT @StageSELECT TOP 1 b1.Value, b2.Value, b3.Value, b4.Value, b5.ValueFROM @Bills b1CROSS JOIN @Bills b2CROSS JOIN @Bills b3CROSS JOIN @Bills b4CROSS JOIN @Bills b5WHERE b1.Value + b2.Value + b3.Value + b4.Value + b5.Value = @AmountORDER BY SIGN(b1.Value) + SIGN(b2.Value) + SIGN(b3.Value) + SIGN(b4.Value) + SIGN(b5.Value), POWER(b1.Value, 2) + POWER(b2.Value, 2) + POWER(b3.Value, 2) + POWER(b4.Value, 2) + POWER(b5.Value, 2)-- Show the dataSELECT COUNT(*) Items, d.BillFROM ( SELECT Bill1 Bill FROM @Stage UNION ALL SELECT Bill2 FROM @Stage UNION ALL SELECT Bill3 FROM @Stage UNION ALL SELECT Bill4 FROM @Stage UNION ALL SELECT Bill5 FROM @Stage ) dWHERE d.Bill > 0 GROUP BY d.BillSELECT * FROM @stageENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOthis also retruns me no values why?thanxsdatabase |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-17 : 22:12:23
|
quote: this also retruns me no values why?
That's because you are inserting nothing in @Bills table ! Insert currencies in @Bills tableHarsh AthalyeIndia."Nothing is Impossible" |
 |
|
Next Page
|