| Author |
Topic |
|
cardullo4321
Starting Member
40 Posts |
Posted - 2011-09-02 : 21:33:09
|
| I have two tables. In Table B, I need to select the most recent by Lookup# in Table A as a derived column. I tried the top one but it does not always pull Year 2011. Table ALookup# Year Rate21008 2011 .0821008 2010 .0741222 2010 .0941222 2011 .10Table BLookup# Description 21008 Apple41222 BananaFinal Results NeededLookup# Description Rate 21008 Apple .0841222 Banana .10Gregory Cardullo |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-02 : 21:39:17
|
| [code]SELECT b.[Lookup#],b.Description,a.RateFROM TableB bINNER JOIN TableA aON a.[Lookup#] = b.[Lookup#]INNER JOIN (SELECT [Lookup#],MAX(Year) AS Latest FROM tableA GROUP BY [Lookup#])a1ON a1.[Lookup#] = a.[Lookup#]AND a1.Latest = a.Year[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-02 : 21:42:38
|
if in sql 2005 or above useSELECT b.[Lookup#],b.Description,a.RateFROM TableB bINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY [Lookup#] ORDER BY [Year] DESC) AS Seq,* FROM TableA) aON a.[Lookup#] = b.[Lookup#]AND Seq=1 or SELECT b.[Lookup#],b.Description,a.RateFROM TableB bCROSS APPLY (SELECT TOP 1 * FROM TableA WHERE [Lookup#] = b.[Lookup#] ORDER BY [Year] DESC) a ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cardullo4321
Starting Member
40 Posts |
Posted - 2011-09-03 : 12:25:49
|
| I am getting confused with all the A and Tables A. Can you help me with this information below?I have two tables called Mapping and WW_Report. I am trying to lookup the most recent rate in the Mapping table and match it to the WW_Report so I can calculate the expense. The mapping table may not have a UBEN that has been used previously so I am okay if NULL is pulled. WW_ReportUBEN Revenue21008, 1021009, 2521010, 40MappingUBEN Title Year Split21008, Bear, 2010, .521008, Dog, 2011, .721009, Cat, 2010, .921009, Tiger,2011, .8Final ResultsUBEN Revenue Rate21008, 10,.721009, 25,.821010, 40, NULLGregory CardulloGregory Cardullo |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-03 : 12:40:24
|
| [code]SELECT b.UBEN,b.Revenue,a.SplitFROM WW_Report bLEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY UBEN ORDER BY [Year] DESC) AS Seq,* FROM mapping) aON a.UBEN = b.UBEN AND Seq=1SELECT b.UBEN,b.Revenue,a.RateFROM WW_Report bOUTER APPLY (SELECT TOP 1 * FROM Mapping WHERE UBEN = b.UBEN ORDER BY [Year] DESC) a[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
cardullo4321
Starting Member
40 Posts |
Posted - 2011-09-03 : 13:04:23
|
| Thanks alot. I was wondering if I want to lookup more columns from the Mapping table such as Provider, Studio, MinRate, would I have to add it to both select statements? Is this query creating some derived table and then doing a lookup off the derived? Do you have a link or any information on how I can understand how this works. I been using Top 1 to handle most of my lookups.Gregory Cardullo |
 |
|
|
cardullo4321
Starting Member
40 Posts |
Posted - 2011-09-03 : 13:09:23
|
| I see that you provided two ways of handling the query and not two select statements. So nevermind the first question.Gregory Cardullo |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-03 : 13:18:09
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|