Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

DISTINCT on one column and return TOP rows

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.

Tags:

Sql Server

Distinct

Top

Related

Difference between star schema and data cube? MySQL commits taking time Difference between inline view and WITH clause? How to copy migrate data to new tables with identity column, while preserving FK relationship? Select a CSV string as multiple columns Unable to truncate transaction log, log_reuse_wait_desc - AVAILABILITY_REPLICA impdp stuck on index import Problem getting zips in radius via MySQL What actually is a SQL clause? PostgreSQL 9.3.13, How do I refresh Materialised Views with different users? How to set a search_path default on a psql cmd execution? Why does a subquery reduce the row estimate to 1?

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy