File: object_unpivot.sql

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (23 lines) | stat: -rw-r--r-- 628 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WITH example_data AS (
    SELECT
        10 AS shop_id
        , json_parse('{"apple_count": 2, "orange_count": 6}') AS inventory
    UNION ALL
    SELECT
        20 AS shop_id
        , json_parse('{"pear_count": 10, "other_data": 42}') AS inventory
    UNION ALL
    SELECT
        30 AS shop_id
        , json_parse('{"apple_count": 3, "lemon_count": 5}') AS inventory
)

SELECT
    shop_id
    , key
    , value
FROM example_data ed, UNPIVOT ed.inventory AS value AT key;

SELECT attr as attribute_name, val as object_value
FROM customer_orders_lineitem c, c.c_orders AS o, UNPIVOT o AS val AT attr
WHERE c_custkey = 9451;