sql BIGQUERY通过时间戳获得最新结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql BIGQUERY通过时间戳获得最新结果相关的知识,希望对你有一定的参考价值。

/**
 * Format Currect Data
 *
 * @param {string} inJson
 * @return {string} outJson
 */
function transform(inJson) {
  var obj = JSON.parse(inJson);

  // Limitação do scopo somente para b2w, skyhun e preço
  if( ( obj.target !== 'b2w' || obj.target !== 'skyhub' ) && new_obj.action !== 'sent_product_price_to_mktp' ) {
    return;
  }

  var new_obj = {
    account_id: obj.account_id,
    sku: obj.external_id,
    action: obj.target === 'skyhub' ? 'sent_product_price_to_mktp' : obj.action,
    target: obj.target,
    created_at: obj.timestamp
  }

  return JSON.stringify(new_obj);
}
{
  "account_id": "account_id",
  "sku": "sku",
  "action": "price",
  "target": "b2w",
  "created_at": "2018-09-27T11:28:27-03:00"
}
# Queries

Esperando atualização

    SELECT account_id, sku, action, target, created_at
    FROM (
     SELECT account_id, sku, action, target, created_at
          , RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
     FROM `lunar-sled-217521.skyhub.timeline_metrics`
    )
    WHERE rank=1 and target = "skyhub"

Last B2w Update

    SELECT * FROM (
      SELECT
        account_id, sku, action, target, created_at,
        RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
      FROM (
        SELECT
          t1.account_id, t1.sku, t1.action, t1.target, t1.created_at
        FROM
          `lunar-sled-217521.skyhub.timeline_metrics` as t1
        JOIN
          `lunar-sled-217521.skyhub.timeline_metrics_waiting_update` as t2
        ON
          t1.account_id = t2.account_id and t1.sku = t2.sku and t1.action = t2.action
        WHERE
          t1.target = 'b2w'
      )
    ) WHERE rank=1

LIST all first updates wating to go to b2w

    SELECT
      account_id, sku, action, target, created_at
    FROM (
      SELECT
        t3.account_id, t3.sku, t3.action, t3.target, t3.created_at,
        RANK() OVER(PARTITION BY t3.account_id, t3.sku, t3.action ORDER BY t3.created_at ASC) rank
      FROM
        `lunar-sled-217521.skyhub.timeline_metrics` t3
      JOIN
        `lunar-sled-217521.skyhub.timeline_metrics_last_b2w_updates` as t4
      ON 
        t3.account_id = t4.account_id and t3.sku = t4.sku and t3.action = t4.action
      WHERE
        t3.target = 'skyhub' and
        t3.created_at > t4.created_at
    )
    WHERE
      rank=1

LIST all pending updates from first update

    SELECT
      account_id, sku, action, target, created_at,
      TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), created_at, SECOND) as seconds_opening
    FROM (
      SELECT 
        account_id, sku, action, target, created_at,
        RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at ASC) rank
      FROM (
        SELECT
          account_id, sku, action, target, created_at
        FROM
          `lunar-sled-217521.skyhub.timeline_metrics_waiting_update_first_insert`
        UNION ALL
        SELECT
          account_id, sku, action, target, created_at   
        FROM `lunar-sled-217521.skyhub.timeline_metrics_waiting_update`
      )
    ) WHERE rank = 1
    
INSERT HELPER

    INSERT INTO `lunar-sled-217521.skyhub.timeline_metrics`
      (account_id, sku, action, target, created_at, inserted_at)
    VALUES ("account_id_2", "sku_2", "price", "skyhub", CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())
SELECT
  account_id,
  sku,
  action,
  target,
  created_at,
  TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), created_at, SECOND) as seconds_opening
FROM (
 SELECT
  account_id,
  sku,
  action,
  target,
  created_at,
  RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
 FROM `table_name`
)
WHERE rank=1 and target = "skyhub"

以上是关于sql BIGQUERY通过时间戳获得最新结果的主要内容,如果未能解决你的问题,请参考以下文章

如何在 BigQuery 标准 SQL 中将时间戳转换为秒

在 BigQuery SQL for Data Studio 中格式化 Firestore 时间戳

在 BigQuery SQL for Data Studio 中格式化 Firestore 时间戳

努力从 BigQuery SQL 中的时间戳字段中提取特定月份的 DATE

通过在 ms-sql 中的表中添加一个名为 recordversion 的时间戳列,我可以获得啥?

BigQuery:如何执行滚动时间戳窗口组计数,每天产生行