获取BigQuery中最新行的属性?

时间:2017-04-12 11:26:48

标签: sql google-bigquery

我在BigQuery工作。我有一张表t1,其中包含地址,邮政编码,价格和日期字段。我想通过地址和邮政编码对此进行分组,找到每个地址的最新行的价格。

我如何在BigQuery中执行此操作?我知道如何获取地址,邮政编码和最近的日期:

SELECT
  ADDRESS, POSTCODE, MAX(DATE)
FROM
  [mytable]
GROUP BY
  ADDRESS,
  POSTCODE

但我不知道如何获得与这些字段匹配的这些行的价格。这是我最好的猜测,它会产生结果 - 这是正确的吗?

SELECT 
  t1.address, t1.postcode, t1.date, t2.price
FROM [mytable] t2
JOIN 
(SELECT
  ADDRESS, POSTCODE, MAX(DATE) AS date
FROM
  [mytable]
GROUP BY
  ADDRESS,
  POSTCODE) t1
ON t1.address=t2.address 
   AND t1.postcode=t2.postcode 
   AND t1.date=t2.date

在我看来,这似乎应该有效,但有些similar questions的解决方案要复杂得多。

2 个答案:

答案 0 :(得分:3)

只需使用row_number()

SELECT t.*
FROM (SELECT t.*,
             ROW_NUMBER() OVER (PARTITION BY ADDRESS, POSTCODE
                                ORDER BY DATE DESC
                               ) as seqnum
      FROM [mytable] t
     ) t
WHERE seqnum = 1;

这不是聚合查询。您希望过滤行以获取最新值。

答案 1 :(得分:1)

在下面尝试BigQuery Standard SQL

#standardSQL
SELECT row.* FROM (
  SELECT ARRAY_AGG(t ORDER BY date DESC LIMIT 1)[OFFSET(0)] AS row
  FROM `yourTable` AS t
  GROUP BY address, postcode
)  

您可以使用虚拟数据播放/测试它,如下所示

#standardSQL
WITH yourTable AS (
  SELECT 'address_1' AS address, 'postcode_1' AS postcode, '2017-01-01' AS date, 1 AS price UNION ALL
  SELECT 'address_1', 'postcode_1', '2017-01-02', 2 UNION ALL
  SELECT 'address_1', 'postcode_1', '2017-01-03', 3 UNION ALL
  SELECT 'address_1', 'postcode_1', '2017-01-04', 4 UNION ALL
  SELECT 'address_2', 'postcode_2', '2017-01-01', 5 UNION ALL
  SELECT 'address_3', 'postcode_1', '2017-01-01', 6 UNION ALL
  SELECT 'address_3', 'postcode_1', '2017-01-02', 7 UNION ALL
  SELECT 'address_3', 'postcode_1', '2017-01-03', 8 
)
SELECT row.* FROM (
  SELECT ARRAY_AGG(t ORDER BY date DESC LIMIT 1)[OFFSET(0)] AS row
  FROM `yourTable` AS t
  GROUP BY address, postcode
)