In PostgreSQL, to select distinct combinations of multiple columns, you can use the DISTINCT keyword followed by the column names separated by commas. Here's the general syntax:
SELECT DISTINCT column1, column2, column3
FROM table_name;
This query will return only unique combinations of values from the specified columns (column1, column2, column3, etc.) in the table_name.
Example:
Suppose you have a sales table with the columns product_id, store_id, and sale_date, and you want to get all distinct combinations of product_id and store_id:
SELECT DISTINCT product_id, store_id
FROM sales;
This will return all unique pairs of product_id and store_id from the sales table, eliminating any duplicates.
Notes:
- If any of the columns have
NULLvalues, these will also be treated as distinct, even though they represent missing data. - You can add more columns to the list to get distinct combinations across multiple fields.
No comments:
Post a Comment