Ad
Count Distinct By Boolean Value
Is there a better (prettier, more idiomatic, or even performant) way to do the following?
Objective: calculate distinct values for a column by another boolean column.
Sample data:
id | metadata_streaming_date | cols_exist |
--- | ----------------------- | -----------|
1 | 2022-02-20 | true |
1 | 2022-02-20 | true |
2 | 2022-02-20 | true |
2 | 2022-02-20 | true |
3 | 2022-02-20 | false |
1 | 2022-02-19 | true |
2 | 2022-02-19 | false |
3 | 2022-02-19 | false |
4 | 2022-02-19 | false |
4 | 2022-02-19 | false |
Expected result is to count distinct id
grouped by metadata_streaming_date
split by wanted (where cols_exist = false
) and overall (all rows for this id per date).
Result table expected:
| metadata_streaming_date | wanted | overall |
| ----------------------- | -------| --------|
| 2022-02-20 | 1 | 3 |
| 2022-02-19 | 3 | 4 |
I can achieve it through two sub-queries and inner-joining then by metadata_streaming_date
:
select
t1.metadata_streaming_date,
overall,
wanted,
wanted / overall as perc
from
(
select
metadata_streaming_date,
count(distinct id) as overall
from
non_needed_fields_view
where
metadata_streaming_date >= '2022-02-19'
group by
metadata_streaming_date
) as t1
inner join (
select
metadata_streaming_date,
count(distinct id) as wanted
from
non_needed_fields_view
where
cols_exist is false
and metadata_streaming_date >= '2022-02-19'
group by
metadata_streaming_date
) as t2 on t1.metadata_streaming_date = t2.metadata_streaming_date
Ad
Answer
- There is a cool FILTER syntax for aggregate functions, currently supported by some RDBMS / SQL engines including Spark SQL, PostgreSQL & SQLite. As far as I remember it is part of the SQL ISO standard.
- The ISO syntax for date in SQL is
DATE 'yyyy-MM-dd'
select metadata_streaming_date
,count(distinct id) filter (where cols_exist = false) as wanted
,count(distinct id) as overall
from non_needed_fields_view
where metadata_streaming_date >= date '2022-02-19'
group by metadata_streaming_date
+-----------------------+------+-------+
|metadata_streaming_date|wanted|overall|
+-----------------------+------+-------+
| 2022-02-19| 3| 4|
| 2022-02-20| 1| 3|
+-----------------------+------+-------+
Ad
source: stackoverflow.com
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key
Ad