【问题标题】:SQL: Get results of time series of days backwardsSQL:向后获取时间序列的结果
【发布时间】:2019-10-16 07:01:14
【问题描述】:

给出的是每天保存的实体的库存值表。我想得到:

给出今天缺货(数量 = 0)且前一天没有缺货的所有实体。

这是前天与前天相比的前天。以此类推。

我的方法有效,但我必须手动在 WHERE 条件下创建部件。

2019年全年查询怎么样?

#standardSQL
WITH
  TableStockDaily AS (
  SELECT TIMESTAMP('2019-10-10 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-10 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-10 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-11 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-11 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-11 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-12 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-12 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-12 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-13 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-13 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-13 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-14 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-14 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-14 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-15 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-15 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-15 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 0 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-16 00:00:00 UTC') AS ExportDate, 1001 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-16 00:00:00 UTC') AS ExportDate, 1002 AS EntityId, 1 as Qty UNION ALL
  SELECT TIMESTAMP('2019-10-16 00:00:00 UTC') AS ExportDate, 1003 AS EntityId, 1 as Qty
  )
SELECT
  sd1.ExportDate AS DateOutOfStock,
  sd2.ExportDate AS DateNotOutOfStock,
  sd1.EntityId AS EntityId,
  sd1.Qty AS Qty1,
  sd2.Qty AS Qty2
FROM
  TableStockDaily sd1
LEFT JOIN
  TableStockDaily sd2
ON
  sd1.EntityId = sd2.EntityId
WHERE
  sd1.Qty = 0
  AND sd2.Qty > 0
  AND sd1.ExportDate > sd2.ExportDate
  AND
  (
    ( 
      DATE(sd1.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -1 DAY)
      AND DATE(sd1.ExportDate) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -2 DAY)
      AND DATE(sd2.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -2 DAY)
      AND DATE(sd2.ExportDate) > DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -3 DAY)
    )
    OR
    ( 
      DATE(sd1.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -2 DAY)
      AND DATE(sd1.ExportDate) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -3 DAY)
      AND DATE(sd2.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -3 DAY)
      AND DATE(sd2.ExportDate) > DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -4 DAY)
    )
    OR
    ( 
      DATE(sd1.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -3 DAY)
      AND DATE(sd1.ExportDate) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -4 DAY)
      AND DATE(sd2.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -4 DAY)
      AND DATE(sd2.ExportDate) > DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -5 DAY)
    )
    OR
    ( 
      DATE(sd1.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -4 DAY)
      AND DATE(sd1.ExportDate) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -5 DAY)
      AND DATE(sd2.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -5 DAY)
      AND DATE(sd2.ExportDate) > DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -6 DAY)
    )
    OR
    ( 
      DATE(sd1.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -5 DAY)
      AND DATE(sd1.ExportDate) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -6 DAY)
      AND DATE(sd2.ExportDate) <= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -6 DAY)
      AND DATE(sd2.ExportDate) > DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -7 DAY)
    )
  )

【问题讨论】:

    标签: sql arrays datetime google-bigquery unnest


    【解决方案1】:

    我认为您正在寻找LAG()。此窗口函数可用于根据排序标准(此处为 ExportDate)恢复一组记录(此处为共享相同 EntityID 的记录)中给定列的值:

    SELECT *
    FROM (
        SELECT
            EntityId,
            ExportDate AS DateOutOfStock,
            Qty AS QtyOutOfStock,
            LAG(ExportDate) OVER(PARTITION BY EntityId ORDER BY ExportDate) AS DateNotOutOfStock,
            LAG(Qty) OVER(PARTITION BY EntityId ORDER BY ExportDate) AS QtyNotOutOfStock
        FROM
          TableStockDaily sd1
    ) x
    WHERE QtyOutOfStock = 0 AND QtyNotOutOfStock > 0
    

    【讨论】:

    • 太棒了。谢谢。有用。我检查了不同的日期,结果是一样的。
    猜你喜欢
    • 2022-11-07
    • 2021-12-26
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2017-04-23
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多