mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Count The Number Of Trues In An Aggregate Query as a postgres til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_853 TILs and counting..._
|
_854 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -403,6 +403,7 @@ _853 TILs and counting..._
|
|||||||
- [Constructing A Range Of Dates](postgres/constructing-a-range-of-dates.md)
|
- [Constructing A Range Of Dates](postgres/constructing-a-range-of-dates.md)
|
||||||
- [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md)
|
- [Convert A String To A Timestamp](postgres/convert-a-string-to-a-timestamp.md)
|
||||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||||
|
- [Count The Number Of Trues In An Aggregate Query](postgres/count-the-number-of-trues-in-an-aggregate-query.md)
|
||||||
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
|
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
|
||||||
- [Create An Index Without Locking The Table](postgres/create-an-index-without-locking-the-table.md)
|
- [Create An Index Without Locking The Table](postgres/create-an-index-without-locking-the-table.md)
|
||||||
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
|
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)
|
||||||
|
|||||||
26
postgres/count-the-number-of-trues-in-an-aggregate-query.md
Normal file
26
postgres/count-the-number-of-trues-in-an-aggregate-query.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# Count The Number Of Trues In An Aggregate Query
|
||||||
|
|
||||||
|
The `sum` function is an aggregate function that allows you to sum up a bunch
|
||||||
|
of integers. What if you want to sum up a boolean column? You may want to know
|
||||||
|
how many times `true` appears in a collection of grouped records.
|
||||||
|
|
||||||
|
This can be done by mixing in a `case` statement.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
select
|
||||||
|
author_id,
|
||||||
|
sum(case when available then 1 else 0 end)
|
||||||
|
from books
|
||||||
|
group by author_id;
|
||||||
|
```
|
||||||
|
|
||||||
|
Here, we are able to find out for each author how many books they have
|
||||||
|
available.
|
||||||
|
|
||||||
|
If we want to count `false` values, we can just invert the `sum` statement:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
sum(case when available then 0 else 1 end)
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](https://stackoverflow.com/a/5396728/535590)
|
||||||
Reference in New Issue
Block a user