Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Select 3 distinct products ordered with the orderdate in desc ie. recentlyI wrote the following query but it is returning duplicate productidselect DISTINCT top 3b.productid,a.OrderDate,c.productnamefrom orders as ainner join orderdetails as b on a.orderid = b.orderidinner join products as c on b.productid = c.productidorder by a.orderdate descproductid OrderDate productname----------- ----------------------- --------------------------------------------------2 3004-04-05 00:00:00.000 Product Name 2 1 2009-01-01 00:00:00.000 Product Name 12 2009-01-01 00:00:00.000 Product Name 2
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-01-28 : 12:27:14
did you mean this?
select top 3 with tiesb.productid,a.OrderDate,c.productname,MAX(OrderDate) OVER (PARTITION BY productid) AS Latestfrom orders as ainner join orderdetails as b on a.orderid = b.orderidinner join products as c on b.productid = c.productidorder by Latest desc