DISTINCT on one column and return TOP rows
How do you query for three unique customers with the largest Purchase_Cost?I want to apply the DISTINCT only on Customer_Name, but the query below applies the distinct on all three columns. How should...
View ArticleAnswer by SqlWorldWide for 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]...
View ArticleAnswer by Fatih for DISTINCT on one column and return TOP rows
please try:SELECT DISTINCT TOP 3 order_no, customer_name, Purchase_CostFROM( SELECT order_no, customer_name, Purchase_Cost, ROW_NUMBER() OVER(PARTITION BY customer_name ORDER BY Purchase_Cost DESC)...
View ArticleAnswer by Jonathan Roberts for DISTINCT on one column and return TOP rows
I think this could be more efficient that the other solutions, provided you have an index on: (Customer_Name, Purchase_Cost DESC) INCLUDE (Order_No);WITH PurchaseTable AS( SELECT * FROM (VALUES...
View Article