【问题标题】:select first non-null row with minimum date (Big Query)选择具有最短日期的第一个非空行(大查询)
【发布时间】:2020-10-13 18:28:15
【问题描述】:

我想选择第一个具有最短日期的非空行。我想在满足该条件时使用 CASE,然后使用 1 ELSE 0。

所以更像是 CASE WHEN WHEN row IS NOT 并且 DATE 是最小 DATE 然后 1 ELSE 0。我只需要选择一行。

【问题讨论】:

    标签: sql date select google-bigquery window-functions


    【解决方案1】:

    另一个选项(适用于 BigQuery 标准 SQL)

    #standardSQL
    SELECT *, 0 AS marker FROM `project.dataset.table` WHERE item_count IS NULL
    UNION ALL
    SELECT *, IF(1 = ROW_NUMBER() OVER(PARTITION BY user ORDER BY date), 1, 0)
    FROM `project.dataset.table` WHERE NOT item_count IS NULL
    ORDER BY user, date      
    

    【讨论】:

      【解决方案2】:

      考虑:

      select 
          t.*
          case when date = min(case when itemcount is not null then date end) over(partition by user order by date)
              then 1
              else 0
          end as marker
      from mytable t
      

      我不确定 BigQuery 是否支持 minif() 作为窗口函数:

      select 
          t.*
          case when date = minif(date, itemcount is not null) over(partition by user order by date)
              then 1
              else 0
          end as marker
      from mytable
      

      【讨论】:

      • 第一个具有 MIN 功能的工作。 MINIF 现在不适用于 Google Bigquery
      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2017-10-12
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多