SELECT ARRAY<STRUCT<
          id INT64,
          name STRING,
          promo_id STRING,
          quantity FLOAT64,
          price FLOAT64
        >>[(
          SAFE_CAST(NULL AS INT64),
          SAFE_CAST(NULL AS STRING),
          SAFE_CAST(NULL AS STRING),
          SAFE_CAST(NULL AS FLOAT64),
          SAFE_CAST(NULL AS FLOAT64)
        )]
--------------------
SELECT ARRAY<STRUCT<id INT64, name STRING, promo_id STRING, quantity FLOAT64, price FLOAT64>>[(SAFE_CAST(NULL AS INT64), SAFE_CAST(NULL AS STRING), SAFE_CAST(NULL AS STRING), SAFE_CAST(NULL AS FLOAT64), SAFE_CAST(NULL AS FLOAT64))]
------------------------------------------------------------------------------------------------------------------------
SELECT CAST(null as STRUCT<ARRAY<STRUCT<INT64, STRING>>>)
--------------------
SELECT CAST(NULL AS STRUCT<ARRAY<STRUCT<INT64, STRING>>>)
------------------------------------------------------------------------------------------------------------------------
SELECT `a-b-c-d-e.udfs.st`(1);
--------------------
SELECT `a-b-c-d-e`.udfs.st(1);
------------------------------------------------------------------------------------------------------------------------
  SELECT
    campaign.key AS campaign_key,
    campaign.start_date AS campaign_start_date,
    campaign.end_date AS campaign_end_date,
  FROM
    UNNEST([
      STRUCT(
        'trial_campaign' AS key,
        '2024-11-17T17:00:00+00:00' AS start_date,
        '2024-12-01T16:59:59+00:00' AS end_date,
        ARRAY<STRUCT<key STRING, start_date STRING, end_date STRING>>[
          STRUCT('mission_1', '2024-11-17T17:00:00+00:00', '2024-11-24T16:59:59+00:00'),
          STRUCT('mission_2', '2024-11-24T17:00:00+00:00', '2024-12-01T16:59:59+00:00')
        ] AS missions
      )
    ]) AS campaign
--------------------
SELECT campaign.key AS campaign_key, campaign.start_date AS campaign_start_date, campaign.end_date AS campaign_end_date
FROM UNNEST([STRUCT('trial_campaign' AS key, '2024-11-17T17:00:00+00:00' AS start_date, '2024-12-01T16:59:59+00:00' AS end_date, ARRAY<STRUCT<key STRING, start_date STRING, end_date STRING>>[STRUCT('mission_1', '2024-11-17T17:00:00+00:00', '2024-11-24T16:59:59+00:00'),
	STRUCT('mission_2', '2024-11-24T17:00:00+00:00', '2024-12-01T16:59:59+00:00')] AS missions)]) AS campaign
------------------------------------------------------------------------------------------------------------------------
CREATE TEMP FUNCTION invalidRoute(polyline1 STRING)
  RETURNS BOOL
  LANGUAGE js AS
  "return (lengthOfEncodedPolyline(polyline1) <= 1)"
  OPTIONS (library="gs://bi_cloud_storage_ees6see6/public/lib/polyutil_new.js");
--------------------
CREATE TEMPORARY FUNCTION invalidRoute (
	polyline1 STRING
)
RETURNS BOOL
LANGUAGE js
OPTIONS (library = 'gs://bi_cloud_storage_ees6see6/public/lib/polyutil_new.js')
AS """return (lengthOfEncodedPolyline(polyline1) <= 1)""";
------------------------------------------------------------------------------------------------------------------------
create temporary function poly(polygon string)
returns string language js as """
var res = JSON.parse(polygon);
var l = res.length;
var i;
for (i=0; i<l; i++) {
  temp = res[i][0]
  res[i][0] = res[i][1]
  res[i][1] = temp
};
var last = [res[0][0],res[0][1]];
res.push(last);
var geojson = {"type": "Polygon", "coordinates" : [res]}
return JSON.stringify(geojson);
"""
OPTIONS();
--------------------
CREATE TEMPORARY FUNCTION poly (
	polygon string
)
RETURNS string
LANGUAGE js
AS """
var res = JSON.parse(polygon);
var l = res.length;
var i;
for (i=0; i<l; i++) {
  temp = res[i][0]
  res[i][0] = res[i][1]
  res[i][1] = temp
};
var last = [res[0][0],res[0][1]];
res.push(last);
var geojson = {"type": "Polygon", "coordinates" : [res]}
return JSON.stringify(geojson);
""";
------------------------------------------------------------------------------------------------------------------------
CREATE TEMP FUNCTION myFunc(a FLOAT64, b STRING)
RETURNS STRING
LANGUAGE js
AS r"""
  // Assumes 'doInterestingStuff' is defined in one of the library files.
  return doInterestingStuff(a, b);
"""
OPTIONS (
    library=['gs://my-bucket/path/to/lib1.js', 'gs://my-bucket/path/to/lib2.js']);
--------------------
CREATE TEMPORARY FUNCTION myFunc (
	a FLOAT64,
	b STRING
)
RETURNS STRING
LANGUAGE js
OPTIONS (library = ['gs://my-bucket/path/to/lib1.js', 'gs://my-bucket/path/to/lib2.js'])
AS """
  // Assumes 'doInterestingStuff' is defined in one of the library files.
  return doInterestingStuff(a, b);
""";
------------------------------------------------------------------------------------------------------------------------
SELECT DATE(verify_timestamp, 'Asia/Jakarta') AS jakarta_data_date, app_version, device_platform as device_platform_name, 
BU as business_unit_name, verification_method as verification_type,
  CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(50)] AS NUMERIC) AS p50_granted_time_in_second,
  CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(75)] AS NUMERIC) AS p75_granted_time_in_second,
  CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(90)] AS NUMERIC) AS p90_granted_time_in_second,
  CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(95)] AS NUMERIC) AS p95_granted_time_in_second,
  CAST(AVG(time_diff) AS NUMERIC) AS average_login_time_in_second,
  CURRENT_TIMESTAMP() AS load_timestamp,
  CAST('2024-10-03T00:00:00Z' AS TIMESTAMP) AS last_modified_timestamp
FROM 
  data
--------------------
SELECT DATE(verify_timestamp, 'Asia/Jakarta') AS jakarta_data_date, app_version, device_platform AS device_platform_name
	, BU AS business_unit_name, verification_method AS verification_type, CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(50)] AS NUMERIC) AS p50_granted_time_in_second, CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(75)] AS NUMERIC) AS p75_granted_time_in_second, CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(90)] AS NUMERIC) AS p90_granted_time_in_second
	, CAST(APPROX_QUANTILES(time_diff, 100 IGNORE NULLS)[OFFSET(95)] AS NUMERIC) AS p95_granted_time_in_second, CAST(AVG(time_diff) AS NUMERIC) AS average_login_time_in_second, CURRENT_TIMESTAMP() AS load_timestamp, CAST('2024-10-03T00:00:00Z' AS TIMESTAMP) AS last_modified_timestamp
FROM data
------------------------------------------------------------------------------------------------------------------------
SELECT SAFE_CAST(var_start_date AS STRING FORMAT 'DD')
--------------------
SELECT SAFE_CAST(var_start_date AS STRING FORMAT 'DD')
------------------------------------------------------------------------------------------------------------------------
select 1,2,
union all
select 2, 3
--------------------
SELECT 1, 2
UNION ALL
SELECT 2, 3
------------------------------------------------------------------------------------------------------------------------
IF source_start_date <= latest_old_table_partition
  THEN
  SET previous_partition = (
          SELECT MAX(_partitiontime)
              from `p-gofin-id-data-raw.data_science_us.dm_customer_cs_cscore`
          WHERE
              _partitiontime < TIMESTAMP(source_start_date)
      );

  SET latest_partition = (
        SELECT MAX(_partitiontime) as partitiontime
            from `p-gofin-id-data-raw.data_science_us.dm_customer_cs_cscore`
      );
ELSEIF source_start_date = first_new_table_partition
  THEN
  SET previous_partition = TIMESTAMP(latest_old_table_partition);
  SET latest_partition = TIMESTAMP(first_new_table_partition);
ELSE
  SET previous_partition = (
        SELECT MAX(_partitiontime) as partitiontime
            from `p-gofin-id-data-raw.data_science_us.m_cust_pl_c_scores`
        WHERE
            _partitiontime < TIMESTAMP(source_start_date)
      );

  SET latest_partition = (
        SELECT MAX(_partitiontime) 
            from `p-gofin-id-data-raw.data_science_us.m_cust_pl_c_scores`
      );
END IF;
--------------------
IF source_start_date <= latest_old_table_partition
	THEN
	SET previous_partition =(
		SELECT MAX(_partitiontime)
		FROM `p-gofin-id-data-raw`.data_science_us.dm_customer_cs_cscore
		WHERE _partitiontime < TIMESTAMP(source_start_date)
	);
	SET latest_partition =(
		SELECT MAX(_partitiontime) AS partitiontime
		FROM `p-gofin-id-data-raw`.data_science_us.dm_customer_cs_cscore
	);
ELSEIF source_start_date = first_new_table_partition
 THEN
	SET previous_partition = TIMESTAMP(latest_old_table_partition);
	SET latest_partition = TIMESTAMP(first_new_table_partition);
ELSE
	SET previous_partition =(
		SELECT MAX(_partitiontime) AS partitiontime
		FROM `p-gofin-id-data-raw`.data_science_us.m_cust_pl_c_scores
		WHERE _partitiontime < TIMESTAMP(source_start_date)
	);
	SET latest_partition =(
		SELECT MAX(_partitiontime)
		FROM `p-gofin-id-data-raw`.data_science_us.m_cust_pl_c_scores
	);
END IF;
------------------------------------------------------------------------------------------------------------------------
select a, b, c
from t1
group by all
--------------------
SELECT a, b, c
FROM t1
GROUP BY ALL
------------------------------------------------------------------------------------------------------------------------
BEGIN
	BEGIN TRANSACTION;
	COMMIT TRANSACTION;
EXCEPTION WHEN ERROR THEN
  RAISE USING message = FORMAT("Error %s at %s because of statement %s.", @@error.message, @@error.formatted_stack_trace, @@error.statement_text);
  ROLLBACK TRANSACTION;
END
--------------------
BEGIN
	START TRANSACTION;
	COMMIT TRANSACTION;
EXCEPTION
	WHEN ERROR THEN
		RAISE USING MESSAGE = FORMAT('Error %s at %s because of statement %s.', @@error.message, @@error.formatted_stack_trace, @@error.statement_text);
		ROLLBACK TRANSACTION;
END;
------------------------------------------------------------------------------------------------------------------------
select PARSE_DATE("%Y%m%d", date.date_id) as date, week.week_start_date
 from t1
--------------------
SELECT PARSE_DATE('%Y%m%d', date.date_id) AS date, week.week_start_date
FROM t1
------------------------------------------------------------------------------------------------------------------------
WITH Races AS (
 SELECT "800M" AS race,
   [STRUCT("Rudisha" AS name, [23.4, 26.3, 26.4, 26.1] AS laps),
    STRUCT("Makhloufi" AS name, [24.5, 25.4, 26.6, 26.1] AS laps),
    STRUCT("Murphy" AS name, [23.9, 26.0, 27.0, 26.0] AS laps),
    STRUCT("Bosse" AS name, [23.6, 26.2, 26.5, 27.1] AS laps),
    STRUCT("Rotich" AS name, [24.7, 25.6, 26.9, 26.4] AS laps),
    STRUCT("Lewandowski" AS name, [25.0, 25.7, 26.3, 27.2] AS laps),
    STRUCT("Kipketer" AS name, [23.2, 26.1, 27.3, 29.4] AS laps),
    STRUCT("Berian" AS name, [23.7, 26.1, 27.0, 29.3] AS laps),
    STRUCT("Nathan" AS name, ARRAY<FLOAT64>[] AS laps),
    STRUCT("David" AS name, NULL AS laps)]
    AS participants)
SELECT
  name, sum(duration) AS finish_time
FROM Races CROSS JOIN Races.participants LEFT JOIN participants.laps AS duration
GROUP BY name;
--------------------
WITH Races AS (
		SELECT '800M' AS race, [STRUCT('Rudisha' AS name, [23.4, 26.3, 26.4, 26.1] AS laps),
			STRUCT('Makhloufi' AS name, [24.5, 25.4, 26.6, 26.1] AS laps),
			STRUCT('Murphy' AS name, [23.9, 26.0, 27.0, 26.0] AS laps),
			STRUCT('Bosse' AS name, [23.6, 26.2, 26.5, 27.1] AS laps),
			STRUCT('Rotich' AS name, [24.7, 25.6, 26.9, 26.4] AS laps),
			STRUCT('Lewandowski' AS name, [25.0, 25.7, 26.3, 27.2] AS laps),
			STRUCT('Kipketer' AS name, [23.2, 26.1, 27.3, 29.4] AS laps),
			STRUCT('Berian' AS name, [23.7, 26.1, 27.0, 29.3] AS laps),
			STRUCT('Nathan' AS name, ARRAY<FLOAT64>[] AS laps),
			STRUCT('David' AS name, NULL AS laps)] AS participants
	)
SELECT name, sum(duration) AS finish_time
FROM Races
	CROSS JOIN Races.participants
	LEFT JOIN participants.laps duration
GROUP BY name;
------------------------------------------------------------------------------------------------------------------------
SELECT
  some_numbers,
  some_numbers[0] AS index_0,
  some_numbers[OFFSET(1)] AS offset_1,
  some_numbers[ORDINAL(1)] AS ordinal_1
FROM Sequences
--------------------
SELECT some_numbers, some_numbers[0] AS index_0, some_numbers[OFFSET(1)] AS offset_1, some_numbers[ORDINAL(1)] AS ordinal_1
FROM Sequences
------------------------------------------------------------------------------------------------------------------------
WITH
  Sequences AS (
    SELECT [0, 1, 1, 2, 3, 5] AS some_numbers UNION ALL
    SELECT [2, 4, 8, 16, 32] UNION ALL
    SELECT [5, 10]
  )
SELECT * FROM Sequences
--------------------
WITH Sequences AS (
		SELECT [0, 1, 1, 2, 3,
			5] AS some_numbers
		UNION ALL
		SELECT [2, 4, 8, 16, 32]
		UNION ALL
		SELECT [5, 10]
	)
SELECT *
FROM Sequences
------------------------------------------------------------------------------------------------------------------------
merge into t1 as tgt
using
(select * from base) as src
ON FALSE
WHEN NOT MATCHED BY SOURCE
THEN DELETE
when not matched
then insert
(business_id)
values(src.business_id)
--------------------
MERGE INTO t1 tgt
USING (
	SELECT *
	FROM base
) src ON false
WHEN NOT MATCHED BY SOURCE THEN DELETE
WHEN NOT MATCHED THEN INSERT (business_id)
VALUES (src.business_id)
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.Inventory T
USING dataset.NewArrivals S
ON T.product = S.product
WHEN MATCHED THEN
  UPDATE SET quantity = T.quantity + S.quantity
--------------------
MERGE INTO dataset.Inventory T
USING dataset.NewArrivals S ON T.product = S.product
WHEN MATCHED THEN UPDATE
SET quantity = T.quantity + S.quantity
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.Inventory T
USING (SELECT product, quantity, state FROM dataset.NewArrivals t1 JOIN dataset.Warehouse t2 ON t1.warehouse = t2.warehouse) S
ON T.product = S.product
WHEN MATCHED AND state = 'CA' THEN
  UPDATE SET quantity = T.quantity + S.quantity
WHEN MATCHED THEN
  DELETE
--------------------
MERGE INTO dataset.Inventory T
USING (
	SELECT product, quantity, state
	FROM dataset.NewArrivals t1
		JOIN dataset.Warehouse t2 ON t1.warehouse = t2.warehouse
) S ON T.product = S.product
WHEN MATCHED THEN UPDATE
SET quantity = T.quantity + S.quantity
WHERE state = 'CA'
WHEN MATCHED THEN DELETE
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.Inventory T
USING dataset.NewArrivals S
ON FALSE
WHEN NOT MATCHED AND product LIKE '%washer%' THEN
  INSERT (product, quantity) VALUES(product, quantity)
WHEN NOT MATCHED BY SOURCE AND product LIKE '%washer%' THEN
  DELETE
--------------------
MERGE INTO dataset.Inventory T
USING dataset.NewArrivals S ON false
WHEN NOT MATCHED THEN INSERT (product, quantity)
VALUES (product, quantity)
	WHERE product LIKE '%washer%'
WHEN NOT MATCHED BY SOURCE AND product LIKE '%washer%' THEN DELETE
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.NewArrivals T
USING (SELECT * FROM dataset.NewArrivals WHERE warehouse <> 'warehouse #2') S
ON T.product = S.product
WHEN MATCHED AND T.warehouse = 'warehouse #1' THEN
  UPDATE SET quantity = T.quantity + 20
WHEN MATCHED THEN
  DELETE
--------------------
MERGE INTO dataset.NewArrivals T
USING (
	SELECT *
	FROM dataset.NewArrivals
	WHERE warehouse <> 'warehouse #2'
) S ON T.product = S.product
WHEN MATCHED THEN UPDATE
SET quantity = T.quantity + 20
WHERE T.warehouse = 'warehouse #1'
WHEN MATCHED THEN DELETE
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.Inventory T
USING dataset.NewArrivals S
ON T.product = S.product
WHEN MATCHED THEN
  UPDATE SET quantity = T.quantity + S.quantity
WHEN NOT MATCHED THEN
  INSERT (product, quantity) VALUES(product, quantity)
--------------------
MERGE INTO dataset.Inventory T
USING dataset.NewArrivals S ON T.product = S.product
WHEN MATCHED THEN UPDATE
SET quantity = T.quantity + S.quantity
WHEN NOT MATCHED THEN INSERT (product, quantity)
VALUES (product, quantity)
------------------------------------------------------------------------------------------------------------------------
MERGE dataset.DetailedInventory T
USING dataset.Inventory S
ON T.product = S.product
WHEN NOT MATCHED AND quantity < 20 THEN
  INSERT(product, quantity, supply_constrained, comments)
  VALUES(product, quantity, true, NULL)
WHEN NOT MATCHED THEN
  INSERT(product, quantity, supply_constrained)
  VALUES(product, quantity, false)
--------------------
MERGE INTO dataset.DetailedInventory T
USING dataset.Inventory S ON T.product = S.product
WHEN NOT MATCHED THEN INSERT (product, quantity, supply_constrained, comments)
VALUES (product, quantity, true, NULL)
	WHERE quantity < 20
WHEN NOT MATCHED THEN INSERT (product, quantity, supply_constrained)
VALUES (product, quantity, false)
------------------------------------------------------------------------------------------------------------------------
SELECT ARRAY(SELECT AS STRUCT
        cast(coalesce(a,0) as NUMERIC) as seconds_amount,
        cast(coalesce(b,0) as NUMERIC) as nanos_amount,
      )
from t1
--------------------
SELECT ARRAY(
		SELECT AS STRUCT CAST(coalesce(a, 0) AS NUMERIC) AS seconds_amount, CAST(coalesce(b, 0) AS NUMERIC) AS nanos_amount
	)
FROM t1
------------------------------------------------------------------------------------------------------------------------
SELECT "[\"sameday\"]"
--------------------
SELECT '["sameday"]'
------------------------------------------------------------------------------------------------------------------------
SELECT DATEADD("2024-10-01", -1, 'mm');
--------------------
SELECT DATEADD('2024-10-01', -1, 'mm');
------------------------------------------------------------------------------------------------------------------------
DROP TABLE `a.b`;
--------------------
DROP TABLE a.b;
------------------------------------------------------------------------------------------------------------------------
INSERT `a.b`
SELECT *
FROM `c.d`;
--------------------
INSERT INTO a.b
SELECT *
FROM c.d;
------------------------------------------------------------------------------------------------------------------------
/* a
   b
   c
*/
select 1
--------------------
/* a
   b
   c
*/
SELECT 1
------------------------------------------------------------------------------------------------------------------------

select * FROM
##`a-b.c.e`
`a-b-c-e`
--------------------
SELECT *
FROM  ##`a-b.c.e`
`a-b-c-e`
------------------------------------------------------------------------------------------------------------------------
SELECT *
FROM p-- [NEW!]
--------------------
SELECT *
FROM p -- [NEW!]
------------------------------------------------------------------------------------------------------------------------
SELECT a.* EXCEPT (f1, f2) from t
--------------------
SELECT a.* EXCEPT(f1, f2)
FROM t
------------------------------------------------------------------------------------------------------------------------
SELECT CAST(b'\x48\x65\x6c\x6c\x6f' AS STRING FORMAT 'ASCII') AS bytes_to_string;
--------------------
SELECT CAST(b'\x48\x65\x6c\x6c\x6f' AS STRING FORMAT 'ASCII') AS bytes_to_string;
------------------------------------------------------------------------------------------------------------------------
select """abc""", '''it's''', '''Title:"Boy"''', '''two
                                                 lines''', '''why\?'''
--------------------
SELECT 'abc', 'it''s', 'Title:"Boy"'
	, 'two
                                                 lines', 'why\?'
------------------------------------------------------------------------------------------------------------------------
UPDATE
  `p1` main
SET
  a = FALSE,
  b = dup.x
FROM
  tp dup
WHERE
  main.x = dup.u;
--------------------
UPDATE `p1` main
SET a = false, b = dup.x
FROM tp dup
WHERE main.x = dup.u;
------------------------------------------------------------------------------------------------------------------------
SELECT STRUCT<x int64, y string>(1, t.str_col)
--------------------
SELECT STRUCT<x int64, y string>(1, t.str_col)
------------------------------------------------------------------------------------------------------------------------
SELECT
    CAST(NULL AS STRING) AS pn,
    ARRAY(
      SELECT AS STRUCT
        CAST(NULL AS STRING) pt,
        CAST(NULL AS STRING) pn
      ) AS p,
    a.s,
FROM `d-k-id-m.g.x` AS a
--------------------
SELECT CAST(NULL AS STRING) AS pn, ARRAY(
		SELECT AS STRUCT CAST(NULL AS STRING) AS pt, CAST(NULL AS STRING) AS pn
	) AS p, a.s
FROM `d-k-id-m`.g.x a
------------------------------------------------------------------------------------------------------------------------
WITH PlayerStats AS (
  SELECT 'Adams' as LastName, 'Noam' as FirstName, 3 as PointsScored UNION ALL
  SELECT 'Buchanan', 'Jie', 0 UNION ALL
  SELECT 'Coolidge', 'Kiran', 1 UNION ALL
  SELECT 'Adams', 'Noam', 4 UNION ALL
  SELECT 'Buchanan', 'Jie', 13)
SELECT
  SUM(PointsScored) AS total_points,
  FirstName AS first_name,
  LastName AS last_name
FROM PlayerStats
GROUP BY ALL;
--------------------
WITH PlayerStats AS (
		SELECT 'Adams' AS LastName, 'Noam' AS FirstName, 3 AS PointsScored
		UNION ALL
		SELECT 'Buchanan', 'Jie', 0
		UNION ALL
		SELECT 'Coolidge', 'Kiran', 1
		UNION ALL
		SELECT 'Adams', 'Noam', 4
		UNION ALL
		SELECT 'Buchanan', 'Jie', 13
	)
SELECT SUM(PointsScored) AS total_points, FirstName AS first_name, LastName AS last_name
FROM PlayerStats
GROUP BY ALL;
------------------------------------------------------------------------------------------------------------------------
DELETE FROM `data-c-id-mart.x.b` AS a
--------------------
DELETE FROM `data-c-id-mart`.x.b a
------------------------------------------------------------------------------------------------------------------------
SELECT case when contains(u.roles[company_id_cross], 'report_reader') then 1 else 0 end is_report_reader from t1
--------------------
SELECT CASE
		WHEN CONTAINS (u.roles[company_id_cross], 'report_reader') THEN 1
		ELSE 0
	END AS is_report_reader
FROM t1
------------------------------------------------------------------------------------------------------------------------
SELECT ARRAY_AGG(service_type_name IGNORE NULLS ORDER BY event_timestamp DESC LIMIT 10)[OFFSET(0)] AS service_type_name
--------------------
SELECT ARRAY_AGG(service_type_name IGNORE NULLS ORDER BY event_timestamp DESC LIMIT 10)[OFFSET(0)] AS service_type_name
------------------------------------------------------------------------------------------------------------------------
SELECT * from a table
--------------------
SELECT *
FROM a table
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE
  mydataset.newtable (transaction_id INT64, transaction_ts TIMESTAMP)
PARTITION BY
  TIMESTAMP_TRUNC(transaction_ts, DAY)
  OPTIONS (
    partition_expiration_days = 3,
    require_partition_filter = TRUE);
--------------------
CREATE TABLE mydataset.newtable (
	transaction_id INT64,
	transaction_ts TIMESTAMP
)
PARTITION BY TIMESTAMP_TRUNC(transaction_ts, DAY)
OPTIONS (
	partition_expiration_days = 3,
	require_partition_filter = true
);
------------------------------------------------------------------------------------------------------------------------
SELECT SAFE_CAST("apple" AS INT64) AS not_a_number;
--------------------
SELECT SAFE_CAST('apple' AS INT64) AS not_a_number;
------------------------------------------------------------------------------------------------------------------------
SELECT
ARRAY_AGG(
  table ORDER BY ts DESC LIMIT 1
)[OFFSET(0)] event
FROM cleaned
--------------------
SELECT ARRAY_AGG(table ORDER BY ts DESC LIMIT 1)[OFFSET(0)] AS event
FROM cleaned
------------------------------------------------------------------------------------------------------------------------
SELECT ARRAY_AGG(x LIMIT 5) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
--------------------
SELECT ARRAY_AGG(x LIMIT 5) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2,
	1, 2]) AS x;
------------------------------------------------------------------------------------------------------------------------
ASSERT (
  (SELECT COUNT(*) > 5 FROM UNNEST([1, 2, 3, 4, 5, 6]))
) AS 'Table must contain more than 5 rows.';
--------------------
ASSERT (
	SELECT COUNT(*) > 5
	FROM UNNEST([1, 2, 3, 4, 5,
		6]))
AS '''Table must contain more than 5 rows.''';
------------------------------------------------------------------------------------------------------------------------
SELECT f1
FROM UNNEST(t1)
WHERE f1 IS NOT NULL
ORDER BY event_timestamp DESC
LIMIT 1
--------------------
SELECT f1
FROM UNNEST(t1)
WHERE f1 IS NOT NULL
ORDER BY event_timestamp DESC
LIMIT 1
------------------------------------------------------------------------------------------------------------------------
CREATE TEMP TABLE xxx
PARTITION BY date(partitiontime)
CLUSTER BY latest_status_name
AS
  select 1;
--------------------
CREATE TEMPORARY TABLE xxx
PARTITION BY date(partitiontime)
CLUSTER BY latest_status_name
AS
SELECT 1;
------------------------------------------------------------------------------------------------------------------------
select *
from t
where TIMESTAMP(abc.created_timestamp) >= area.effective_timestamp
--------------------
SELECT *
FROM t
WHERE TIMESTAMP(abc.created_timestamp) >= area.effective_timestamp
------------------------------------------------------------------------------------------------------------------------
WITH
  Items AS (
    SELECT STRUCT('blue' AS color, 'round' AS shape) AS info UNION ALL
    SELECT STRUCT('blue', 'square') UNION ALL
    SELECT STRUCT('red', 'round')
  )
SELECT * FROM Items;
--------------------
WITH Items AS (
		SELECT STRUCT('blue' AS color, 'round' AS shape) AS info
		UNION ALL
		SELECT STRUCT('blue', 'square')
		UNION ALL
		SELECT STRUCT('red', 'round')
	)
SELECT *
FROM Items;
------------------------------------------------------------------------------------------------------------------------
SELECT DATE_ADD(DATE '2008-12-25', INTERVAL 5 DAY) AS five_days_later;
--------------------
SELECT DATE_ADD(DATE '2008-12-25', INTERVAL 5 DAY) AS five_days_later;
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE mydataset.myclusteredtable
(
  customer_id STRING,
  transaction_amount NUMERIC
)
PARTITION BY DATE(_PARTITIONTIME)
CLUSTER BY
  customer_id
OPTIONS (
  partition_expiration_days=3,
  description="a table clustered by customer_id"
)
--------------------
CREATE TABLE mydataset.myclusteredtable (
	customer_id STRING,
	transaction_amount NUMERIC
)
PARTITION BY DATE(_PARTITIONTIME)
CLUSTER BY customer_id
OPTIONS (
	partition_expiration_days = 3,
	description = 'a table clustered by customer_id'
)
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE `myproject.mydataset.mytable`
CLONE `myproject.mydataset.mytablesnapshot`
OPTIONS(
  expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 365 DAY),
  friendly_name="my_table",
  description="A table that expires in 1 year",
  labels=[("org_unit", "development")]
)
--------------------
CREATE TABLE myproject.mydataset.mytable
CLONE myproject.mydataset.mytablesnapshot
OPTIONS (
	expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 365 DAY),
	friendly_name = 'my_table',
	description = 'A table that expires in 1 year',
	labels = [('org_unit', 'development')]
)
------------------------------------------------------------------------------------------------------------------------
SELECT
  WITH DIFFERENTIAL_PRIVACY
    OPTIONS(epsilon=10, delta=.01, max_groups_contributed=2, privacy_unit_column=id)
    item,
    AVG(quantity, contribution_bounds_per_group => (0,100)) AS average_quantity
FROM professors
GROUP BY item;
--------------------
SELECT
	WITH DIFFERENTIAL_PRIVACY
	OPTIONS (epsilon = 10,delta = .01,max_groups_contributed = 2,privacy_unit_column = id)
	item, AVG(quantity, contribution_bounds_per_group => (0, 100)) AS average_quantity
FROM professors
GROUP BY item;
------------------------------------------------------------------------------------------------------------------------
select cast(json_parse(tickets_fields_value) as array<varchar>) as tickets_fields_value
--------------------
SELECT CAST(json_parse(tickets_fields_value) AS ARRAY<varchar>) AS tickets_fields_value