Left-Outer Join in Postgres Not Returning Values for Null

前端 未结 4 800
清歌不尽
清歌不尽 2021-01-04 13:07

A download is comprised of download-times, download-time id, and buno ID. Faults are comprised of fault-codes, download-time id, status, and type. A download can have many

4条回答
  •  臣服心动
    2021-01-04 13:42

    The left outer join selects everything in the first table plus matching rows in the second table. The first table seems to consist of download attempts. So, your result from the "from" includes all download attempts.

    But, it does not necessarily contain all your fault codes. What is happening is that you have no faults for one or more codes that meet the criteria.

    You need a table that contains all the fault codes, in order for this to work. Here I just create a list of the fault codes as the first table. I think the following query does this:

    SELECT thefaults.faultcode, f.downloadtimeid, d.downloadtime, count(*) as faultcount
    FROM  (select 1000 as faultcode union all select 1100
          ) thefaults join
          fs_fault f
          on f.faultcode = thefaults.faultcode and
             f.statusid in (2, 4) left outer join
          download_time d
          ON f.downloadtimeid = d.id
    WHERE (d.downloadtime BETWEEN '04/11/2011' AND '05/01/2012') AND
          d.bunoid = 166501
    GROUP BY d.bunoid, f.downloadtimeid, d.downloadtime, f.faultcode 
    

    I admit: I am using SQL Server syntax to create "thefaults".

提交回复
热议问题