如何在 Big Query 中使用下一个非空 LEAD 值?

问题描述 投票:0回答:1

我想选择下一个非空引导场景_时间戳。我尝试了以下方法,但收到“从 BOOL 到 TIMESTAMP 的转换无效”错误。

FORMAT_TIMESTAMP(
        '%Y-%m-%d %H:%M:%E6S',
        CAST(
            CONCAT
            (
                CAST(date AS STRING),
                ' ',
                FORMAT_TIMESTAMP
                ('%H:%M:%E6S', cast(lead(scenario_timestamp is not null ) OVER (ORDER BY date DESC )  as timestamp
                  )
                )
            ) AS TIMESTAMP
        )
    )
sql google-bigquery
1个回答
0
投票

这有效:

SELECT
            date,
            name,
            CASE
                WHEN scenario_timestamp IS NOT NULL THEN scenario_timestamp
                ELSE FORMAT_TIMESTAMP (
                    '%Y-%m-%d %H:%M:%E6S',
                    CAST(
                        CONCAT (
                            CAST(date AS STRING),
                            ' ',
                            FORMAT_TIMESTAMP (
                                '%H:%M:%E6S',
                                CAST(
                                    (
                                        SELECT
                                            MIN(sub.scenario_timestamp)
                                        FROM
                                            dates_combine AS sub
                                        WHERE
                                            sub.date > main.date
                                            AND sub.scenario_timestamp IS NOT NULL
                                    ) AS TIMESTAMP
                                )
                            )
                        ) AS TIMESTAMP
                    )
                )
            END AS scenario_timestamp
        FROM
            dates_combine AS main
© www.soinside.com 2019 - 2024. All rights reserved.