Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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_Google Bigquery - Fatal编程技术网

Sql 使用第一个值用前面的非空值填充空值

Sql 使用第一个值用前面的非空值填充空值,sql,google-bigquery,Sql,Google Bigquery,我要把两张桌子合在一起 在第一个表中,我有一些在特定时间开始的项目。在第二个表中,我有每个项目的开始和结束时间之间每分钟的值和时间戳 第一桌 UniqueID Items start_time 123 one 10:00 AM 456 two 11:00 AM 789 three 11:30 AM 第二桌 UniqueID Items time_hit value 123 one 10:00 AM x 123 one

我要把两张桌子合在一起

在第一个表中,我有一些在特定时间开始的项目。在第二个表中,我有每个项目的开始和结束时间之间每分钟的值和时间戳

第一桌

UniqueID  Items start_time
123       one   10:00 AM
456       two   11:00 AM
789       three 11:30 AM
第二桌

UniqueID Items time_hit  value
123      one   10:00 AM    x
123      one   10:05 AM    x
123      one   10:10 AM    x
123      one   10:30 AM    x
456      two   11:00 AM    x
456      two   11:15 AM    x
789      three 11:30 AM    x
因此,当连接这两个表时,我有:

UniqueID Items start_time  time_hit   value 
123      one   10:00 AM    10:00 AM   x
123      null  null        10:05 AM   x
123      null  null        10:10 AM   x
123      null  null        10:30 AM   x
456      two   11:00 AM    11:00 AM   x
456      null  null        11:15 AM   x
789      three 11:30 AM    11:30 AM   x
我想用非null行中的值替换这些null值

因此,预期的结果是

UniqueID Items start_time  time_hit   value 
123      one   10:00 AM    10:00 AM   x
123      one   10:00 AM    10:05 AM   x
123      one   10:00 AM    10:10 AM   x
123      one   10:00 AM    10:30 AM   x
456      two   11:00 AM    11:00 AM   x
456      two   11:00 AM    11:15 AM   x
789      three 11:30 AM    11:30 AM   x
我尝试使用以下函数构建我的连接,但未成功:

  FIRST_VALUE(Items IGNORE NULLS) OVER (
    PARTITION BY time_hit ORDER BY time_hit
    ROWS BETWEEN CURRENT ROW AND
    UNBOUNDED FOLLOWING) AS test

我的问题有点离题。我发现UniqueID不一致,这就是为什么我的输出中有这些空值。因此,当连接两个表时,验证的答案是填充空值的一个好选项,并且其中一个表的行数比另一个表的行数多。

我猜您希望通过使用内部连接得到输出。但不确定为什么使用第一个值

SELECT I.Item, I.Start_Time, ID.Time_hit,  ID.Value
FROM Items I
INNER JOIN ItemDetails ID
 ON I.Items = ID.Items

如果您正在寻找查看此方法的任何特定原因,请解释。

一种替代解决方案是使用NOT EXISTS子句作为联接条件,并使用相关子查询确保我们与相关记录相关

SELECT t1.items, t1.start_time, t2.time_hit, t2.value
FROM table1 t1
INNER JOIN table2 t2 
    ON  t1.items = t2.items
    AND t1.start_time <= t2.time_hit  
    AND NOT EXISTS (
        SELECT 1 FROM table1 t10
        WHERE 
            t10.items = t2.items 
            AND t10.start_time <= t2.time_hit
            AND t10.start_time > t1.start_time
    )
避免在大查询中不允许的联接条件上使用EXISTS的替代解决方案:只需将该条件移动到WHERE子句

您可以使用first_值,但在这种情况下,last_值也可以使用。导入部分是在无界的前一行和当前行之间指定行,以设置窗口的边界

答案已更新,以反映更新后的问题,以及对第一个_值的偏好

结果


注意:我必须在SQLFIDLE中使用Oracle,所以我必须更改数据类型和列名。但它应该适用于您的数据库。

请提供您的预期结果。你的意思是继续进行包含非空值的行吗?让我知道它是否清晰。我猜我的问题不够清楚。。。是的,我使用了一个内部连接来连接两个表。。。我仍然有这些空值。请让我知道如何澄清。谢谢。我有以下错误存在子查询在连接谓词中不受支持@西蒙布雷顿:好的。。。使用与上一个查询功能相同的替代查询更新了我的答案,但如果不使用,则存在作为连接条件的连接。确定我可以运行该查询。不再出现错误,但它不工作。我实际上使用了一个唯一的ID来连接这两个表。这会影响您的解决方案吗?@SimonBreton:鉴于您提供的示例数据,我的查询似乎运行良好并返回预期的输出。。。我可以看到你编辑了你的文章,添加了UniqueID栏:这不是一个好的做法。你可能想问一个新问题,对你的目标和不同的样本数据/预期输出提供更多的解释。我想你是对的,我的问题不够清楚。我使用一个简单的左连接,我有这些空值。但是根据你的反应;我想知道我的加入是否有问题。我将提出一个新问题。谢谢尽管如此,来自@bruceskyaus的答案仍然有效。因此,与我使用的函数完全相同,只是它是最后一个值而不是第一个值。。。是吗?这些功能来自同一个系列,但有不同的功能。请参见,在这种情况下,第一个_值或最后一个_值都可以工作,因为空值被删除,窗口中只存在一行。值由无界前一行和当前行条件之间的行设置。希望这是有意义的。最好通过UniqueID进行分区以获得更好的性能,只要它与Items值一致。
| items | start_time | time_hit | value |
| ----- | ---------- | -------- | ----- |
| one   | 10:00:00   | 10:00:00 | x     |
| one   | 10:00:00   | 10:05:00 | x     |
| one   | 10:00:00   | 10:10:00 | x     |
| one   | 10:00:00   | 10:30:00 | x     |
| two   | 11:00:00   | 11:00:00 | x     |
| two   | 11:00:00   | 11:15:00 | x     |
| three | 11:30:00   | 11:30:00 | x     |
SELECT t1.items, t1.start_time, t2.time_hit, t2.value
FROM table1 t1
INNER JOIN table2 t2 
    ON  t1.items = t2.items
    AND t1.start_time <= t2.time_hit  
WHERE NOT EXISTS (
    SELECT 1 FROM table1 t10
    WHERE 
        t10.items = t2.items 
        AND t10.start_time <= t2.time_hit
        AND t10.start_time > t1.start_time
)
select
first_value(t1.UniqueId ignore nulls) over (partition by t2.UniqueId
                                           order by t2.time_hit
                                           rows between unbounded preceding and current row) as UniqueId,
first_value(t1.items ignore nulls) over (partition by t2.UniqueId
                                        order by t2.time_hit
                                        rows between unbounded preceding and current row) as Items,
first_value(t1.start_time ignore nulls) over (partition by t2.UniqueId
                                        order by t2.time_hit
                                        rows between unbounded preceding and current row) as start_time,
t2.time_hit,
t2.item_value
from table2 t2
left join table1 t1 on t1.start_time = t2.time_hit
order by t2.time_hit;
| UNIQUEID | ITEMS | START_TIME | TIME_HIT | ITEM_VALUE |
|----------|-------|------------|----------|------------|
|      123 |   one |   10:00:00 | 10:00:00 |          x |
|      123 |   one |   10:00:00 | 10:05:00 |          x |
|      123 |   one |   10:00:00 | 10:10:00 |          x |
|      123 |   one |   10:00:00 | 10:30:00 |          x |
|      456 |   two |   11:00:00 | 11:00:00 |          x |
|      456 |   two |   11:00:00 | 11:15:00 |          x |
|      789 | three |   11:30:00 | 11:30:00 |          x |