使用条件表达式省略查询中的空行

时间:2011-09-17 07:28:20

标签: postgresql null conditional expression

有没有办法省略“CREATE”和“DELETE”都为NULL的行? 这是我的查询,非常感谢任何帮助!

这适用于PostgreSQL数据库。*

SELECT 
jivedw_container.container_id as "ID", jivedw_day.year as "Year",
jivedw_day.week_of_year as "Week Of", jivedw_container.name as "Community",
jivedw_activity_agg_user_day.direct_object_type as "Object",
( jivedw_container.name 
                   || '@' 
                   || To_char(CURRENT_DATE- INTERVAL '138 day', 'mm/dd/yy') ) AS
"Object Link", 
sum(CASE WHEN jivedw_activity_agg_user_day.activity_type = '20' THEN 1 ELSE NULL END)
as "CREATE",
sum(CASE WHEN jivedw_activity_agg_user_day.activity_type = '30' THEN 1 ELSE NULL END)
as "DELETE"
FROM jivedw_activity_agg_user_day
INNER JOIN jivedw_day on jivedw_activity_agg_user_day.day_id = jivedw_day.day_id
INNER JOIN jivedw_container on jivedw_activity_agg_user_day.dw_container_id =
jivedw_container.dw_container_id
WHERE (jivedw_day.begin_ts BETWEEN CURRENT_DATE - INTERVAL '138 day' AND CURRENT_DATE - INTERVAL '132 day')
AND (jivedw_activity_agg_user_day.direct_object_type IN (1,2,37,38, 102, 1100))
GROUP BY
jivedw_container.container_id,
jivedw_container.name, 
jivedw_day.year,
jivedw_day.week_of_year,
jivedw_activity_agg_user_day.direct_object_type
ORDER BY jivedw_container.container_id, jivedw_container.name
;

1 个答案:

答案 0 :(得分:1)

SELECT * FROM (SELECT 

jivedw_container.container_id as "ID", jivedw_day.year as "Year",
jivedw_day.week_of_year as "Week Of", jivedw_container.name as "Community",
jivedw_activity_agg_user_day.direct_object_type as "Object",
( jivedw_container.name 
  || '@' 
  || To_char(CURRENT_DATE- INTERVAL '138 day', 'mm/dd/yy') ) AS "Object Link", 
sum(CASE WHEN jivedw_activity_agg_user_day.activity_type = '20' THEN 1 ELSE NULL END)
as "CREATE",
sum(CASE WHEN jivedw_activity_agg_user_day.activity_type = '30' THEN 1 ELSE NULL END)
as "DELETE"
FROM jivedw_activity_agg_user_day
INNER JOIN jivedw_day on jivedw_activity_agg_user_day.day_id = jivedw_day.day_id
INNER JOIN jivedw_container on jivedw_activity_agg_user_day.dw_container_id =
jivedw_container.dw_container_id
WHERE (jivedw_day.begin_ts BETWEEN CURRENT_DATE - INTERVAL '138 day' AND CURRENT_DATE - INTERVAL '132 day')
AND (jivedw_activity_agg_user_day.direct_object_type IN (1,2,37,38, 102, 1100))
GROUP BY
jivedw_container.container_id,
jivedw_container.name, 
jivedw_day.year,
jivedw_day.week_of_year,
jivedw_activity_agg_user_day.direct_object_type
ORDER BY jivedw_container.container_id, jivedw_container.name
;) as SUB_QUERY WHERE SUB_QUERY.CREATE IS NOT NULL AND SUB_QUERY.DELETE is NOT NULL

这应该有效