如何直接在BigQuery界面中计算timeOnPage?

时间:2015-09-25 16:48:30

标签: google-bigquery

即使BigQuery直接向我们提供了点击(页面和事件),但它没有每页花费的时间(因为它是两次连续点击之间的差异,因为页面路径已经改变)以便重新计算一组页面花费的全球时间。

我知道我可以在我的数据仓库中使用我的大查询表的简单副本(页面上的过滤器和a.hit_number = b.hit_number-1上的连接)来完成它但我希望直接计算它在BigQuery中。

3 个答案:

答案 0 :(得分:1)

是肯定的。可以使用bigquery的窗口函数https://cloud.google.com/bigquery/query-reference#windowfunctions

轻松完成

答案 1 :(得分:1)

希望以下简化示例将有所帮助

     void FixedUpdate ()
        {
            isGrounded = GroundedCheck ();
            if (gm.gameState == GameManager.GameState.playing) {

                foreach (GameObject g in gm.deadObjects) {
                    if (g.name == "P13") {
                        g.GetComponent<Mygroup> ().targetName = "P12";
                    } else if (g.name == "P12") {
                        g.GetComponent<Mygroup> ().targetName = "P11";
                    } else if (g.name == "P11") {
                        g.GetComponent<Mygroup> ().targetName = "P10";
                    } else if (g.name == "P10") {
                        g.GetComponent<Mygroup> ().targetName = "P9";
                    } else if (g.name == "P9") {
                        g.GetComponent<Mygroup> ().targetName = "P8";
                    } else if (g.name == "P8") {
                        g.GetComponent<Mygroup> ().targetName = "P7";
                    } else if (g.name == "P7") {
                        g.GetComponent<Mygroup> ().targetName = "P6";
                    } else if (g.name == "P6") {
                        g.GetComponent<Mygroup> ().targetName = "P5";
                    } else if (g.name == "P5") {
                        g.GetComponent<Mygroup> ().targetName = "P4";
                    } else if (g.name == "P4") {
                        g.GetComponent<Mygroup> ().targetName = "P3";
                    } else if (g.name == "P3") {
                        g.GetComponent<Mygroup> ().targetName = "P2";
                    } else if (g.name == "P2") {
                        g.GetComponent<Mygroup> ().targetName = "P1";
                    } else if (g.name == "P1") {
                        g.GetComponent<Mygroup> ().targetName = "P0";
                    } else if (g.name == "P0") {
                        g.GetComponent<Mygroup> ().targetName = null;
                    }
                }
                if (this.gameObject.name == "P0") {
                    targetName = "";
                } else if (this.gameObject.name == "P1")
                    targetName = "P0";
                else if (this.gameObject.name == "P2")
                    targetName = "P1";
                else if (this.gameObject.name == "P3")
                    targetName = "P2";
                else if (this.gameObject.name == "P4")
                    targetName = "P3";
                else if (this.gameObject.name == "P5")
                    targetName = "P4";
                else if (this.gameObject.name == "P6")
                    targetName = "P5";
                else if (this.gameObject.name == "P7")
                    targetName = "P6";
                else if (this.gameObject.name == "P8")
                    targetName = "P7";
                else if (this.gameObject.name == "P9")
                    targetName = "P8";
                else if (this.gameObject.name == "P10")
                    targetName = "P9";
                else if (this.gameObject.name == "P11")
                    targetName = "P10";
                else if (this.gameObject.name == "P12")
                    targetName = "P11";

                target = GameObject.Find (targetName);

                if (isGrounded) {
                    jumping = false;
                    doubleJump = false;
                    jumpAnim = false;

    if (this.gameObject.name == "P0" && isGrounded && !doubleJump && !jumping) {
                if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
                        jumping = true;
                        rid.velocity = new Vector3 (0, 50f * jump, 0);
                        doubleJump = true;
                    }
                }
    if (this.gameObject.name == "P0" && !isGrounded && doubleJump && jumping) {
        if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
                        rid.velocity = new Vector3 (0, 60f * jump, 0);
                        doubleJump = false;
                    }
                }

if (target != null && this.gameObject.name != "P0" && isGrounded && target.GetComponent<Mygroup> ().doubleJump && !target.GetComponent<Mygroup> ().isGrounded) {
                    StartCoroutine (Jump ());
                }

    if (target != null && this.gameObject.name != "P0" && !isGrounded && doubleJump && !target.GetComponent<Mygroup> ().isGrounded && !target.GetComponent<Mygroup> ().doubleJump) {
                    StartCoroutine (JumpDouble ());
                }
        }
        IEnumerator Jump ()
        {
            yield return new WaitForSeconds (time);

            if (gameObject.name == "P1")
                rid.velocity = new Vector3 (0, 49.6f * jump, 0);
            if (gameObject.name == "P2")
                rid.velocity = new Vector3 (0, 49.4f * jump, 0);
            if (gameObject.name == "P3")
                rid.velocity = new Vector3 (0, 49.2f * jump, 0);
            if (gameObject.name == "P4")
                rid.velocity = new Vector3 (0, 49f * jump, 0);
            if (gameObject.name == "P5")
                rid.velocity = new Vector3 (0, 48.8f * jump, 0);
            if (gameObject.name == "P6")
                rid.velocity = new Vector3 (0, 48.6f * jump, 0);
            if (gameObject.name == "P7")
                rid.velocity = new Vector3 (0, 48.4f * jump, 0);
            if (gameObject.name == "P8")
                rid.velocity = new Vector3 (0, 48.2f * jump, 0);
            if (gameObject.name == "P9")
                rid.velocity = new Vector3 (0, 48f * jump, 0);
            if (gameObject.name == "P10")
                rid.velocity = new Vector3 (0, 47.8f * jump, 0);
            if (gameObject.name == "P11")
                rid.velocity = new Vector3 (0, 47.6f * jump, 0);
            if (gameObject.name == "P12")
                rid.velocity = new Vector3 (0, 47.4f * jump, 0);

            jumping = true;
            doubleJump = true;

        }
        IEnumerator JumpDouble ()
        {
            yield return new WaitForSeconds (time);
            if (doubleJump) {

                if (gameObject.name == "P1")
                    rid.velocity = new Vector3 (0, 59.8f * jump, 0);
                if (gameObject.name == "P2")
                    rid.velocity = new Vector3 (0, 59.6f * jump, 0);
                if (gameObject.name == "P3")
                    rid.velocity = new Vector3 (0, 59.4f * jump, 0);
                if (gameObject.name == "P4")
                    rid.velocity = new Vector3 (0, 59.2f * jump, 0);
                if (gameObject.name == "P5")
                    rid.velocity = new Vector3 (0, 59f * jump, 0);
                if (gameObject.name == "P6")
                    rid.velocity = new Vector3 (0, 58.8f * jump, 0);
                if (gameObject.name == "P7")
                    rid.velocity = new Vector3 (0, 58.6f * jump, 0);
                if (gameObject.name == "P8")
                    rid.velocity = new Vector3 (0, 58.4f * jump, 0);
                if (gameObject.name == "P9")
                    rid.velocity = new Vector3 (0, 58.2f * jump, 0);
                if (gameObject.name == "P10")
                    rid.velocity = new Vector3 (0, 58f * jump, 0);
                if (gameObject.name == "P11")
                    rid.velocity = new Vector3 (0, 57.8f * jump, 0);
                if (gameObject.name == "P12")
                    rid.velocity = new Vector3 (0, 57.6f * jump, 0);
                doubleJump = false;
                jumping = false;
            }
        }

预期产出如下:

select
  user,
  page,
  ts as start,
  CASE WHEN finish = 0 THEN NULL 
    ELSE finish - ts END as duration
from (
  select 
    user,
    page,
    ts,
    LAG(ts, 1, 0) OVER(PARTITION BY user ORDER BY ts DESC) as finish
  from ( -- below is sample data 
    select user, page, type, ts from 
        (select 1 as user, 1 as page, 1 as ts, 'page' as type),
        (select 1 as user, 2 as page, 3 as ts, 'page' as type),
        (select 1 as user, 3 as page, 6 as ts, 'page' as type),
        (select 1 as user, 4 as page, 9 as ts, 'other' as type),
        (select 1 as user, 5 as page, 12 as ts, 'other' as type),
        (select 2 as user, 1 as page, 1 as ts, 'page' as type),
        (select 2 as user, 2 as page, 2 as ts, 'page' as type),
        (select 2 as user, 3 as page, 10 as ts, 'page' as type)
  )
  where type = 'page'
)
order by user, ts

null持续时间适用于最后一页

答案 2 :(得分:0)

谢谢米哈伊尔,但它仍然很模糊。

是这样的吗?

myDeclarer

如何指定: - 在分区中我正在寻找hits.hitNumber - 1为hits.type =&#39; PAGE&#39;只要 ? - 如何加入另一个变量(visitNumber,fullvisitorId,...)?

由于