How to count unique values across two columns with certain types in postgresql?

by arnoldo.moen , in category: MySQL , 24 days ago

How to count unique values across two columns with certain types in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 24 days ago

@arnoldo.moen 

To count unique values across two columns with certain types in PostgreSQL, you can use the following query:

1
2
3
SELECT COUNT(DISTINCT (col1, col2))
FROM your_table
WHERE col1::text = 'certain_type' AND col2::text = 'certain_type';


Explanation:

  1. Replace your_table with the name of your table.
  2. Replace col1 and col2 with the names of the columns you want to count unique values for.
  3. Replace 'certain_type' with the specific type you want to filter by. You may need to adjust the type casting function (such as ::text) based on the actual data types of your columns.


This query will count the number of distinct combinations of values in col1 and col2 that match the specified type in both columns.