Search code examples
sqlprestotrino

How do we get dict like string column in Presto from multiple columns?


We have a table

|      Label          |      Value1          |     Value 2      |
| ------------------- | -------------------- |------------------|
|    0.99993938       |    1.88473736        |   2.47467484     |
|    4.66993938       |    10.7473736        |   0.37636378     |

How do we create a new column with dict like string?

Expected outcome:

|                 Value3                          |
|-------------------------------------------------|
|     {0.99993938 : [1.88473736, 2.47467484]}     |
|     {4.66993938 : [10.7473736, 0.37636378]}     |

Solution

  • You could just use plain string concatenation here:

    SELECT Value1, Value2, CONCAT('{', Label, ' : [', Value1, ', ', Value2, ']}') AS Value3
    FROM yourTable;