每天创建最新值视图(无跳过天数)

Create View for Latest Value Each Day (No Skipped Days)

我有一个table每天记录每个设备的固件版本。如果设备出现故障,用于填充设备固件的脚本将无法获取值,因此没有离线天数的记录。我需要一个视图,每天都会 return 每个设备的最新固件版本,无论设备是否关闭。这在 Postgres SQL:

中非常有效
SELECT
    d.ip,
    d.date,
    CASE
        WHEN f.firmware_version IS NOT NULL THEN f.firmware_version
        ELSE (--Use last available firmware_version for the device:
            SELECT l.firmware_version
            FROM firmware l
            WHERE l.date < d.date AND l.firmware_version IS NOT NULL
            ORDER BY l.date DESC
            LIMIT 1)
        END AS firmware_version
FROM
    devices d --Table with a record for every device every day
    LEFT JOIN firmware f ON d.date = f.date AND d.ip = f.ip

但是,我们正在过渡到 Denodo,我无法在 Denodo 中使用此查询。 case 语句中的子查询似乎失败了。有谁知道我如何获得这样的逻辑来在 Denodo 中创建视图?

我想通了!它有点长和复杂,但它按照我希望的方式工作。如果对其他人有帮助,这里是解决方案:

--Get all values of firmware prior to the listed date
--Note: Will need to find the latest firmware for each date in a later step
WITH firmware_prep (
    ip,
    date_main,
    date_firmware,
    firmware
) AS (
    SELECT
        d.ip,
        d.date,
        f.date,
        f.firmware
    FROM
        device d LEFT JOIN
        firmware f ON (d.ip = f.ip AND f.date <= d.date AND f.firmware IS NOT NULL) 
)

SELECT
    s.ip,
    s.date_main AS date,
    f.firmware
FROM
    (--Here's where you find which firmware date is the latest available date for each listed date:
        SELECT
            ip,
            date_main,
            MAX(date_firmware) AS select_date
        FROM
            firmware_prep
        GROUP BY
            ip,
            date_main
    ) s LEFT JOIN
    firmware f ON s.select_date = f.date AND s.ip = f.ip