sql—选择第一个具有最小日期的非空行(大查询)

qncylg1j  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(390)

我要选择第一个具有最小日期的非空行。当这个条件满足时,我想用一个case,然后用1或者0。

所以更像是,行不是,日期是最小日期,然后是1,否则是0。我只需要选择一行。

yzuktlbb

yzuktlbb1#

另一个选项(对于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
gjmwrych

gjmwrych2#

考虑:

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

相关问题