File: array_unnest.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 (30 lines) | stat: -rw-r--r-- 793 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
24
25
26
27
28
29
30
WITH example_data AS (
    SELECT
        10 AS shop_id
        , json_parse('[1, 2]') AS inventory
    UNION ALL
    SELECT
        20 AS shop_id
        , json_parse('[3, 4, 5]') AS inventory
    UNION ALL
    SELECT
        30 AS shop_id
        , json_parse('[6, 7, 8, 9]') AS inventory
)

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

SELECT c_name, orders.o_orderkey AS orderkey, index AS orderkey_index
FROM customer_orders_lineitem c, c.c_orders AS orders AT index
ORDER BY orderkey_index;

-- can extract the correlated values from multiple arrays using the index variable
SELECT
    value_a::BIGINT,
    array_b[idx]::VARCHAR AS value_b,
    array_c[MOD(idx, 3) + 1]::FLOAT8 AS value_c
FROM mytable t, t.array_a AS value_a AT idx;