Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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 带条件的BigQuery LAST_值_Sql_Google Bigquery - Fatal编程技术网

Sql 带条件的BigQuery LAST_值

Sql 带条件的BigQuery LAST_值,sql,google-bigquery,Sql,Google Bigquery,下面是示例数据,我希望填充expectedOutput列中的值。我想根据规则计算它——如果isValid=1,给我上次isValid=1时的结果值。因此在本例中,第1行为null,因为这是第一个值。第6行显示苹果,因为这一行是有效的,苹果是上次有有效值时的水果 row fruit isValid expectedOutput (Prior valid value) 1 apple 1 NULL 2

下面是示例数据,我希望填充expectedOutput列中的值。我想根据规则计算它——如果isValid=1,给我上次isValid=1时的结果值。因此在本例中,第1行为null,因为这是第一个值。第6行显示苹果,因为这一行是有效的,苹果是上次有有效值时的水果

row           fruit       isValid     expectedOutput (Prior valid value)
1             apple       1           NULL
2             apple       0           NULL
3             apple       0           NULL
4             apple       0           NULL
5             orange      0           NULL
6             orange      1           apple
7             grape       1           orange
8             grape       0           NULL
这是我尝试过的,并没有得到正确的结果

LAST_VALUE(case when isValid = 1 then fruit end IGNORE NULLS) OVER(PARTITION BY all ORDER BY row)
见下面的例子

#standardSQL
with `project.dataset.table` as (
  select 1 row , 'apple' fruit, 1 isValid union all
  select 2, 'apple', 0 union all
  select 3, 'apple', 0 union all
  select 4, 'apple', 0 union all
  select 5, 'orange', 0 union all
  select 6, 'orange', 1 union all
  select 7, 'grape', 1 union all
  select 8, 'grape', 0 
)
select *, 
  if(isValid = 1, lag(fruit) over(partition by isValid order by row), null) as Prior_valid_value
from `project.dataset.table` 
# order by row           
有输出

参见下面的示例

#standardSQL
with `project.dataset.table` as (
  select 1 row , 'apple' fruit, 1 isValid union all
  select 2, 'apple', 0 union all
  select 3, 'apple', 0 union all
  select 4, 'apple', 0 union all
  select 5, 'orange', 0 union all
  select 6, 'orange', 1 union all
  select 7, 'grape', 1 union all
  select 8, 'grape', 0 
)
select *, 
  if(isValid = 1, lag(fruit) over(partition by isValid order by row), null) as Prior_valid_value
from `project.dataset.table` 
# order by row           
有输出


您需要向窗口添加边界以排除当前行:

无界前置和1前置之间的行
试试这个:

WITH
  table1 AS (
        SELECT 1 as row,  'apple'  as fruit,  1 as  isValid  union all
        SELECT 2 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 3 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 4 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 5 as row,  'orange' as fruit,  0 as  isValid  union all 
        SELECT 6 as row,  'orange' as fruit,  1 as  isValid  union all 
        SELECT 7 as row,  'grape'  as fruit,  1 as  isValid  union all   
        SELECT 8 as row,  'grape'  as fruit,  0 as  isValid
)

SELECT
  *,
  CASE
    WHEN isValid = 1 THEN LAST_VALUE(IF(isValid=1, fruit, NULL) IGNORE NULLS) OVER(ORDER BY row ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  END AS expectedOutput
FROM
  table1

您需要向窗口添加边界以排除当前行:

无界前置和1前置之间的行
试试这个:

WITH
  table1 AS (
        SELECT 1 as row,  'apple'  as fruit,  1 as  isValid  union all
        SELECT 2 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 3 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 4 as row,  'apple'  as fruit,  0 as  isValid  union all
        SELECT 5 as row,  'orange' as fruit,  0 as  isValid  union all 
        SELECT 6 as row,  'orange' as fruit,  1 as  isValid  union all 
        SELECT 7 as row,  'grape'  as fruit,  1 as  isValid  union all   
        SELECT 8 as row,  'grape'  as fruit,  0 as  isValid
)

SELECT
  *,
  CASE
    WHEN isValid = 1 THEN LAST_VALUE(IF(isValid=1, fruit, NULL) IGNORE NULLS) OVER(ORDER BY row ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  END AS expectedOutput
FROM
  table1