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

后端 未结 1 470
面向向阳花
面向向阳花 2021-01-18 05:31

Documentation for EVERY aggregate:

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

http://www.postgresql.org/docs/9.1

1条回答
  •  没有蜡笔的小新
    2021-01-18 05:56

    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 
    
    [...]
    
     ::=
      COUNT    [  ]
      |  [  ]
      |  [  ]
      |  [  ]
    
     ::=
        [  ]
       
    
     ::=
      
    
     ::=
      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.

    0 讨论(0)
提交回复
热议问题