DISTINCT for a Specific Column
Given a query that retrieves multiple columns, such as ID, Email, ProductName, and ProductModel from the Products table, how can it be modified to display unique Email values only?
To address this, SQL Server 2005 and later versions offer a solution:
This query uses the ROW_NUMBER() function to assign a row number to each row within each Email partition, ordered in descending order of ID. The rn column then identifies which row should be included in the final result, as only those with rn equal to 1 will be selected.
To further refine the query, a WHERE clause can be added to filter the rows based on specific criteria. For instance, to include only rows with a specific ProductModel and whose ProductName contains a certain substring:
The above is the detailed content of How Can I Retrieve Only Unique Email Addresses from a Multi-Column SQL Query?. For more information, please follow other related articles on the PHP Chinese website!