如何解决BigQuery-列成行 当前表从CTE生成我的预期输出:
我正在尝试将行转换为列(枢轴类型)
当前表(从CTE生成)
with cte as (select date(start_time) as date,type,count(*) as total
from table
where start_time >='2020-09-09'
group by 1,2
order by 1),c1 as(
select date,total as C1 from cte where type='c1'),c2 as(
select date,total as C2 from cte where type='c2'),c3 as(
select date,total as C3 from cte where type='c3')
select cte.date,c1.C1,c2.C2,c3.C3 from cte join c1 on cte.date=c1.date join c2 on cte.date=c2.date
join c3 on cte.date=c3.date join ext on cte.date=ext.date
我的预期输出:
我尝试了此查询,但没有成功
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
class Foo {
@XmlElement(name = "ID")
private long id;
@XmlElement(name = "TYPE",type = Character.class)
private char type;
}
这行得通,但每一行都添加了3次。
解决方法
使用条件聚合:
select
date(start_time) start_date,countif(cat = 'c1') c1,countif(cat = 'c2') c2,countif(cat = 'c3') c3
from mytable
where start_time >='2020-09-09'
group by date(start_time)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。