Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return true if all column values are true

Is there a faster way in PostgreSQL to essentially do an if on several rows?

Say I have a table

ticket | row | archived
1      | 1   | true
1      | 2   | true
1      | 3   | true
2      | 1   | false
2      | 2   | true

Is there any way I could do an if statement across down the column where ticket = ? So that where ticket = 1 would be true because

true && true && true = true

and where ticket = 2 would be false because

false && true = false

Or should I just stick with

SELECT ( (SELECT COUNT(*) FROM table WHERE ticket = 1)
       = (SELECT COUNT(*) FROM table WHERE ticket = 1 AND archived = true) )
like image 942
mouckatron Avatar asked Oct 22 '12 15:10

mouckatron


People also ask

Why do you use the ANY and ALL operators?

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.

When to use ALL and ANY in SQL?

ANY means that the condition will be satisfied if the operation is true for any of the values in the range. ALL means that the condition will be satisfied only if the operation is true for all values in the range. mentioning that SOME and ANY are synonyms.

What is the use of ALL in SQL?

ALL is used to select all records of a SELECT STATEMENT. It compares a value to every value in a list or results from a query. The ALL must be preceded by the comparison operators and evaluates to TRUE if the query returns no rows. For example, ALL means greater than every value, means greater than the maximum value.

How to get true from a subquery in SQL?

If any value is false, the subquery will return at least one row. EXISTS will report true, which is flipped to false. The only way to get true out of it is for the subquery to return no rows, which only happens when all are true Select case when Count (BooleanField)=Sum (BooleanField) then 1 Else 0 End as Result From MyTable

How do I get a subquery to return only one row?

SELECT NOT EXISTS (SELECT null FROM t WHERE NOT boolcolumn); If any value is false, the subquery will return at least one row. EXISTS will report true, which is flipped to false. The only way to get true out of it is for the subquery to return no rows, which only happens when all are true

How to check if column-wise values are all true in pandas Dataframe?

So, call all () method on DataFrame with default values for parameters to check if column-wise values are all True. import pandas as pd df = pd.DataFrame ( {'col_1': [True, True, True], 'col_2': [True, True, False]}) result = df.all () print (result)


1 Answers

Aggregate function bool_and()

Simple, short, clear:

SELECT bool_and(archived)
FROM   tbl
WHERE  ticket = 1;

The manual:

true if all input values are true, otherwise false

Subquery expression EXISTS

Like @Mike provided.

Faster. But you have to additionally check whether any rows with ticket = 1 exist at all, or you'll get incorrect results for non-existing tickets:

SELECT EXISTS (SELECT 1 FROM tbl WHERE ticket=1)
       AND NOT
       EXISTS (SELECT 1 FROM tbl WHERE ticket=1 AND archived = FALSE);

Indices

Both forms can and will use an index like:

CREATE index tbl_ticket_idx ON tbl (ticket);

.. which makes both fast, but the EXISTS query faster, because this form can stop to scan as soon as the first matching row is found. There is hardly any difference between the two queries with only few rows per ticket, but a substantial difference for many rows per ticket.

To make use of index-only scans in pg 9.2 you would need a multi-column index of the form:

CREATE index tbl_ticket_archived_idx ON tbl (ticket, archived);

This one is better in any case most cases and any version of PostgreSQL. Due to data alignment, adding a boolean to the integer in the index will not make the index grow at all. Added benefit for hardly any cost.

However, indexed columns prevent HOT (Heap Only Tuple) updates. Say, an UPDATE changes only the column archived. If the column isn't used by any index (in any way), the row can be HOT updated. Else, this shortcut cannot be taken. More on HOT updates:

  • Redundant data in update statements

It all depends on your actual workload.

like image 97
Erwin Brandstetter Avatar answered Sep 28 '22 11:09

Erwin Brandstetter