Search code examples
google-bigqueryspring-cloud-gcp-bigquery

How to join two tables that has one to many relationship in bigquery


Let say I have student Table like below in BigQuery:

Student Table

Mark Table looks like below in Big Query:

mark table

I would like to join this two tables and create a new table like below :

enter image description here


Solution

  • Consider below query,

    SELECT s.*, m.Mark_Details
      FROM student_table s
      JOIN (
        SELECT student_ID,
               STRING_AGG('' || Subject_ID || ':' || Subject_Name || ':' || Mark, ' | ') AS Mark_Details
          FROM mark_table
         GROUP BY 1
      ) m USING (student_ID)