Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying get the number of unique ip_addresses (in this case '3'). The table looks like this:


CREATE TABLE bandits (
  key text NOT NULL,
  ip_address inet,
  offence text,
  count bigint DEFAULT 1);


COPY bandits (key, ip_address, offence, count) FROM stdin;  testing  1  testing  3  testing2 1  testing  1
share|improve this question
Try select distinct ..... – John Gardeniers Feb 15 '11 at 1:50
up vote 5 down vote accepted
SELECT COUNT(DISTINCT ip_address) FROM bandits
share|improve this answer
wow, thank you, I was trying SELECT DISTINCT COUNT[...] etc. Sometimes I'm just unseeing... – Tie-fighter Feb 15 '11 at 2:07

As mentioned here:, it could be much faster to use a bit longer version instead:

SELECT count(*) FROM (SELECT DISTINCT ip_address FROM bandits) AS bandits_distinct
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.