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 |
AllanRidley
Starting Member
5 Posts |
Posted - 2012-08-13 : 05:19:47
|
Hi AllI have a database that has (amongst others)Products Table--------------IDNameSales Table--------------IDProductIDDateSoldI want to pull a report which shows the last 3 products sold (last based on the sold date). I don't want any productIDs duplicated, though, so if the sales table looks like this:ID ProductID DateSold1 2 2012-08-13 0:042 2 2012-08-13 0:033 3 2012-08-13 0:024 1 2012-08-13 0:01I would like my report to showProductID231NotProductID223Does that make sense?Thanks! |
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2012-08-13 : 06:27:01
|
DECLARE @Products TABLE (ID INT, ProductID INT, DateSold DATETIME)INSERT INTO @ProductsSELECT 1, 2, '2012-08-13 0:04' UNION ALLSELECT 2, 2, '2012-08-13 0:03' UNION ALLSELECT 3, 3, '2012-08-13 0:02' UNION ALLSELECT 4, 1, '2012-08-13 0:01'SELECT ProductID FROM @ProductsGROUP BY ProductIDORDER BY Max(DateSold) DESC--------------------------http://connectsql.blogspot.com/ |
 |
|
AllanRidley
Starting Member
5 Posts |
Posted - 2012-08-13 : 06:54:01
|
Perfect! Thanks :) |
 |
|
|
|
|