Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any equivalent to Postgresql EVERY aggregate function on other RDBMS?

Documentation for EVERY aggregate:

every(expression) : true if all input values are true, otherwise false

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


EVERY is semantically equivalent to 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

Output:

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

Data source:

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');
like image 790
Hao Avatar asked May 02 '12 03:05

Hao


People also ask

What is aggregate function in Rdbms?

In database management, an aggregate function or aggregation function is a function where the values of multiple rows are grouped together to form a single summary value. Common aggregate functions include: Average (i.e., arithmetic mean) Count. Maximum.

What are the aggregate functions in PostgreSQL?

Like most other relational database products, PostgreSQL supports aggregate functions. An aggregate function computes a single result from multiple input rows. For example, there are aggregates to compute the count , sum , avg (average), max (maximum) and min (minimum) over a set of rows.

Is it possible to apply an aggregate function for each group in PostgreSQL?

For each group, you can apply an aggregate function e.g., SUM() to calculate the sum of items or COUNT() to get the number of items in the groups. In this syntax: First, select the columns that you want to group e.g., column1 and column2 , and column that you want to apply an aggregate function ( column3 ).

Can PostgreSQL users create their own aggregate functions?

Fortunately, PostgreSQL is really flexible and allows end users to create your own aggregation functions, which can help to move your business logic to PostgreSQL. The “CREATE AGGREGATE” command is there to create all kinds of aggregations.


1 Answers

Emulating EVERY() with CASE and SUM()

In fact, this article describes how EVERY() can be emulated via CASE and SUM(). The following two statements are equivalent:

SELECT EVERY(id < 10)
FROM book

SELECT CASE SUM(CASE WHEN id < 10 THEN 0 ELSE 1 END) 
         WHEN 0 THEN 1 
         ELSE 0 
       END
FROM book;

The same is true for the EVERY() window function:

SELECT 
  book.*, 
  EVERY(title LIKE '%a') OVER (PARTITION BY author_id)
FROM book

SELECT
  book.*,
  CASE SUM(CASE WHEN title LIKE '%a' THEN 0 ELSE 1 END)
       OVER(PARTITION BY author_id)
    WHEN 0 THEN 1 
    ELSE 0
  END
FROM book;

SQL Standard

The SQL:2008 standard mentions the EVERY aggregate function:

10.9 <aggregate function>

[...]

<aggregate function> ::=
  COUNT <left paren> <asterisk> <right paren> [ <filter clause> ]
  | <general set function> [ <filter clause> ]
  | <binary set function> [ <filter clause> ]
  | <ordered set function> [ <filter clause> ]

<general set function> ::=
  <set function type> <left paren> [ <set quantifier> ]
  <value expression> <right paren>

<set function type> ::=
  <computational operation>

<computational operation> ::=
  AVG
  | MAX
  | MIN
  | SUM
  | EVERY
  | [...]

But "advanced" SQL standard features are not often implemented by databases. Oracle 11g for instance, doesn't support it, neither does SQL Server 2012.

With HSQLDB, however, you may be more lucky. HSQLDB 2.x is very standards-compliant, also MySQL knows the BIT_AND() aggregate function, which is a non-standard alias to EVERY(), also supported by Postgres.

Note, some databases allow for writing user-defined aggregate functions, so you may as well implement EVERY() yourself.

like image 134
Lukas Eder Avatar answered Sep 28 '22 14:09

Lukas Eder