複数条件のcount()

SQLにおいて表題を行いたい。

■ 環境

  • AWS Athena

■ count()

where句で絞ると複数queryを実行しなければならない。Athenaを使用している為、読み込むデータ量は少なくさせたい。というわけで1回のクエリで複数の条件の”count()“を取得したい。

下記の方法にて一度のqueryで取得することができた。”column1“カラムの数値で”count()“したい。

SELECT
  regist_date,
  count(1) as total,
  count(column1 < 10 or null) as count1,
  count(column1 >= 10 or null) as count2
FROM testtable
GROUP BY regist_date

ポイントは”or null“である。

下記のような結果が得られた。満足である。

+--------------+-------+--------+--------+
| regist_date  | total | count1 | count2 |
+--------------+-------+--------+--------+
| 2018-02-01   |    51 |     31 |     20 |
| 2018-02-02   |     8 |      6 |      2 |
  :
+--------------+-------+--------+--------+

以上。

■ 関連

count(distinct (case when … then … end))

複数のcount()の結果を四則演算