连续数据并在无数据时添加行 BIGQUERY

Posted

技术标签:

【中文标题】连续数据并在无数据时添加行 BIGQUERY【英文标题】:Continuous data and add row when no data BIGQUERY 【发布时间】:2021-10-23 05:55:47 【问题描述】:

请原谅我的英语

我有这样的数据 [RAW Data][1]g 我正在尝试做两件事

    每个“数据”都有 a、b、c、d,所以当没有 a、b、c、d 时,我们将添加它并添加值 0 尝试创建新列,总和连续但计算每个代码 0

如果您能提供帮助,我将不胜感激;谢谢你们。

我尝试了很多我能找到的代码。但只有连续的我可以,但不幸的是它是全局连续的,不是每个代码连续的 2

SELECT tanggal,code, code2, data,

  SUM(data) OVER (ORDER BY DateT, Code 
                      ROWS BETWEEN UNBOUNDED PRECEDING 
                      AND CURRENT ROW) AS ContinousData



from`market-maker-328312.MarketMakerV2.MM16

docs of brief

Final expectation

RAAWg

【问题讨论】:

请将数据和代码作为文本而不是图像发布。 【参考方案1】:

你是不是想在下面做这样的事情

with sample as
(

select date ('2021-01-01') as tangal,'Code 1' as code1 ,'Code a' as code2, 100 as data  union all
select date ('2021-01-01') as tangal,'Code 1' as code1 ,'Code b' as code2, 101 as data  union all
select date ('2021-01-01') as tangal,'Code 1' as code1 ,'Code c' as code2, 102 as data  union all
select date ('2021-01-01') as tangal,'Code 1' as code1 ,'Code d' as code2, 103 as data  union all
select date ('2021-01-01') as tangal,'Code 2' as code1 ,'Code a' as code2, 198 as data  union all
select date ('2021-01-01') as tangal,'Code 2' as code1 ,'Code b' as code2, 10 as data  union all
select date ('2021-01-01') as tangal,'Code 2' as code1 ,'Code c' as code2, 101 as data  union all

select date ('2021-01-02') as tangal,'Code 1' as code1 ,'Code a' as code2, 102 as data  union all
select date ('2021-01-02') as tangal,'Code 1' as code1 ,'Code b' as code2, 109 as data  union all
select date ('2021-01-02') as tangal,'Code 1' as code1 ,'Code d' as code2, 108 as data  union all
select date ('2021-01-02') as tangal,'Code 2' as code1 ,'Code a' as code2, 105 as data  union all
select date ('2021-01-02') as tangal,'Code 2' as code1 ,'Code a' as code2, 10 as data  
)
,lkp_data as
(
    select  distinct sample.tangal ,sample.code1 ,code
    from sample, (
                    select 'Code a' as code UNION all
                    select 'Code b' as code UNION all
                    select 'Code c' as code UNION all
                    select 'Code d' as code
    )
)

select  ld.*,
        if(sp.data is null,0,sp.data) as data ,
        sum(if(sp.data is null,0,sp.data))over(partition by ld.tangal,ld.code1 order by ld.code) cum_sum
from lkp_data ld
left join sample sp on  
ld.tangal = sp.tangal and ld.code1 = sp.code1 and ld.code = sp.code2

输出

【讨论】:

谢谢巴特拉先生。所以显然这是我安排这个计划的错误。但是您的代码向我展示了对连续分区求和的想法。非常感谢

以上是关于连续数据并在无数据时添加行 BIGQUERY的主要内容,如果未能解决你的问题,请参考以下文章

向 UItableview 添加行并在 Viewcontroller 之间传递数据

如何将新列动态添加到 bigquery 中已存在的表..?

Bigquery 返回内部错误 503:23 列 X 600,00 行

Json 连续拟合数字并在每个 1-9 数字前添加一个 0

如何过滤 bigquery 的重复表行数据以删除重复行?

如何添加双击事件并在 Angular KendoUI 中传递选定的行数据