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
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic Pricing Lookup

Author  Topic 

jmarkee
Starting Member

9 Posts

Posted - 2010-08-09 : 23:37:40
I am trying to create a dynamic billing system with the following two tables (Pricing and Volume). How do I do a lookup of the volume for each customer and tell me what price they should be paying based on the pricing table?

Pricing Table
Volume | Price
<100 | $10
<200 | $20
<300 | $40

Volume Table
Client | Volume
name1 | 50
name2 | 135
name3 | 285


Thanks!

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-08-09 : 23:42:32
sounds suspiciously like a homework question.

start by making the pricing field an integer (or decimal) data type.

your formula has those who buy the least getting the best price?!
Go to Top of Page

jmarkee
Starting Member

9 Posts

Posted - 2010-08-09 : 23:46:51
Not homework :). I greatly simplflied so it the question would be clear. Yeah i made up the pricing and Im tired :). Not sure I follow your comment on making the pricing field an integer.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-10 : 02:31:00
"Not sure I follow your comment on making the pricing field an integer."

Remove the "<" from the front, and change datatype to Integer, then you could use it for numeric comparison.

ROW_NUMBER ... OVER will give you the Pricing.Price for the MAX Pricing.Volumn (i.e. for the specific Volume.Volume) including a JOIN to the Volume table
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-10 : 02:50:07
[code]
select *
from Volume v
cross apply
(
select top 1 Price
from Price x
where v.Volume <= x.Volume
order by Volume
) p
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-10 : 03:37:00
Do you know if that is faster than ROW_NUMBER ... OVER? (I presume so, but have not tested it)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-10 : 03:47:09
quote:
Originally posted by Kristen

Do you know if that is faster than ROW_NUMBER ... OVER? (I presume so, but have not tested it)


I don't know. But i presume you will test it ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-10 : 04:12:17
Darn it ...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-10 : 04:26:25
Not sure of best way to do it with ROW_NUMBER now I've looked at it. Here's a framework though:

CREATE TABLE #Pricing
(
P_Volume int NOT NULL,
P_Price money NOT NULL,
PRIMARY KEY
(
P_Volume
)
)

INSERT INTO #Pricing(P_Volume, P_Price)
SELECT 100, 10.00 UNION ALL
SELECT 200, 20.00 UNION ALL
SELECT 300, 40.00

CREATE TABLE #Volume
(
V_Client varchar(20) NOT NULL,
V_Volume int NOT NULL,
PRIMARY KEY
(
V_Client
)
)

INSERT INTO #Volume(V_Client, V_Volume)
SELECT 'name1', 50 UNION ALL
SELECT 'name2', 135 UNION ALL
SELECT 'name3', 285

-- SET SHOWPLAN_TEXT ON
GO
-- SET STATISTICS IO ON; SET STATISTICS TIME ON

PRINT 'KHTan'
SELECT *
FROM #Volume
CROSS APPLY
(
SELECT TOP 1 P_Price
FROM #Pricing
WHERE V_Volume <= P_Volume
ORDER BY P_Volume
) AS P

/**
Table '#Pricing'. Scan count 3, logical reads 6
Table '#Volume'. Scan count 1, logical reads 2

Nested Loops(Inner Join, OUTER REFERENCES:([tempdb].[dbo].[#Volume].[V_Volume]))
|--Clustered Index Scan(OBJECT:([tempdb].[dbo].[#Volume]))
|--Top(TOP EXPRESSION:((1)))
|--Clustered Index Seek(OBJECT:([tempdb].[dbo].[#Pricing]), SEEK:([tempdb].[dbo].[#Pricing].[P_Volume]
>= [tempdb].[dbo].[#Volume].[V_Volume]) ORDERED FORWARD)
**/



GO
SET STATISTICS IO OFF; SET STATISTICS TIME OFF
GO
SET SHOWPLAN_TEXT OFF
GO
DROP TABLE #Volume
GO
DROP TABLE #Pricing
GO
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-08-10 : 05:16:19
My pathetic attempt at this one

;with cte
as
(
select *,ROW_NUMBER()over(order by P_volume)as id from #Pricing
),cte1
as
(
select *,ROW_NUMBER()over(order by V_volume)as id from #Volume
)
select V_Client,V_Volume,P_Price from cte C inner join cte1 C1 on C.id=C1.id




Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

jmarkee
Starting Member

9 Posts

Posted - 2010-08-10 : 17:00:01
Thanks everyone! I was able to get these suggestions working.
Go to Top of Page
   

- Advertisement -