Thursday, July 2, 2009

Count Distinct on Multiple Columns

Some days back I figured following is a valid query. It can be used to get count of distinct occurrences of a column.

select count(distinct colname) from tablename;

But it would not take more than one column. To look up distinct records based on more than one column we can use Oracle's concatenate function - "||". Thus all the columns will be treated as a single entity.

select count(distinct colname1 || colname2 || colname3) from tablename;

A good example where we would want to use above query is to get distinct customer orders from a transaction table that contains customer-name,order-date and order-status.

No comments:

Post a Comment