列向量和派生位向量的加权和-版本2

时间:2019-07-02 20:26:20

标签: google-bigquery standard-sql

我们有一个报价表和两个买家的规模。大小为 s 的竞标价格 p 表示买方愿意以价格 p 购买数量为 s 的产品。我们有一个表,其中包含几列(例如时间戳记,有效性标志)以及以下四列:

  • 两个买家 pA pB 的出价。
  • 出价大小, sA sB

我们的工作是在表中添加一个新的最佳尺寸列( bS ),以最佳价格返回尺寸。如果两个买家的价格相同,则 bS 等于 sA + sB ,否则,我们需要采用提供更高价格的买家的出价大小。 / p>

下面是带有所需输出的示例表(忽略既不是价格也不是大小的列)。 enter image description here

该问题的简单解决方案:

SELECT *,
  CASE
    WHEN pA = pB THEN sA + sB
    WHEN pA > pB THEN sA
    ELSE sB
  END AS bS
FROM t

现在让我们将问题推广到四个买家。一个标准的SQL解决方案是

WITH t_ext AS (
SELECT *, GREATEST(pA, pB, pC, pD) as bP
FROM `t` 
)
SELECT *, (sA * CAST(pA = bP AS INT64) + 
           sB * CAST(pB = bP AS INT64) + 
           sC * CAST(pC = bP AS INT64) +
           sD * CAST(pD = bP AS INT64)) 
AS bS FROM t_ext

问题:

是否存在简化的查询

  • 使用函数SUM代替手动添加四个项目
  • 避免重复投射?

请注意,我们无法按索引标识价格和尺寸列,而只能按名称标识。否则,我们可以使用

提出的解决方案

Weighted sum of a column vector and a derived bit vector

顺便说一句。我针对这个问题写了blog post,重点介绍了Python和Q中的解决方案,我想知道标准sql中最好的解决方案的样子。

1 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

  

请注意,我们无法按索引标识价格和尺寸列,而只能按名称标识

#standardSQL
WITH t_ext AS (
  SELECT * EXCEPT(arr), 
    ARRAY(SELECT CAST(val AS INT64) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET < ARRAY_LENGTH(arr) / 2) AS prices,
    ARRAY(SELECT CAST(val AS INT64) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET >= ARRAY_LENGTH(arr) / 2) AS sizes,
    (SELECT MAX(CAST(val AS INT64)) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET < ARRAY_LENGTH(arr) / 2) AS bestPrice
  FROM (
    SELECT *, REGEXP_EXTRACT_ALL(TO_JSON_STRING(T), r'(?:"(?:pA|pB|pC|pD|sA|sB|sC|sD)"):(\d+)') AS arr
    FROM `project.dataset.table` t
  )
)
SELECT * EXCEPT(prices, sizes), 
  (SELECT SUM(size)
    FROM UNNEST(prices) price WITH OFFSET
    JOIN UNNEST(sizes) size WITH OFFSET
    USING(OFFSET) 
    WHERE price = bestPrice
  ) AS bS
FROM t_ext

您将看到-唯一需要提供的是价格和尺寸列名称列表,如以下示例所示

pA|pB|pC|pD|sA|sB|sC|sD    

如果要应用于以下虚拟数据

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'a' id, 1 pA, 2 pB, 3 pC, 4 pD, 'x' extra_col1, 1 sA, 1 sB, 1 sC, 5 sD UNION ALL
  SELECT 'b', 1, 4, 2, 4, 'y', 1, 6, 1, 5 UNION ALL
  SELECT 'c', 5, 4, 2, 1, 'z', 7, 1, 1, 1
), t_ext AS (
  SELECT * EXCEPT(arr), 
    ARRAY(SELECT CAST(val AS INT64) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET < ARRAY_LENGTH(arr) / 2) AS prices,
    ARRAY(SELECT CAST(val AS INT64) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET >= ARRAY_LENGTH(arr) / 2) AS sizes,
    (SELECT MAX(CAST(val AS INT64)) FROM UNNEST(arr) val WITH OFFSET WHERE OFFSET < ARRAY_LENGTH(arr) / 2) AS bestPrice
  FROM (
    SELECT *, REGEXP_EXTRACT_ALL(TO_JSON_STRING(T), r'(?:"(?:pA|pB|pC|pD|sA|sB|sC|sD)"):(\d+)') AS arr
    FROM `project.dataset.table` t
  )
)
SELECT * EXCEPT(prices, sizes), 
  (SELECT SUM(size)
    FROM UNNEST(prices) price WITH OFFSET
    JOIN UNNEST(sizes) size WITH OFFSET
    USING(OFFSET) 
    WHERE price = bestPrice
  ) AS bS
FROM t_ext

结果是

Row id  pA  pB  pC  pD  extra_col1  sA  sB  sC  sD  bestPrice   bS   
1   a   1   2   3   4   x           1   1   1   5   4           5    
2   b   1   4   2   4   y           1   6   1   5   4           11   
3   c   5   4   2   1   z           7   1   1   1   5           7      

希望,这就是您要寻找的