0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bigquery 横に長いテーブルの中身を条件句で集計する

Posted at

やりたいこと

BigQueryで集計するなかで
横に長く伸びているテーブルの中から一定条件を満たしたものだけをを集計したかったので調べました。
if(....) で頑張ることもできるのですが、項目が多くなると大変なので配列集計を使ってスマートに記述するやり方です

id user_id name description photo skill
1 101 public public closed deleted
2 102 public public public public
3 103 public public closed public
4 104 public closed closed public

↓↓↓↓↓↓↓

user_id public_cnt
101 3
102 4
103 3
104 2

クエリサンプル

SELECT
  user_id, 
  (SELECT COUNTIF(_x = 'public') FROM UNNEST([name, description, photo, skill]) as _x) AS public_cnt
FROM item_display_statuses
;

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?