Опустить пустые строки из запроса с условными выражениями - PullRequest
0 голосов
/ 17 сентября 2011

Есть ли способ пропустить строки, в которых "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 Ответ

1 голос
/ 17 сентября 2011
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

Это должно работать

...