Home Products QA Support Contact

Count Distinct Postgresql

Sometimes you want to count a the distinct set of rows returned in a complex query. There's no select count(distinct x, y). You need to do someting like the following:

select count(*) from (select distinct chapter, verse from book where word = 'word') as foo

You should note, however, that the distinct command often is unnessesary and considered a bad practice by many programmers. Select distinct can always be emulated by select ... group by