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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Newest price in effect for a given purchase date

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-10 : 07:43:38
Bill writes "I'd like some input on potentially more efficient ways of doing this. It seems overly complicated and I'm hoping there's a better way.

In this code, the following represents:
---------------------------------------
Product = Table with products for sale
ProductPrice = Table with Prices for the products
* Note that the same product purchased at different times may have different prices
** Note that the product may have multiple price entries for the same EffDate - newest UpdateDate should be chosen
*** Since products could have been priced when the first entry for an EffDate was in effect, those records must be retained

[other code here]
from Product p
Inner Join ProductPrice pp On p.a = pp.a
and p.b = pp.b
and p.c = pp.c
and p.PurchaseDate >= pp.EffDate -- Price must have been in effect at this purchase date
and pp.EffDate = (Select Max(EffDate) -- Newest price that was in effect for this product
From ProductPrice pp2
Where p.a = pp2.a
and p.b = pp2.b
and p.c = pp2.c
and pp2.EffDate <= p.PurchaseDate)
and pp.UpdateDate = ( Select Max(UpdateDate) -- Newest price with that effective date
From ProductPrice pp2
Where p.a = pp2.a
and p.b = pp2.b
and p.c = pp2.c
and pp2.EffDate = pp.EffDate)"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-10 : 07:56:55
Refer this and post accordingly
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -