google-bigquery

BigQuery/SQL: How do I join two tables and use a column value as column name?

。_饼干妹妹 提交于 2020-05-24 07:55:32
问题 I have these tables: Foods | food_id | title | | 1 | soy milk | | 2 | banana | | 3 | apple | Nutrients | food_id | nutrient_id | amount | | 1 | n1 | 0.05 | | 1 | n2 | 2 | | 1 | n3 | 34 | ... I need this: | food_id | title | n1 | n2 | n3 | | 1 | soy milk | 0.05 | 2 | 34 | | 2 | banana | | | | | 3 | apple | | | | Struct would also work. I know all the joins, but can't wrap my head around this... how do I put nutrient_id into a column title or a Struct key? 回答1: If you have a fixed list of

changing column values in bigquery

回眸只為那壹抹淺笑 提交于 2020-05-24 07:31:07
问题 I have a table where there is a column with different values like America, South Korea, Japan and so on. I would like to replace the values with America=USA, South Korea=SA, Japan= JP like these. What would be the code? 回答1: The best way to probably handle this would be to maintain a separate table which maps full country names to their two letter codes: country_full | country_abbr America | USA South Korea | SA Japan | JP Then, you may join your current table to this lookup table to bring in

Big query: LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-24 05:07:49
问题 Help, please. I created the next query but gen issue and I don`t understand how to fix it enter image description here SELECT tb1.* FROM (SELECT tb1.date, clientId, REGEXP_EXTRACT (hits.pagePath,"^([^\?]+)\?") as page_url, hits.type as type, hits.eventInfo.eventCategory AS eventCategory, hits.eventInfo.eventAction AS eventAction, hits.eventInfo.eventLabel AS person_email, FROM `table` AS tb1, UNNEST (hits) AS hits) as tb1 where tb1.type = "pageview" or (tb1.eventCategory = "Enroll_Free

Capture changes between 2 datasets with duplicates

蹲街弑〆低调 提交于 2020-05-17 06:27:47
问题 This is a follow-up question of Capture changes in 2 datasets. I need to capture change between 2 datasets based on key(s): one historical and another current version of the same dataset (both datasets share same schema). These datasets can have duplicate rows as well. In below example id is considered key for comparison: -- Table t_curr ------- id col ------- 1 A 1 B 2 C 3 F -- Table t_hist ------- id col ------- 1 B 2 C 2 D 4 G -- Expected output t_change ---------------- id col change ----

Capture changes between 2 datasets with duplicates

蓝咒 提交于 2020-05-17 06:27:06
问题 This is a follow-up question of Capture changes in 2 datasets. I need to capture change between 2 datasets based on key(s): one historical and another current version of the same dataset (both datasets share same schema). These datasets can have duplicate rows as well. In below example id is considered key for comparison: -- Table t_curr ------- id col ------- 1 A 1 B 2 C 3 F -- Table t_hist ------- id col ------- 1 B 2 C 2 D 4 G -- Expected output t_change ---------------- id col change ----

Schema modification giving partitioning error

故事扮演 提交于 2020-05-17 06:24:07
问题 I have previously created a table in bigquery using following script in terraform resource "google_bigquery_dataset" "my-dataset" { dataset_id = "datasetname" description = "description" } resource "google_bigquery_table" "mytable" { dataset_id = google_bigquery_dataset.my-dataset.dataset_id table_id = "mytable" time_partitioning { type = "DAY" } schema = <<EOF [ { "name": "field_one", "type": "STRING", "mode": "NULLABLE" }, { "name": "field_two", "type": "RECORD", "mode": "NULLABLE", "fields

String Aggregtion to create pivot columns in Big Query

爱⌒轻易说出口 提交于 2020-05-17 05:49:07
问题 I am trying to implement this exact solution linked below, but it seems GROUP_CONCAT_UNQUOTED is no longer a valid BQ function. Is there a solution to this that works in 2020? p.s. I would have just commented on the original post, but apparently my reputation isn't high enough yet... Transpose rows into columns in BigQuery (Pivot implementation) 回答1: Below example is for BigQuery Standard SQL Assume you have data as below #standardSQL WITH `project.dataset.table` AS ( SELECT 1 id, 'channel

String Aggregtion to create pivot columns in Big Query

≡放荡痞女 提交于 2020-05-17 05:49:07
问题 I am trying to implement this exact solution linked below, but it seems GROUP_CONCAT_UNQUOTED is no longer a valid BQ function. Is there a solution to this that works in 2020? p.s. I would have just commented on the original post, but apparently my reputation isn't high enough yet... Transpose rows into columns in BigQuery (Pivot implementation) 回答1: Below example is for BigQuery Standard SQL Assume you have data as below #standardSQL WITH `project.dataset.table` AS ( SELECT 1 id, 'channel

Capture changes in 2 datasets

泪湿孤枕 提交于 2020-05-17 05:45:33
问题 I need to capture change between 2 datasets based on key(s): one historical and another current version of the same dataset (both datasets share same schema). For example for input tables: -- Table t_hist ------- id col ------- 1 A 2 B 3 C 4 D -- Table t_curr ------- id col ------- 1 a 2 B 4 d 5 E Expected result (considering id as comparison key): -- Table t_change ---------------- id col change ---------------- 1 a modified 2 B same 3 C deleted 4 d modified 5 E inserted A naive approach

How to change the col type of a BigQuery repeated record

放肆的年华 提交于 2020-05-15 22:43:47
问题 I'm trying to change a col type of a repeated record from STRING to TIMESTAMP . There are a few suggestions from BQ docs here (manually-changing-schemas). However, I'm running into issues with each of the recommended suggestions. Here is an example schema: { 'name' => 'id', 'type' => 'STRING', 'mode' => 'REQUIRED' }, { 'name' => 'name', 'type' => 'STRING', 'mode' => 'REQUIRED' }, // many more fields including nested records and repeated records { 'name' => 'locations', 'type' => 'RECORD',