如何用count()返回零值

时间:2011-09-21 20:16:27

标签: postgresql subquery grouping aggregates

如何返回count(a.thread_id)= 0的行?

SELECT current_date                                       AS "Import Date",
       b.container_id                                     AS "ID",
       b.name                                             AS "Container Name",
       b.creation_ts                                      AS
       "Container Creation Date",
       COUNT(a.thread_id)                                 AS
       "Total Number of Un-Replied-To Threads",
       round(Avg(current_date - ( 99 ) - a.creation_ts :: DATE)) AS "Average Age",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) = 1 ) THEN
             1
             ELSE 0
           END)                                           AS "1 Day",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) = 2 ) THEN
             1
             ELSE 0
           END)                                           AS "2 Day",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) = 3 ) THEN
             1
             ELSE 0
           END)                                           AS "3 Day",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) BETWEEN 4
                    AND 6 )
           THEN 1
             ELSE 0
           END)                                           AS "4-6 Days",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) BETWEEN 7
                    AND 10
                  )
           THEN 1
             ELSE 0
           END)                                           AS "7-10 Days",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) BETWEEN 11
                    AND 15
                  )
           THEN 1
             ELSE 0
           END)                                           AS "11-15 Days",
       SUM(CASE
             WHEN ( ( current_date - ( 99 ) - a.creation_ts :: DATE ) > 15 )
           THEN 1
             ELSE 0
           END)                                           AS "15+ Days"
FROM   jivedw_message a,
       jivedw_container b
WHERE  a.thread_id IN (SELECT a.thread_id
                       FROM   jivedw_message a
                       GROUP  BY a.thread_id
                       HAVING COUNT(a.message_id) = 1)
       AND a.thread_id NOT IN (SELECT a.thread_id
                               FROM   jivedw_message a
                                      INNER JOIN jivedw_object
                                        ON ( jivedw_object.object_id =
                                             a.thread_id )
                                      INNER JOIN jivedw_activity_agg_user_day
                                        ON (
jivedw_activity_agg_user_day.direct_dw_object_id =
jivedw_object.dw_object_id
         )
WHERE  jivedw_activity_agg_user_day.direct_object_type =
    '1'
    AND jivedw_activity_agg_user_day.activity_type IN 
        (30, 70)
)
AND ( a.container_id = b.container_id )
AND ( b.creation_ts > '2009-11-19' :: DATE )
GROUP  BY b.container_id,
          b.name,
          b.creation_ts
ORDER  BY b.name;  

1 个答案:

答案 0 :(得分:2)

点击谷歌并查找“有”。在这种情况下,having count(a.thread_id) = 0