![](/img/trans.png)
[英]Using Big Query SQL I can I achieve the following subtraction ( subtract two row values and replace it with that value )?
[英]Big_query SQL To have single row result instead of two rows
我在这里使用 Big Query Sql 这是表格构建 此表显示客户 id_123 在 type_shop 和 delivery_shop 以及 delivery_home 中进行了购买。
我是否有可能将结果反映在单行而不是 2 个不同的行中? 我只想显示此客户 id_123 在 type_shop 中购买并连续使用 delivery_home 和 delivery_shop
我尝试了一些使用 array_agg(stru) 的方法,但它仍然显示 2 行结果而不是 1 行。不确定我应该在这里尝试什么其他 SQL function? 尝试在堆栈溢出中搜索类似的内容,但没有我可以应用的内容。
假设您的样本数据是您的第一个表。 考虑以下方法:
with sample_data as (
select 'id_123' as customer, 'm' as gender, [1,1] as type_shop, [0,0] as type_online, [0,0] as delivery_pickup,[0,1] as delivery_home, [1,0] as delivery_shop,
union all select 'id_456' as customer, 'f' as gender, [1,0,1] as type_shop, [0,1,0] as type_online, [0,0,0] as delivery_pickup,[1,0,0] as delivery_home, [0,1,1] as delivery_shop,
),
normalize_data as (
select
customer,
gender,
type_shop[safe_offset(index)] as type_shop,
type_online[safe_offset(index)] as type_online,
delivery_pickup[safe_offset(index)] as delivery_pickup,
delivery_home[safe_offset(index)] as delivery_home,
delivery_shop[safe_offset(index)] as delivery_shop,
from sample_data,
unnest(generate_array(0,array_length(type_shop)-1)) as index
),
join_data as (
select
customer,
gender,
max(type_shop) as t_shop,
max(type_online) as t_online,
max(delivery_pickup) as delivery_pickup,
max(delivery_home) as delivery_home,
max(delivery_shop) as delivery_shop,
from normalize_data
group by customer,gender,type_shop,type_online
)
select
customer,
gender,
array_agg(t_shop) as type_shop,
array_agg(t_online) as type_online,
array_agg(delivery_pickup) as delivery_pickup,
array_agg(delivery_home) as delivery_home,
array_agg(delivery_shop) as delivery_shop,
from join_data
group by customer,gender
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.