Quantcast
Channel: DISTINCT on one column and return TOP rows - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by SqlWorldWide for DISTINCT on one column and return TOP rows

$
0
0

Replace your dbname and schemaName in the following query.

;WITH CTE AS (SELECT         [Order_No]      ,[Customer_Name]      ,[Purchase_Cost]     , ROW_NUMBER() OVER(PARTITION BY [customer Name] ORDER BY [Purchase Cost] DESC) AS "RowNumber"  FROM [dbname].[schemaName].[PurchaseTable]  )  SELECT TOP(3)       [Order_No]      ,[Customer_Name]      ,[Purchase_Cost]  FROM CTE WHERE RowNumber=1  ORDER BY [Purchase_Cost] DESC

I am sure there are other ways of doing the same. I suggest you read this.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>