简体   繁体   English

如果带有时间戳大查询

[英]IF with timestamp bigquery

I need to add an attribute that indicates if that version is an original or copy.我需要添加一个属性来指示该版本是原始版本还是副本。 If is the first version of the site, is original, it is not, is a copy.如果是网站的第一版,就是原创,否则,就是副本。

the table:桌子:

id_site  id_version timestamp_version
1        5589       2/3/2022
1        2030       10/7/2022
1        1560       10/8/2022
2        6748       2/3/2022
2        7890       2/4/2022
3        4532       2/3/2022

The expected result:预期结果:

id_site id_version  timestamp_version   type_version
1       5589        2/3/2022            original
1       2030        10/7/2022           copy
1       1560        10/8/2022           copy
2       6748        2/3/2022            original
2       7890        2/4/2022            copy
3       4532        2/3/2022            original

You can use an IF or CASE here.您可以在此处使用IFCASE They are mostly interchangeable, but my preference is CASE since it's portable to nearly any other RDBMS where IF is only supported in a few.它们大部分是可以互换的,但我更喜欢CASE ,因为它可以移植到几乎所有其他仅少数支持IF的 RDBMS。

 CASE WHEN ROW_NUMBER() OVER (PARTITION BY id_site ORDER BY timestamp_version ASC) = 1 THEN 'copy' ELSE 'original' END

Inside the CASE expression we do a ROW_NUMBER() window function will "window" or partition each row in the result set by id_site and number each record for each distinct id_site sequentially ordered by timestamp_version in ascending order.CASE表达式中,我们执行 ROW_NUMBER() window function 将按 id_site“窗口”或分区结果集中的每一行,并为每个不同的id_site的每条记录id_site ,按timestamp_version按升序顺序排列。 We test to see if that ROW_NUMBER() is 1 and then label it with original or copy .我们测试ROW_NUMBER()是否为1 ,然后用originalcopy其设为 label。

You can use a window function in an if statement for that:为此,您可以在 if 语句中使用 window function:

with test as (

    select * from unnest([
        struct(1 as id_site, 5589 as id_version, timestamp(date "2022-03-02") as timestamp_version),               
            (1, 2030, timestamp(date "2022-07-10")),
            (1, 1560, timestamp(date "2022-08-10")),
            (2, 6748, timestamp(date "2022-03-02")),
            (2, 7890, timestamp(date "2022-04-02")),
            (3, 4532, timestamp(date "2022-03-02"))
    ])
)

select 
    *,
    IF(timestamp_version = min(timestamp_version) over (partition by id_site), "original", "copy") AS type_version 
from test

Consider below option考虑以下选项

select *,
  if(lag(id_version) over prev is null, 'original', 'copy') type_version,
from your_table
window prev as (partition by id_site order by timestamp_version)    

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM