0

我有一张表,每天记录每个设备的固件版本。如果设备出现故障,填充设备固件的脚本将无法获取它的值,因此没有离线天数的记录。我需要一个每天都会为每个设备返回最新固件版本的视图,无论设备是否关闭。这在 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 中创建视图?

4

1 回答 1

0

我想到了!它有点长和复杂,但它就像我希望的那样工作。如果它对其他人有帮助,这是解决方案:

--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
于 2019-10-10T22:41:58.947 回答