BigQuery - Concatenate multiple rows into a single row

Jaison

I have a BigQuery table with 2 columns:

id|name
1|John
1|Tom
1|Bob
2|Jack
2|Tim

Expected output: Concatenate names grouped by id

id|Text
1|John,Tom,Bob
2|Jack,Tim
Mikhail Berlyant

For BigQuery Standard SQL:

#standardSQL
--WITH yourTable AS (
--  SELECT 1 AS id, 'John' AS name UNION ALL
--  SELECT 1, 'Tom' UNION ALL
--  SELECT 1, 'Bob' UNION ALL
--  SELECT 2, 'Jack' UNION ALL
--  SELECT 2, 'Tim' 
--)
SELECT 
  id, 
  STRING_AGG(name ORDER BY name) AS Text 
FROM yourTable 
GROUP BY id

Optional ORDER BY name within STRING_CONCAT allows you to get out sorted list of names as below

id  Text     
1   Bob,John,Tom     
2   Jack,Tim     

For Legacy SQL

#legacySQL
SELECT 
  id, 
  GROUP_CONCAT(name) AS Text   
FROM yourTable
GROUP BY id  

If you would need to output sorted list here, you can use below (formally - it is not guaranteed by BigQuery Legacy SQL to get sorted list - but for most practical cases I had - it worked)

#legacySQL
SELECT 
  id, 
  GROUP_CONCAT(name) AS Text 
FROM (
  SELECT id, name 
  FROM yourTable 
  ORDER BY name
)
GROUP BY id  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Single row to multiple columns and rows

Merging multiple rows in a spark dataframe into a single row

Postgres Multiple Rows as Single Row

Group Multiple Rows Into Single Row

How to join two tables of mysql and concatenate multiple rows into single cell?

Concatenate multiple rows into one row pandas

Concatenate a column from multiple rows into a single formatted string

Concatenate rows into single column

Use LINQ to concatenate multiple rows list with same value into single row

MariaDB SELECT multiple rows as single row

How to concatenate multiple rows into a single row and repeate this operation over a big dataframe?

How to concatenate multiple rows of strings into one row?

BigQuery - Concatenate multiple columns into a single column for large numbers of columns

How to insert multiple rows in a table with single row?

Convert multiple rows to single row for same Material

Inserting multiple rows with column into single row

Common multiple rows in single row in oracle sql

Concatenate multiple rows to single row with condition check using recursive CTE

Paste into multiple rows and not a single row

Concatenate field from multiple rows into single field

Converting multiple rows into single row with multiple columns

Standard SQL in BigQuery - Get concatenate text from multiple rows

BigQuery - Concatenate multiple rows and columns into a single row

Convert single row into multiple rows Bigquery SQL

Concatenate/join multiple rows of strings into one single row for the entire dataframe

Merge multiple rows to single row

How to concatenate multiple rows list with same value into single row

multiple rows into single row in pandas

Transposing multiple rows to single row

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive