在 Google BigQuery/Excel 中转置
Transpose in Google BigQuery/Excel
我对 BQ 中的数据传输有疑问(或者实际导出并在 Excel 中执行)。我正在尝试获得此结果(抱歉,我是新手,不确定如何分隔 2 列,variant1 和 variant2 应该是 2 列):
ClientID
Date
Variant1. Variant2
AB
12/2
123. 456
我当前的查询将给出以下输出:
ClientID
Date
Variant
AB
12/1
123
AB
12/2
456
SELECT DISTINCT
case when (hits.ecommerceAction.action_type = '3') then date end date, [enter image description here][1]
clientId AS client_id,
page.pagepath as pagepath,
product.productVariant as variant,
FROM
`xxxx.ga_sessions_`,
UNNEST(hits) AS hits, unnest(hits.product) as product
有没有我可以用来实现转置步骤的方法?我目前的输出更像是一个主数据,所有与产品相关的信息都在一栏下。如果您能分享任何想法,我们将不胜感激!
考虑以下方法
select * from (
select ClientID, Variant, Pagepath,
max(Date) over win Date,
row_number() over (win order by Date) pos
from your_current_output
window win as (partition by ClientID)
)
pivot (any_value(Variant) as Variant, any_value(Pagepath) as Pagepath for pos in (1,2,3))
是否适用于您问题中的样本
with your_current_output as (
select '12/1' Date, 123 ClientID, 'abc' Variant, 'fis.com' Pagepath union all
select '12/2', 123, 'efg', 'fere.com'
)
输出是
我对 BQ 中的数据传输有疑问(或者实际导出并在 Excel 中执行)。我正在尝试获得此结果(抱歉,我是新手,不确定如何分隔 2 列,variant1 和 variant2 应该是 2 列):
ClientID | Date | Variant1. Variant2 |
---|---|---|
AB | 12/2 | 123. 456 |
我当前的查询将给出以下输出:
ClientID | Date | Variant |
---|---|---|
AB | 12/1 | 123 |
AB | 12/2 | 456 |
SELECT DISTINCT
case when (hits.ecommerceAction.action_type = '3') then date end date, [enter image description here][1]
clientId AS client_id,
page.pagepath as pagepath,
product.productVariant as variant,
FROM
`xxxx.ga_sessions_`,
UNNEST(hits) AS hits, unnest(hits.product) as product
有没有我可以用来实现转置步骤的方法?我目前的输出更像是一个主数据,所有与产品相关的信息都在一栏下。如果您能分享任何想法,我们将不胜感激!
考虑以下方法
select * from (
select ClientID, Variant, Pagepath,
max(Date) over win Date,
row_number() over (win order by Date) pos
from your_current_output
window win as (partition by ClientID)
)
pivot (any_value(Variant) as Variant, any_value(Pagepath) as Pagepath for pos in (1,2,3))
是否适用于您问题中的样本
with your_current_output as (
select '12/1' Date, 123 ClientID, 'abc' Variant, 'fis.com' Pagepath union all
select '12/2', 123, 'efg', 'fere.com'
)
输出是