[BigQuery] λ°μ΄ν° νμ λ³ ν¨μ μ 리 (λ¬Έμμ΄κ³Ό μκ°/λ μ§)
π― λ°μ΄ν°λ₯Ό κ·Έλλ‘ κ°μ Έμ€μ§ μκ³ λ³νμ΄ νμν κ²½μ°μ μΈ μ μλ ν¨μλ₯Ό μμ보μ
- SELECT λ¬Έμμ λ°μ΄ν°λ₯Ό λ³νμν¬ μ μμΌλ©°, WHEREμ 쑰건문μμλ μ¬μ© κ°λ₯
- λ°μ΄ν°μ νμ μ λ°λΌ λ€μν ν¨μκ° μ‘΄μ¬
1. μλ£ νμ μ λ³ννλ ν¨μ : CAST
SELECT
CAST(1 AS STRING) # μ«μ 1μ λ¬Έμ 1λ‘ λ³κ²½
π λ μμ νκ² λ°μ΄ν° νμ λ³κ²½νκΈ° : SAFE_CAST()
→ λ³νμ΄ μ€ν¨ν κ²½μ° NULLμ λ°ννλ©°, λ€μκ³Ό κ°μ κ²½μ°μ μλ¬ λμ NULLμ΄ λ¨
SELECT
SAFE_CAST("λ¬Έμμ΄" AS INT64)
(μ μ¬ μ¬λ‘) μμ νκ² λλκΈ° μ°μ°νκΈ° : SAFE_DIVIDE()
SAFE_DIVIDE(x, y) # zero error λμ null λ°μ
2. λ¬Έμμ΄μ λ€λ£¨λ ν¨μ
(1) CONCAT : λ¬Έμμ΄ λΆμ΄κΈ°
→ CONCAT(컬λΌ1, 컬λΌ2, ...)
SELECT
CONCAT('μλ
', 'νμΈμ', '!!') AS result;
(2) SPLIT : λ¬Έμμ΄ λΆλ¦¬νκΈ°
→ SPLIT(λ¬Έμμ΄ μλ³Έ, λλ κΈ°μ€μ΄ λλ λ¬Έμ)
SELECT
SPLIT('κ°, λ, λ€, λΌ', ", ") AS result;
(3) REPLACE : νΉμ λ¨μ΄ μμ νκΈ°
→ REPLACE(λ¬Έμμ΄ μλ³Έ, μ°Ύμ λ¨μ΄, λ°κΏ λ¨μ΄)
SELECT
REPLACE('μλ
νμΈμ', 'μλ
', 'μ€μ²') AS result;
(3) TRIM : λ¬Έμμ΄ μλ₯΄κΈ°
→ TRIM(λ¬Έμμ΄ μλ³Έ, μλ₯Ό λ¨μ΄)
SELECT
TRIM("μλ
νμΈμ", "νμΈμ") AS trim_example
(4) UPPER : μμ΄ λλ¬Έμ λ³ννκΈ°
→ UPPER(λ¬Έμμ΄ μλ³Έ)
SELECT
UPPER("ab") AS upper_example
3. λ μ§ λ° μκ° λ°μ΄ν° (κΈ°λ³Έ)
π μκ° κ΄λ ¨ λ°μ΄ν° νμ μ μ’ λ₯
- DATE : DATEλ§ νμνλ λ°μ΄ν° → μ: 2024-03-01
- DATETIME : DATEμ TIMEκΉμ§ νμνλ λ°μ΄ν°(Timezone μ 보 μμ) → μ: 2024-03-03 14:00:00
- TIME : λ μ§μ 무κ΄νκ² μκ°λ§ νμνλ λ°μ΄ν° → μ: 23:59:59.00
π νμμ‘΄ κ°λ μ§κ³ κ°κΈ°
- UTC(Universal Time Coordinated : νκ΅μκ° = UTC + 9)
- κ΅μ μ μΈ νμ€ μκ°μΌλ‘, νμ μΈκ³μ
- νμμ‘΄μ΄ μ‘΄μ¬ν¨ → νΉμ μ§μμ νμ€ μκ°λ
- TIMESTAMP
- UTCλΆν° κ²½κ³Όν μκ°μ λνλ΄λ κ°
- νμμ‘΄ μ 보 μμ
- νμ μ: 2024-03-03 14:00:00 UTC
π millisecond & microsecond μκ³ κ°κΈ°
- millisecond(ms) : μ² λΆμ 1μ΄(1,000 ms = 1 sec)
- λΉ λ₯Έ λ°μμ΄ νμν λΆμΌμμ μ¬μ©(μ΄λ³΄λ€ λ μ ννκ² β°)
- Millisecond → TIMESTAMP → DATETIMEμΌλ‘ λ³κ²½ν΄μ μ¬μ©νλ κ²½μ° λ§μ
- microsecond(μs) : 1 / 1,000 ms
- μλ μμμ κ°μ΄ μκ° νμ μ μ ννλ©°, DATETIMEμΌλ‘ μ ν μ 'νμμ‘΄' λͺ μν΄μΌ ν¨!
- λ§μ νμ¬λ€μ Tableμ μκ°μ΄ Timestampλ‘ μ μ₯λ κ²½μ°κ° λ§μ(λλ Datetime) : λ°λΌμ λ³νμ΄ νμν κ²½μ° λ§μ
SELECT
TIMESTAMP_MILLIS(1704176819711) AS milli_to_timestamp_value,
TIMESTAMP_MICROS(1704176819711000) AS micro_to_timestamp_value,
DATETIME(TIMESTAMP_MICROS(1704176819711000), 'Asia/Seoul') AS datetime_value
π μ 리 : TIMESTAMP vs. DATETIME
TIMESTAMP | DATETIME | |
νμμ‘΄ | UTCλΌκ³ λμ΄ | Tκ° λμ΄(Time μλ―Έ) |
μκ° μ°¨μ΄ | νκ΅ μκ° -9μκ° | νκ΅ Zone μ¬μ©μ νκ΅ μκ°κ³Ό λμΌ |
3. Datetime κ°κ³΅ ν¨μ
(1) CURRENT_DATETIME : νμ¬ Datetime μΆλ ₯
→ CURRENT_DATETIME([time_zone])
CURRENT_DATE('Asia/Seoul')
CURRENT_DATETIME('Asia/Seoul')
(2) EXTRACT : Datetimeμμ νΉμ λΆλΆλ§ μΆμΆ
→ EXTRACT(part FROM [col])
EXTRACT(DATE FROM [datetime_col])
EXTRACT(YEAR FROM [datetime_col])
EXTRACT(MONTH FROM [datetime_col])
EXTRACT(DAY FROM [datetime_col])
EXTRACT(HOUR FROM [datetime_col])
EXTRACT(MINUTE FROM [datetime_col])
π μμΌ μΆμΆ : EXTRACT(DAYOFWEEK FROM [datetime_col])
→ ν μ£Όμ 첫 λ μΈ μΌμμΌλΆν° 1λ‘ λ°νλ¨(ν μμΌμ΄ λ§μ§λ§ 7)
(3) DATETIME_TRUNC : Datetime μκ° μλ₯΄κΈ°
→ DATETIME_TRUNC([datetime_col], [HOUR:μλ₯Ό λΆλΆ λͺ μ])
DATETIME_TRUNC([datetime_col], DAY)
DATETIME_TRUNC([datetime_col], YEAR)
DATETIME_TRUNC([datetime_col], MONTH)
DATETIME_TRUNC([datetime_col], HOUR)
(4) PARSE_DATETIME : λ¬Έμμ΄λ‘ μ μ₯λ Datetimeμ Datetime νμ μΌλ‘ λ°κΎΈκΈ°
→ PARSE_DATETIME('λ¬Έμμ΄μ νν', 'DATETIME λ¬Έμμ΄')
→ Format Elements λ¬Έμλ₯Ό μ°Έκ³ νμ¬ λ¬Έμμ΄ ννμ μλ―Έλ₯Ό 체ν¬(νμν λ νμΈ) : https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements#format_elements_date_time
PARSE_DATETIME('%Y-%m-%d %H:%M:%S', '2024-01-11 12:35:35') AS parse_datetime;
(5) FORMAT_DATETIME : Datetime νμ λ°μ΄ν°λ₯Ό νΉμ ννμ λ¬Έμμ΄ λ°μ΄ν°λ‘ λ³ννκΈ°
→ FORMAT_DATETIME('λ¬Έμμ΄μ νν', 'DATETIME λ¬Έμμ΄')
SELECT
FORMAT_DATETIME("%c", DATETIME "2024-01-11 12:35:35") AS formatted;
(6) LAST_DAY : μλμΌλ‘ μμ λ§μ§λ§ κ°μ κ³μ°ν΄μ νΉμ μ°μ°μ ν κ²½μ°
→ LAST_DAY([DATETIME], λ§μ§λ§λ κΈ°μ€)
→ μλ μμλ₯Ό λ³΄κ³ μ°¨μ΄λ₯Ό νμΈ
SELECT
LAST_DAY(DATETIME '2024-01-03 15:30:00') AS last_day,
LAST_DAY(DATETIME '2024-01-03 15:30:00', MONTH) AS last_day_month,
LAST_DAY(DATETIME '2024-01-03 15:30:00', WEEK) AS last_day_week,
LAST_DAY(DATETIME '2024-01-03 15:30:00', WEEK(SUNDAY)) AS last_day_week_sun,
LAST_DAY(DATETIME '2024-01-03 15:30:00', WEEK(MONDAY)) AS last_day_week_mon
(7) DATETIME_DIFF : μλμΌλ‘ μμ λ§μ§λ§ κ°μ κ³μ°ν΄μ νΉμ μ°μ°μ ν κ²½μ°
→ DATETIME_DIFF([첫 DATETIME], [λ€μ DATETIME], μ°¨μ΄ νμ° κΈ°μ€)
→ μλ μμλ₯Ό λ³΄κ³ μ°¨μ΄λ₯Ό νμΈ
SELECT
DATETIME_DIFF(first_datetime, second_datetime, DAY) AS day_diff1,
DATETIME_DIFF(second_datetime, first_datetime, DAY) AS day_diff2,
DATETIME_DIFF(first_datetime, second_datetime, MONTH) AS month_diff,
DATETIME_DIFF(first_datetime, second_datetime, WEEK) AS week_diff,
FROM ( SELECT
DATETIME "2024-04-02 10:20:00" AS first_datetime,
DATETIME "2021-01-01 15:30:00" AS second_datetime, )