A: 

I don't know the answer off the top of my head, but my guess is that you mean need to use a sub-query, which could make it a very expensive query.

It may be easier to pull all the data in an use code to post-process it yourself.

Chris Thompson
+2  A: 
select ft.ItemName,
       price
from   (select   ItemName,
                 Max(ItemPriceEffectiveDate) as MaxEff
        from     FunTable
        where    ItemPriceEffectiveDate <= GETDATE()
        group by ItemName) as d,
       FunTable as ft
where  d.MaxEff = ft.ItemPriceEffectiveDate;

Edit: Changed the query to actually work, assuming the price does not change more than once a day.

nos
I'd say this is the most elegant solution
hamlin11
A: 

should work if you do

SELECT ItemName, MAX(ItemPriceEffectiveDate)
FROM FunTable
WHERE ItemPriceEffectiveDate < getDate()
GROUP BY ItemName
Russ Bradberry
You'd want to get the price too. Which means you'd need to group by the price. And that won't give you what you need(unless the price never changes :)
nos
Yep, that's correct
hamlin11
+2  A: 

Something like this (don't have your names, but you get the idea) ...

WITH A (Row, Price) AS
(
  SELECT Row_Number() OVER (PARTITION BY Item ORDER BY Effective DESC) AS [RN],
         Price FROM Table2
  WHERE Effective <= GETDATE()
)
SELECT * FROM A WHERE A.Row = 1
JP Alioto
I believe this answer is correct, though I think the following may be a bit quicker:Select ItemName, ItemPricefrom FunTable f INNER JOIN (Select ItemKey, MAX(ItemPriceEffectiveDate) DateFrom FunTableWhere ItemPriceEffectiveDate <= GetDate()Group by ItemKey) tON f.ItemKEY = t.ItemKey and f.ItemPriceEffectiveDate = t.Date
hamlin11
sorry for the bad formatting, i have no idea how to make it pretty
hamlin11