Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择第一个具有最小日期的非空行(大查询)_Sql_Date_Select_Google Bigquery_Window Functions - Fatal编程技术网

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

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

我想选择第一个具有最小日期的非空行。我想在满足该条件时使用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 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