0
0

集中演習SQL入門 BigQueryで始めるビジネスデータ分析からの学び

Last updated at Posted at 2024-07-06

年-月-日のデータを取る方法

2021-01-20といった形で日付型のデータを取りたいときは、%Fを使う

format_datetime("%F",date_time)

参考
https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements#format_elements_date_time

文字列や小数が入っているデータを整数へ変換

SQL出方変更をするときには、CAST()関数を使う。小数から整数へ変換

CAST(your_field AS INT64) AS your_field,

文字列や小数が入っているデータを整数へ変換

SQL出方変更をするときには、CAST()関数を使う。小数から整数へ変換

CAST(your_field AS INT64) AS your_field,

SQLでWith句を2つ以上書く

,を使うことでwith句を2つ以上作れる

with cart as(
select concat(cid,"-",session_count) as session , count(page)
from sample.web_log
where page="/cart/"
group by session
),
thank as(
select concat(cid,"-",session_count) as session , count(page)
from sample.web_log
where page="/thank_you/"
group by session
)

SQLで欠損値処理をする

SQLでnullの値へ何かを入れるときには、ifnull()関数を使う

select ifnull(registration_year,2017) as reg_year
from sample.small_ltv

関係ない2つの値を横並びにするときは、cross joinを使う

同一の関数から出せず、共通のキーもない場合にはcross joinを使って結合をする

with master as(
  select sum(revenue) / sum(quantity) as avg_unit_price_1
  from sample.sales
  where product_id =1),
master2 as(
  select sum(revenue) / sum(quantity) as avg_unit_price_all
  from sample.sales)
select *
from master cross join master2 
0
0
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
0