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 |
DeNam
Starting Member
15 Posts |
Posted - 2013-10-26 : 14:59:42
|
Hi,I want to join a table with different vehicles and vehicle ages with a table containing market values.If i have a motorcycle that is 9 months old, I would like it to choose a residual value of 72 (i.e. the middle of row 3 and 4).How can i do this?Brand code Brands Months Residual Value2 Motorcycles 0 802 Motorcycles 1,5 772 Motorcycles 6 742 Motorcycles 12 702 Motorcycles 18 642 Motorcycles 24 582 Motorcycles 30 522 Motorcycles 36 462 Motorcycles 42 402 Motorcycles 48 332 Motorcycles 54 272 Motorcycles 60 222 Motorcycles 66 182 Motorcycles 72 142 Motorcycles 78 122 Motorcycles 84 102 Motorcycles 90 83 Cars 0 803 Cars 1,5 793 Cars 6 723 Cars 12 653 Cars 18 603 Cars 24 573 Cars 30 523 Cars 36 493 Cars 42 443 Cars 48 423 Cars 54 373 Cars 60 353 Cars 66 313 Cars 72 283 Cars 78 263 Cars 84 233 Cars 90 16 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-26 : 15:31:01
|
why is second row alone having value in different format (1,5)? is it intentional?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
DeNam
Starting Member
15 Posts |
Posted - 2013-10-26 : 16:03:30
|
Yes, this is intentional.Means 1.5 months old. Anyway, how can i solve my problem? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-27 : 02:26:19
|
[code]DECLARE @Brands varchar(100)--Pass Brand value through thisDECLARE @Age int -- Pass age value through it;With CTEAS(SELECT ROW_NUMBER() OVER (PARTITION BY Brands ORDER BY Months) AS Seq,*FROM Table)SELECT (t1.Value + COALESCE(t2.Value,0))/ (1 + CASE WHEN t2.Value IS NULL THEN 0 ELSE 1 END)FROM CTE t1LEFT JOIN CTE t2ON t2.Brands = t1.BrandsAND t2.Seq = t1.Seq + 1WHERE @Age BETWEEN t1.Months AND COALESCE(t2.Months,@Age)AND t1.Brands = @Brands[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|