0
如何返回count(a.thread_id)= 0的行?如何使用count()返回零值
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;
其他人肯定已經寫了這個查詢,答案已經存在,在子查詢中:[HAVING](http://www.postgresql.org/docs/current/static/sql-select。 html#SQL-HAVING) –
我寫了查詢。 我不只想返回count爲零的行,我希望它返回count> 0且行count = 0的行。 –
當前,它只返回count> 0的行,並省略count = 0。 任何意見/提示將不勝感激。 –