Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sql Help
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Divers
Thread ID:
01439765
Message ID:
01439800
Vues:
29
Yes, that was the idea. :)

>Easy :-)
>You just need to join two table by Trucks.Overweight BETWEEN MinWeight AND Maxweight
>And then to sum that additional column to (Trucks.Overweight - MinWeight + 1) * FineRate
>
>Something like this (NOT TESTED!!! and all in T-SQL, sorry)
>
>DECLARE @Truck TABLE (id int, overweight int);
>insert @truck values (1,3500);
>insert @truck values (1,2500);
>insert @truck values (2,1500);
>insert @truck values (2,500);
>insert @truck values (1,500);
>
>DECLARE @ttt table (minweight int, maxweight int, fineamount numeric(8,4), PreviousAmount numeric(8,4));
>insert @ttt values (   1,     1000,0.005, 0)
>insert @ttt values (1001,     2000,0.01 ,1000*0.005)
>insert @ttt values (2001,     3000,0.015,1000*0.005+1000*0.01)
>insert @ttt values (3001,     4000,0.02 ,1000*0.005+1000*0.01+1000*0.015)
>insert @ttt values (4001,     5000,0.025,1000*0.005+1000*0.01+1000*0.015+1000*0.02)
>insert @ttt values (5001,999999999,0.03 ,1000*0.005+1000*0.01+1000*0.015+1000*0.02+1000*0.025)
>
>SELECT Truck.*, TestMe.PreviousAmount + (Truck.overweight-TestMe.minweight+1)*fineamount AS Something
>FROM @Truck Truck
>INNER JOIN @ttt TestMe ON Truck.overweight BETWEEN TestMe.minweight AND TestMe.maxweight
>
>
>
>
>>Hi Sergey
>>
>>Sorry for my ignorance, but I don't see how the extra column will help?
>>
>>>Add 1 more column that would hold cumulative fine for the previous ranges. You can write code that will populate it for you or you can build it on the fly. After that you will need only one record to calculate fine.
>>>
>>>create table #ttt (minweight int, maxweight int, FineRate numeric(8,4), PreviousAmount numeric(8,4));
>>>insert #ttt values (1,1000,.005, 0);
>>>insert #ttt values (1001,2000,.01, 5);
>>>insert #ttt values (2001,3000,.015, 15);
>>>insert #ttt values (3001,4000,.02, 20);
>>>insert #ttt values (4001,5000,.025, 40);
>>>insert #ttt values (5001,999999999,.03, 65);
>>>
>>>
>>>>This is the temp table that I created.
>>>>
>>>>create table #ttt (minweight int, maxweight int, fineamount numeric(8,4));
>>>>insert #ttt values (1,1000,.005);
>>>>insert #ttt values (1001,2000,.01);
>>>>insert #ttt values (2001,3000,.015);
>>>>insert #ttt values (3001,4000,.02);
>>>>insert #ttt values (4001,5000,.025);
>>>>insert #ttt values (5001,999999999,.03);
>>>>
>>>>
>>>>>Can you post complete rate table
>>>>>
>>>>>>I am using a Sybase database but I would think the sql syntax should be almost the same. I have a requirement that I need to calculate fine for overweight trucks. The way the customer wants to do this is to have 1000 pound groups and each group has a progressively higher rate. For example, if a truck is 3500 lbs overweight then the calculation should be:
>>>>>>
>>>>>>1000 * .005   = $5.00
>>>>>>1000 * .01     = 10.00
>>>>>>1000 * .015   = 15.00
>>>>>>500   * .02    = 10.00
>>>>>>for a total of 40.00 
>>>>>>
>>>>>>I tried to create a temp table to hold the rates but I I'm stumped on how to do a join. I was hoping for a SQL solution if it is possible.
>>>>>>
>>>>>>Thanks.
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform