缺少数据时选择一行

时间:2013-07-04 12:17:59

标签: postgresql

我有一个问题。

我使用这个直接的前向查询来每天检索数据。部分数据是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

我如何从表格中选择没有日期的日期,而日期ID为002且缺少日期?

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

1 个答案:

答案 0 :(得分: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
相关问题