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
 Transact-SQL (2000)
 SQL Select problem

Author  Topic 

liuyang
Starting Member

6 Posts

Posted - 2003-01-29 : 22:54:47
Dear experts:
i have a table call productpricehistory which consists three columns
productID,PublishDate,Price
there are many records with same productID but different PublishDate
so how to get a list with the latest publishdate for each product
thank you

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-01-29 : 23:05:17
SELECT ProductID, Max(PublishDate) FROM ProductPriceHistory
GROUP BY ProductID


Go to Top of Page

liuyang
Starting Member

6 Posts

Posted - 2003-01-29 : 23:13:30
Thanks but if i also want to get the latest price for each product then what should i do?
there is an error of:
Column 'Price' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
when i add price in

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-01-29 : 23:13:52
You probably want the price as well?

SELECT ProductID, PublishDate, Price
FROM ProductPriceHistory t1
where t1.PublishDate = (select max(t2.PublishDate) from ProductPriceHistory t2 where t2.ProductID = t1.ProductID)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

liuyang
Starting Member

6 Posts

Posted - 2003-01-29 : 23:20:46
Thank you very much

Go to Top of Page
   

- Advertisement -