尝试将多列中的单元格拆分为行 - UNNEST 查询

Trying to split cells in multiple columns into rows - UNNEST query

大家好,

我已经在网站上找到了针对单个列的类似问题的解决方案,这对我的中途有所帮助,但是当我需要取消嵌套数组时,我卡在了最后一部分。

这是目前的查询:

#StandardSQL
With Data AS
(
SELECT 'aaa' as Item, 'EA/BX/PA' as UOM, '1/10/100' as Factor UNION ALL
SELECT 'bbb' as Item, 'EA/PA' as UOM, '1/50' as Factor UNION ALL
SELECT 'ccc' as Item, null as UOM, null as Factor 
),
SplitData AS
(
SELECT
Item,
SPLIT(UOM, "/") as UOM,
SPLIT(Factor, "/") as Factor
FROM Data
)
SELECT
*
FROM
SplitData,
UNNEST(SplitData.UOM),UNNEST(SplitData.Factor)

结果是:

--Row---+--Item---+--UOM--¦--Factor--+--f0_--+--f1_--
   1    ¦  aaa    ¦  EA   ¦    1     ¦  EA   ¦  1
        ¦         ¦  BX   ¦   10     ¦       ¦
        ¦         ¦  PA   ¦  100     ¦       ¦
--------+---------+-------+----------+-------+-------    
   2    ¦  aaa    ¦  EA   ¦    1     ¦  EA   ¦  10
        ¦         ¦  BX   ¦   10     ¦       ¦
        ¦         ¦  PA   ¦  100     ¦       ¦
--------+---------+-------+----------+-------+-------

等等,等等

我想看的是:

--Row---+--Item---+--UOM--¦--Factor--+
   1    ¦  aaa    ¦  EA   ¦    1     ¦
--------+---------+-------+----------+
   2    ¦  aaa    ¦  BX   ¦   10     ¦
--------+---------+-------+----------+
   3    ¦  aaa    ¦  PA   ¦  100     ¦
--------+---------+-------+----------+
   4    ¦  bbb    ¦  EA   ¦    1     ¦ 
--------+---------+-------+----------+

等等,等等

我知道问题是我的查询中的双重嵌套试图将每个与每个连接起来,但是当我 select 只有 ItemCode 时,我根本没有得到 UOM 和因子。 ..

知道我需要如何修改查询才能得到这个结果吗?

试试这个:

With

Data AS (
    SELECT 'aaa' as Item, 'EA/BX/PA' as UOM, '1/10/100' as Factor UNION ALL
    SELECT 'bbb' as Item, 'EA/PA' as UOM, '1/50' as Factor UNION ALL
    SELECT 'ccc' as Item, null as UOM, null as Factor 
),

SplitData AS (
    SELECT
        Item,
        SPLIT(UOM, "/") as UOM,
        SPLIT(Factor, "/") as Factor
    FROM
        Data
)

SELECT
    *,
    Factor[offset(uom_offset)] as unnested_factor
FROM
    SplitData as sd
    cross join UNNEST(sd.UOM) as unnested_uom  -- use left join if you want to display 'ccc' Item
        with offset as uom_offset

还有一个:

With

Data AS (
    SELECT 'aaa' as Item, 'EA/BX/PA' as UOM, '1/10/100' as Factor UNION ALL
    SELECT 'bbb' as Item, 'EA/PA' as UOM, '1/50' as Factor UNION ALL
    SELECT 'ccc' as Item, null as UOM, null as Factor
),

SplitData AS (
    SELECT
        Item,
        SPLIT(UOM, "/") as UOM,
        SPLIT(Factor, "/") as Factor
    FROM Data
)

SELECT
    sd.*,
    unnested_uom,
    unnested_factor
FROM
    SplitData as sd
    left join UNNEST(sd.UOM) as unnested_uom
        with offset as uom_offset
    left join UNNEST(sd.Factor) as unnested_factor
        with offset as factor_offset
where
    coalesce(uom_offset, factor_offset, 1)
    = coalesce(factor_offset, uom_offset, 1)

以下适用于 BigQuery Standard SQL 并假设(基于示例数据示例)UOM 和 Factor 中的元素数量相同

#standardSQL
SELECT item, u AS UOM, f AS Factor
FROM `project.dataset.data`
LEFT JOIN UNNEST(SPLIT(UOM, '/')) u WITH OFFSET
JOIN UNNEST(SPLIT(Factor, '/')) f WITH OFFSET
USING(OFFSET)

如果应用于您问题中的示例数据 - 结果是

Row item    UOM     Factor   
1   aaa     EA      1    
2   aaa     BX      10   
3   aaa     PA      100  
4   bbb     EA      1    
5   bbb     PA      50