Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sql Help
Message
 
 
To
18/12/2009 14:14:02
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, United States
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Miscellaneous
Thread ID:
01439765
Message ID:
01439819
Views:
30
The result is indeed correct.
create table #truck (id int, overweight int);
insert #truck values (1,3500);
insert #truck values (2,2500);
insert #truck values (3,1500);
insert #truck values (4,500);
insert #truck values (5,500);
-- Fine Tables

create table #ttt (FineID int identity (1,1), 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);
/*
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 

*/

--select T1.*, F.FineAmount from #truck T1 cross apply (select Fine.FineID *  from #ttt Fine) F 
--(select T1.*, F.* from #truck T cross apply (select Fine.* from #ttt Fine where T.overweight  <= Fine.maxweight or T.overweight ) as F

Select        Truck.*,
               sum(Case When Truck.Overweight > fine.MaxWeight
            Then (MaxWeight - MinWeight + 1) * Fineamount
                        Else (OverWeight - MinWeight + 1) * FineAmount
                        End) As TotalFine
From        #ttt As Fine
               Inner Join #Truck As Truck
                       On Truck.Overweight >= Fine.MinWeight
              group by Truck.id, Truck.overweight    order by Truck.overweight DESC, Truck.ID     
>Still the wrong answer.
>Results
>
>3500 = 50.00 sb 40.00
>2500 = 30.00 sb 22.50
>1500 = 15.00 sb 10.00
>500 = 5.00     sb 2.50 
>
>Sergey's solution clarified by you :).
>
>Thanks.
>
>>It is, but George not always give you a direct solution. You should think a little about it :-)
>>
>>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))
>>insert @ttt values (   1,     1000,0.005)
>>insert @ttt values (1001,     2000,0.01)
>>insert @ttt values (2001,     3000,0.015)
>>insert @ttt values (3001,     4000,0.02)
>>insert @ttt values (4001,     5000,0.025)
>>insert @ttt values (5001,999999999,0.03)
>>
>>Select        Truck.*,
>>               SUM(Case When Truck.Overweight > fine.MaxWeight
>>            Then (MaxWeight - MinWeight + 1) * Fineamount
>>                        Else (OverWeight - MinWeight + 1) * FineAmount
>>                        End) As Fine
>>From        @ttt As Fine
>>               Inner Join @Truck As Truck
>>                       On Truck.Overweight >= Fine.MinWeight
>>GROUP BY Truck.id, Truck.overweight
>>ORDER BY Truck.overweight DESC
>>
>>
>>
>>>That is not correct. This is what the fines should be.
>>>
>>>overweight   Fine amount
>>>3500          $40.00
>>>2500          $22.50
>>>1500         $10.00
>>>500            $2.50
>>>
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform