Solved

# SQL Function for Price Bands

Posted on 2012-08-17
818 Views
hello

I have a requirement to return a total price based on a table of different prices which vary by quantity.

see table
1      2            4
Start      Finish            price      qty            price*qyt
1      10            0.00      10.00      0.00
11      30            300.00      20.00      6,000.00
31      60            420.00      30.00      12,600.00
61      100            480.00      40.00      19,200.00
101      150            600.00      28.00      16,800.00
151      500            600.00      0.00      0.00
Total      54,600.00
the above exampe shows the total if I buy 128 apples.

so the first 10 are free, then 11 to 30 are priced at 300 each, next 31 to 60 at 420 etc

the price bands and price will be stored in a table and the quantity I want to buy will be between 1 and 500.

I think I need a loop to work out each band, accumulate the total price, deduct the quantity at that price and loop until the quantity is zero.

thanks for any help,

graham
0
Question by:Graham_Forbes

LVL 11

Expert Comment

I don't understand a few things:

First, it seems odd that the more you buy, the more they cost.  That's not the real issue here though.  The thing I'm questioning is that from this example, you have 10 out of a total 10 in the first group, but then only 20 out of 30 in the second group, 30 out of 60 in the next, 40 out of 100 in the next, etc.  Wouldn't you use 10/10 in the first, 30/30 in the second, 60/60 in the third and 28/100 in the fourth?
0

Author Comment

you always get the first 10 free, then you get the next 20 at 300 (the 20 is calculated by take the number costed so far (10) and decucting the finish band quantity (30). then the next band is priced at 420 and the quantity is quantity so far (30) and deducct the finish band quantity (60), so on until you have priced all the required quantity

thanks
0

Author Comment

see attachment for code to create and populate the table with the example above
CreateTableAndInsertData.txt
0

LVL 11

Accepted Solution

Use this to create a function.

``````SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION dbo.GetTotalCost
(
@Qty int
)
RETURNS float
AS
BEGIN
declare @tempQty int
declare @totalprice float
declare @curmax integer

set @tempQty = @Qty

set @totalprice = 0
set @curmax = 0

while @tempQty <= @Qty and @tempQty > 0
begin
select @curmax = min(maxqty) from priceband where maxqty > @curmax
if @tempQty >= @curmax
select @totalprice = @totalprice + (maxqty * price) from priceband where maxqty = @curmax
else
select @totalprice = @totalprice + (@tempQty * price) from priceband where maxqty = @curmax
set @tempQty = @tempQty - @curmax
end

return @totalprice

END
GO
``````

To call it:

``````select dbo.GetTotalCost (128)
``````
0

LVL 1

Expert Comment

I created a table bands.  It has 3 columns, ID, quantity and price.  the data looks like this.

id      quantity      price
1      10      0.00
2      30      300.00
3      60      420.00
4      100      480.00
5      150      600.00
6      500      600.00

I then created a scalar funtion

create FUNCTION [dbo].[getTotal]
(
@quantity int
)
RETURNS money
AS
BEGIN
DECLARE @total as money
declare @top as int
declare @bottom as int
declare @curPrice as money

set @total = 0
while @quantity > 10
begin
set @top = (select min(quantity) from bands where quantity >= @quantity)
set @bottom = (select MAX(quantity) from bands where quantity < @quantity)
set @curPrice = (select Price from bands where quantity = @top)
set @total = @total + @curPrice * (@quantity - @bottom)
set @quantity = @quantity - (@quantity - @bottom)
end

RETURN @total

END

Ending the loop requires a little thought.  I solved it by looping until quantity was less than 10.  If you don't like that, you will have to put in a check for @bottom = null and or add a 0 record or something to prevent a null value on one of the band lines.
0

LVL 11

Expert Comment

Oh, I forgot to mention that I added a field to the table called maxqty.  With each band, there is a maximum quantity that can be used:

1-10 = 10
11-30 = 20
31-60 = 30
61-100 = 40
101-150 = 50
151-500 = 350
0

Author Closing Comment

perfect - thanks for this, all worked once I added the new column.
0

LVL 26

Expert Comment

I know the question is answered but I still want to post a solution that doesn't use a loop. I knew this was possible with a simple select query but I didn't have time for it until now. I made some corrections to the table because it had some errors in terms of bands. The fact that I added an ID column is irrelevant, it would work without it as well. Here it is:

``````CREATE TABLE [dbo].[tblPriceBandLookup](
[id] int identity,
[StartBand] [int] NULL,
[FinishBand] [int] NULL,
[Price] [int] NULL
) ON [PRIMARY]
GO

INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (1, 10, 0)
INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (11, 30, 300)
INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (31, 60, 420)
INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (61, 100, 480)
INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (101, 150, 600)
INSERT INTO tblPriceBandLookup (StartBand, FinishBand, Price) VALUES     (151, 500, 700)
GO

declare @quant int
select @quant=47

select
sum(case when @quant>FinishBand then FinishBand-StartBand+1 else @quant-StartBand+1 end*Price) cost
from
[dbo].[tblPriceBandLookup]
where
StartBand<=@quant
``````
0

LVL 26

Expert Comment

Given the simplicity of the select to retrieve the cost it is not necessary a function, the query could be used like given or in a join, depending on the needs. Anyway here it is the function if preferred:

``````CREATE FUNCTION dbo.GetTotalCost
(
@quant int
)
RETURNS float
AS
BEGIN

return
(
select
sum(case when @quant>FinishBand then FinishBand-StartBand+1 else @quant-StartBand+1 end*Price) cost
from
[dbo].[tblPriceBandLookup]
where
StartBand<=@quant
)
END
GO
``````
0

LVL 26

Expert Comment

This code is extremely useful for situations where gradual factors are applied, like for instance to calculate income taxes or in payroll.
0

## Featured Post

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…