年-月-日のデータを取る方法
2021-01-20といった形で日付型のデータを取りたいときは、%Fを使う
format_datetime("%F",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