0
0

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 1 year has passed since last update.

SQL/ BigQuery memo for myself

Posted at

最大値最小値を確認
SELECT
MIN(compression_ratio) AS min_compression_ratio,
MAX(compression_ratio) AS max_compression_ratio
FROM
cars.car_info;
解説
最小値をmin_compression_ratioという名前を付けて算出
最大値も同様
cars.car_infoから出す

値を書き換える方法,ANDで条件を追加することもできる
UPDATE
cars.car_info
SET
num_of_cylinders = "two"
WHERE
num_of_cylinders = "tow";

UPDATE
cars.car_info
SET
num_of_doors = "four"
WHERE
make = "dodge"
AND fuel_type = "gas"
AND body_style = "sedan";

CAST関数
SELECT CAST(purchase_price AS FLOAT64)
FROM ...
ORDER BY CAST(purchase_price AS FLOAT64) DESC
→Float に変換される

CONCAT関数
SELECT
CONCAT(product_code, product_color) AS new_product_code
FROM
customer_data.customer_purchase
WHERE
product = 'couch';
couchのどの色が人気化をぱっと見でわかる
new product codeとして出力される

NULLを値へ変換するCOALESCE関数
SELECT
COALESCE(product, product_code) AS product_info
FROM
customer_data.customer_purchase
→productのnullだったところがproduct_codeが入って出力される
第二引数に特定の値を'xxx'のように追加もできる。

SELECT
customer_id,
SUBSTR(state,1,2) AS new_state
FROM
customer
ORDER BY
state DESC
→stateの最初の二文字が出る

ORDER BY →アルファベット順
ORDER BY DESC → 逆アルファベット順

COUNT or COUNT DISTINCT
SELECT句で使う。例:COUNT=売れた商品の数 CD=売れた商品の種類の数

エイリアス(alias)でテーブルのど長い名前を省略することもできる。
ASはなしでもいい。
TOBINAGA.GAKUTO_birthdate AS birthdate
INNER JOIN TOBINAGA.GAKUTO_birthplace birthplace
ON birthdate.birth_id = birthplace.bitrh_id

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?