Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Quickly Flatten a SQL Table

Tags:

I'm using Presto. If I have a table like:

ID    CATEGORY     VALUE
1      a           ...
1      b
1      c
2      a
2      b
3      b 
3      d
3      e
3      f

How would you convert to the below without writing a case statement for each combination?

ID      A     B    C    D    E    F
1
2
3
like image 369
Moosa Avatar asked Jun 09 '16 22:06

Moosa


People also ask

What is flattening of data in SQL?

Data flattening usually refers to the act of flattening semi-structured data, such as name-value pairs in JSON, into separate columns where the name becomes the column name that holds the values in the rows. Data unflattening is the opposite; adding nested structure to relational data.

What does it mean to flatten a table?

Flattening is the process of packing denormalized data created by joining tables in a "one to many" (1:M) relationship, into repeating groups in the IDT. It can significantly increase search performance.

How do I flatten an array in SQL?

To convert an ARRAY into a set of rows, also known as "flattening," use the UNNEST operator. UNNEST takes an ARRAY and returns a table with a single row for each element in the ARRAY . Because UNNEST destroys the order of the ARRAY elements, you may wish to restore order to the table.


1 Answers

I've never used Presto and the documentation seems pretty thin, but based on this article it looks like you could do

SELECT
  id,
  kv['A'] AS A,
  kv['B'] AS B,
  kv['C'] AS C,
  kv['D'] AS D,
  kv['E'] AS E,
  kv['F'] AS F
FROM (
  SELECT id, map_agg(category, value) kv
  FROM vtable
  GROUP BY id
) t

Although I'd recommend doing this in the display layer if possible since you have to specify the columns. Most reporting tools and UI grids support some sort of dynamic pivoting that will create columns based on the source data.

like image 170
D Stanley Avatar answered Sep 28 '22 01:09

D Stanley