7

すべての集計のドキュメント:

every(expression) : すべての入力値が true の場合は true、そうでない場合は false

http://www.postgresql.org/docs/9.1/static/functions-aggregate.html


EVERY は意味的に COUNT(conditionIsTrue) = COUNT(*) と同等です

select person_id, 

    every(visited_site = 'http://stackoverflow.com') as visited_same_site_forever,

    count(case when visited_site = 'http://stackoverflow.com' then '^_^' end) 
    = count(*) as visited_same_site_forever2

from z
group by person_id
order by person_id

出力:

 person_id | visited_same_site_forever | visited_same_site_forever2 
-----------+---------------------------+----------------------------
        88 | f                         | f
     55327 | t                         | t
    256196 | f                         | f

情報源:

create table z(person_id int, visited_site varchar(100), datetime_visited timestamp);

insert into z values
(55327,'http://stackoverflow.com','Jan 1, 2010'),
(55327,'http://stackoverflow.com','Feb 14, 2012'),
(55327,'http://stackoverflow.com','May 1, 2012'),
(256196,'http://stackoverflow.com','February 1, 2012'),
(256196,'http://stackoverflow.com','February 2, 2012'),
(256196,'http://slashdot.org','May 2, 2012'),
(88,'http://theregister.co.uk','April 1, 2012'),
(88,'http://slashdot.org','April 2, 2012');
4

1 に答える 1