Bigquery按滚动天数分组

时间:2016-04-12 09:47:01

标签: date group-by google-bigquery

这是我尝试按周和上周分组。我理解错误,但是当我按天分组时,最小值变为每行的最小值,因此不是最小值。

必须有一种简单的方法可以在Bigquery中将日子分组。

select
    n_rtb_impressions,
    if(dayy between min(dayy) and (min(dayy) + 6) ,1, 0) as D
from
    (SELECT
    ((dayofyear(datetime))) as dayy,
        sum(IF( c_priority IN (19,20,21), IFNULL(s_impressions,0)-IFNULL(hybrid_p_back,0), 0)) AS n_rtb_impressions,
        FROM
        TABLE_DATE_RANGE(hourly_stats.v1_,DATE_ADD(CURRENT_DATE(),-14,"day"),DATE_ADD(CURRENT_DATE(),-1,"day"))
        group by dayy)

在下面的帮助下进行编辑然后一些游戏我设法将其扩展为多个维度然后过滤掉结果,使得每组维度只有两行,一行为1-7天,一行为8-14天。然后我使用NTH来旋转数据,因此分组现在是维度。我会把它放在别人身上:

select
server,
a_name,
w_name,
rtb_name,

NTH(1, last_7_days_n_rtb_impressions_1) This_week, NTH(2, last_7_days_n_rtb_impressions_1) Last_Week,

from(
    select
        [date],
        dw,
        server,
        a_name,
        w_name,
        rtb_name,
        last_7_days_n_rtb_impressions_1
    from(
        SELECT
            [date],
            dw,
            SUM(n_rtb_impressions) OVER(ORDER BY rtb_name, server, a_name, w_name,[date]
              ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS last_7_days_n_rtb_impressions_1,
            server,
            a_name,
            w_name,
            rtb_name,
        FROM (
          SELECT
              server,
              a_name,
              w_name,
              rtb_name,
              dayofweek(datetime) as dw,
              DATE(datetime) AS [date],
              SUM(IF( c_priority IN (19,20,21), IFNULL(s_impressions,0)-IFNULL(hybrid_p_back,0), 0)) AS n_rtb_impressions,

          FROM TABLE_DATE_RANGE(hourly_stats.v1_,DATE_ADD(CURRENT_DATE(),-14,"day"),
               DATE_ADD(CURRENT_DATE(),-1,"day"))

          GROUP BY [date],dw,server,
                 a_name,
                 w_name,
                 rtb_name,
          order by rtb_name,server, a_name,w_name, [date])

          order by rtb_name,server, a_name,w_name, )

          where dw = dayofweek(DATE_ADD(CURRENT_DATE(),-1,"day"))

          order by rtb_name,server, a_name,w_name, [date])

          group by
          server,
          a_name,
          w_name,
          rtb_name,

1 个答案:

答案 0 :(得分:1)

尽可能接近你的榜样

last_7_days_n_rtb_impressions_1 - 如果您在一年中的日子没有任何差距,则会产生正确的输出。 它将在新的一年中继续计算7天

last_7_days_n_rtb_impressions_2 - 即使一年中的天数存在差距,也能产生正确的输出。它从0年开始。当然,这可以进一步调整以反映您的需求

希望这对你来说是一个好的开始

有关详细信息,请参阅Window functionswindow-frame-clause部分

SELECT 
  [date], dayy, n_rtb_impressions, 
  SUM(n_rtb_impressions) OVER(ORDER BY [date] 
      ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS last_7_days_n_rtb_impressions_1,
  SUM(n_rtb_impressions) OVER(ORDER BY [date] 
      RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS last_7_days_n_rtb_impressions_2  
FROM (
  SELECT
    DAYOFYEAR(datetime) AS dayy,
    DATE(datetime) AS [date],
    SUM(IF( c_priority IN (19,20,21), IFNULL(s_impressions,0)-IFNULL(hybrid_p_back,0), 0)) AS n_rtb_impressions,
  FROM TABLE_DATE_RANGE(hourly_stats.v1_,DATE_ADD(CURRENT_DATE(),-14,"day"),
        DATE_ADD(CURRENT_DATE(),-1,"day"))
  GROUP BY dayy, [date]
)
ORDER BY 1