1

我有一个问题。

我每天都使用这个直接的查询来检索数据。部分数据是一个ID。

例如,我得到了 ID 的 001 002 003 和 004。每个 ID 都有一些包含数据的列。我每天都会根据这些数据生成一份报告。典型的一天看起来像

ID Date Value
001 2013-07-02 900 
002 2013-07-02 800
003 2013-07-02 750 
004 2013-07-02 950
Select * 

FROM
     myTable

WHERE datum > now() - INTERVAL '2 days' and ht not in (select ht from blocked_ht)

order by ht, id;

有时 1 id 的导入会失败。所以我的数据看起来像

ID Date Value
001 2013-07-02 900
003 2013-07-02 750
004 2013-07-02 950

知道缺少 1 个 ID 至关重要,在我的报告中可视化(在 Japserreports 中制作)所以我插入了一个没有日期和值 0 的 ID 并编辑了查询:

SELECT *

FROM
     "lptv_import" lptv_import

WHERE datum > now() - INTERVAL '2 days' and ht not in (select ht from negeren_ht) OR datum IS NULL

order by ht, id;

现在数据看起来像这样:

001 2013-07-02 900
002            800
003 2013-07-02 750
004 2013-07-02 950

如何从表格中选择没有日期的行 WHEN ID 002 WITH a date is missing?

嗯,这看起来比我想象的要复杂......

4

1 回答 1

1
select
    id, coalesce(datum::text, 'NULL') as "date", "value"
from
    (
        select distinct id
        from lptv
    ) id
    left join
    lptv using (id)
where
    datum > now() - INTERVAL '2 days' 
    and not exists (select ht from negeren_ht where ht = lptv.ht)
order by id
于 2013-07-04T13:04:25.940 回答