Sql 选择第一个具有最小日期的非空行(大查询)
我想选择第一个具有最小日期的非空行。我想在满足该条件时使用CASE,然后使用1或0 所以更像是行不是,日期是最小日期,然后是1或0。我只需要选择一行。考虑:Sql 选择第一个具有最小日期的非空行(大查询),sql,date,select,google-bigquery,window-functions,Sql,Date,Select,Google Bigquery,Window Functions,我想选择第一个具有最小日期的非空行。我想在满足该条件时使用CASE,然后使用1或0 所以更像是行不是,日期是最小日期,然后是1或0。我只需要选择一行。考虑: 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 mar
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
另一个选项(用于BigQuery标准SQL)
第一个是最小函数。MINIF现在不适用于google Bigquery
#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