如何使用 count() 返回零值

发布于 2024-12-06 05:42:10 字数 3197 浏览 0 评论 0原文

如何返回 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;  

How can I return rows where 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;  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2024-12-13 05:42:10

点击谷歌并查找“Having”。在本例中,count(a.thread_id) = 0

Hit up google and look up "Having". In this case, having count(a.thread_id) = 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文