如何在Google大查询中将列转换为行

时间:2019-04-03 13:23:59

标签: sql google-bigquery

我正在尝试将列转换为行,因为我想看到经历我们事件的人员的漏斗,而一个事件是带有参数的。

我试图选择所有数据,但是我已经选择了所需的数据作为列,而不是行和一条记录。

SELECT event_name, 
       (SELECT count(distinct(user_pseudo_id))
       FROM `dataset` as t2, UNNEST(event_params) as p
       WHERE t2.event_name = "event_name1" and p.value.string_value IN ("param1","param2","param3","param4")
       ),
       count(distinct(user_pseudo_id))
FROM `dataset` as t1
WHERE event_name IN ("event_name1",
"event_name2",
"event_name3") AND
GROUP BY 1
ORDER BY 3 DESC

我收到以下结果:

event_names    f0       f1  
event_name1    1600     2000
event_name2    1600     1400
event_name3    1600     1200

这是我想要的输出:

event_names           f1    
event_name1           2000
event_name1_param     1600     -> the result from f0
event_name2           1400
event_name3           1200

1 个答案:

答案 0 :(得分:0)

通过工会全部解决。

SELECT event_name,
                 count(distinct user_pseudo_id)
FROM `dataset` as t1
WHERE event_name IN ("event_name1","event_name2","event_name3")

union all

SELECT event_name, --případně jako "event_name1_param"
                  count(distinct user_pseudo_id)
FROM `dataset` as t2, UNNEST(event_params) as p
WHERE t2.event_name = "event_name1"
and p.value.string_value IN ("param1","param2","param3","param4")