我想获得独特的ip_addresses(在这种情况下是'3')的数量。 表格看起来像这样:
结构体:
CREATE TABLE bandits ( key text NOT NULL, ip_address inet, offence text, count bigint DEFAULT 1);
数据:
复制土匪(键,ip_address,进攻,计数)从标准input; 127.0.0.1_testing 127.0.0.1testing1 127.0.0.2_testing 127.0.0.2testing3 127.0.0.2_testing2 127.0.0.2 testing2 1 127.0.0.3_testing 127.0.0.3testing1
SELECT COUNT(DISTINCT ip_address) FROM bandits
正如在这里提到的:使用一个更长的版本可能会更快,而不是使用一个更长的版本: https : //stackoverflow.com/questions/11250253/postgresql-countdistinct-very-slow ,
SELECT count(*) FROM (SELECT DISTINCT ip_address FROM bandits) AS bandits_distinct