BigQuery:通过运行7d / 14d / 30d等桶来聚合度量标准

时间:2017-04-25 21:55:40

标签: google-bigquery

任何人都知道一个简短的BQ查询(#standardsql),通过运行7d / 14d / 30d等来聚合指标(会话/ PV /用户等)。例如,

4月16日至22日:300K会议 4月9日至15日:330K会议 4月2日至8日:270K会议

OR,将GA的日期字段(STRING)转换为days_since_epoch的开箱即用功能

我写了一个查询,但它非常复杂 - 使用REGEXP_EXTRACT()手动提取为YYY,MM,DD组件 - 使用UNIX_DATE转换为days_since_epoch - 除以'7'将每行分组为每周观察 - 使用GROUP BY汇总&报告

任何简化此用例的指针都会非常值得注意!

干杯!

1 个答案:

答案 0 :(得分:4)

  

任何人都知道通过运行7d / 14d / 30d等桶来汇总指标(会话/ PV /用户等)的简短,简洁的BQ查询(#standardsql)

参见下面的BigQuery Standard SQL的7d示例 - 您可以将此逻辑应用于您拥有的任何数据,并希望进行轻微调整

  
    class GeneralChatroom: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UITextViewDelegate {


//Get Data of current cell that has been tapped
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

            //Eliminate highlight after cell is tapped
            tableView.deselectRow(at: indexPath as IndexPath, animated: true)

            let userIdentityString : String = generalRoomDataArr[indexPath.row].cellUserId

            let destinationUID = userCellTapped()

            destinationUID.programVar = userIdentityString

            destinationUID.performSegue(withIdentifier: "profileTapped", sender: self)


        }

}


    import UIKit

    class userCellTapped: UIViewController {

        var programVar : String!


        override func viewDidLoad() {
            super.viewDidLoad()


            print("testbles", programVar)


        }


    }

它产生以下输出,可以作为进一步定制到所需布局的起点

#standardSQL
WITH data AS (
  SELECT 
    day, CAST(1000 * RAND() AS INT64) AS events
  FROM UNNEST(GENERATE_DATE_ARRAY('2017-01-01', '2017-04-25')) AS day
)
SELECT 
  FORMAT_DATE('%U', day) as week,
  FORMAT_DATE('%Y, %B %d', MIN(day)) AS start, 
  FORMAT_DATE('%Y, %B %d', MAX(day)) AS finish,
  SUM(events) AS events
FROM data 
GROUP BY week
ORDER BY week 
  

OR,将GA的日期字段(STRING)转换为days_since_epoch的开箱即用功能

要将STRING表达日期转换为DATE类型的日期 - 请使用PARSE_DATE,如下例

week    start               finish              events   
01      2017, January 01    2017, January 07    3699     
02      2017, January 08    2017, January 14    4008     
03      2017, January 15    2017, January 21    3726    
...     ...                 ...                 ...

结果是

#standardSQL
SELECT PARSE_DATE('%Y%m%d', '20170425') AS date   
  

最后,下面是运行7d / 14d / 30d等桶的示例/模板

date     
2017-04-25   

输出如下

#standardSQL
WITH data AS (
  SELECT 
    DAY, CAST(1000 * RAND() AS INT64) AS events
  FROM UNNEST(GENERATE_DATE_ARRAY('2017-01-01', '2017-04-25')) AS DAY
)
SELECT 
  DAY,
  SUM(CASE WHEN period = 7  THEN events END) AS days_07,
  SUM(CASE WHEN period = 14 THEN events END) AS days_14,
  SUM(CASE WHEN period = 30 THEN events END) AS days_30
FROM (
  SELECT
    dates.day AS DAY,
    periods.period AS period,
    SUM(events) AS events
  FROM data AS activity
  CROSS JOIN (SELECT DAY FROM data GROUP BY DAY) AS dates
  CROSS JOIN (SELECT period FROM (SELECT 7 AS period UNION ALL 
                SELECT 14 AS period UNION ALL SELECT 30 AS period)) AS periods
  WHERE dates.day >= activity.day 
  AND CAST(DATE_DIFF(dates.day, activity.day, DAY) / periods.period AS INT64) = 0
  GROUP BY 1,2
)
GROUP BY DAY
ORDER BY DAY DESC