大家好,
我已经在站点上找到了解决类似问题的解决方案,它只有一列,这对我有所帮助。但是,当我需要对数组进行嵌套时,我会停留在最后一部分。
这是到目前为止的查询:
#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 ¦
--------+---------+-------+----------+
等,等等
我发现问题是我的查询中出现双重错误,试图将每个查询都连接在一起,但是当我仅选择ItemCode时,根本没有得到UOM和因子...
请问我需要如何修改查询才能获得此结果?
答案 0 :(得分:1)
尝试一下:
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)
答案 1 :(得分: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