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.
| Author |
Topic |
|
paulgrooby
Starting Member
1 Post |
Posted - 2009-11-26 : 01:22:50
|
| This one has me stumped completely = I have a table that looks like the following:Id, AgreementNumber, ItemNumber,PurchasePrice, ValidfromSample data looks like this:1 230000 AB123 15.25 1/01/20092 230000 AB123 17.5 15/02/20093 230000 AB123 18.56 10/08/20094 230000 PG1234 10.56 1/02/20095 230000 PG1234 11.56 5/03/20096 230001 AB123 10.56 10/01/20097 230001 AB123 15.65 10/02/20098 230001 AB123 14.23 4/03/20099 230000 ZP456 10.56 6/05/200910 230000 ZP456 15.26 7/06/200911 230000 ZP456 14.25 10/07/200912 230000 AB123 15.23 1/08/2009What I'm trying to do is get the price at a point in time for the Agreement and Item, but also join it back so I get the previous price (assumption is that my dates and records always have enough data that I dont get a null for pervious date).I can get the first part which is the max date per Agreement/Item Select T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom = (Select MAX(ValidFrom) from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber) produces : AgreementNo Itemnumber Validfrom Purchaseprice230001 AB123 2009-03-04 00:00:00.000 14.23000230000 ZP456 2009-07-10 00:00:00.000 14.25000230000 PG1234 2009-03-05 00:00:00.000 11.56000230000 AB123 2009-08-10 00:00:00.000 18.56000 But how dow I get the previous price/valid from on the same line - got me stumped - Appreciate the help on the one.P |
|
|
kbhere
Yak Posting Veteran
58 Posts |
Posted - 2009-11-26 : 01:32:02
|
| Select T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom = (Select TOP 2 with ties ValidFrom from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber ORDER BY ValidFrom DESC) Try This..Balaji.K |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-30 : 04:54:44
|
quote: Originally posted by kbhere Select T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom = (Select TOP 2 with ties ValidFrom from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber ORDER BY ValidFrom DESC) Try This..Balaji.K
It should beSelect T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom in (Select TOP 2 with ties ValidFrom from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber ORDER BY ValidFrom DESC) Becuase subquery returns two values and you can't use = for thatMadhivananFailing to plan is Planning to fail |
 |
|
|
kbhere
Yak Posting Veteran
58 Posts |
Posted - 2009-12-01 : 03:35:32
|
quote: It should beSelect T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom in (Select TOP 2 with ties ValidFrom from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber ORDER BY ValidFrom DESC) Becuase subquery returns two values and you can't use = for thatMadhivananFailing to plan is Planning to fail
Ya.. That is right.. I have missed out that unknowingly..Thanks for your correction..K.Balaji |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-01 : 07:25:09
|
quote: Originally posted by kbhere
quote: It should beSelect T1.AgreementNo,T1.Itemnumber,T1.Validfrom ,T1.Purchaseprice from tbl_PurchaseAgreements T1 where T1.ValidFrom in (Select TOP 2 with ties ValidFrom from tbl_purchaseagreements T2 where T1.Agreementno = t2.agreementno and t1.itemnumber = t2.Itemnumber ORDER BY ValidFrom DESC) Becuase subquery returns two values and you can't use = for thatMadhivananFailing to plan is Planning to fail
Ya.. That is right.. I have missed out that unknowingly..Thanks for your correction..K.Balaji
No problem MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|