Почему здесь Null будет если name_o_d имеет значения наименования нефтебаз. А мне нужно подсчитать только для тех у которых имена типа "Какаято НБ"
SELECT SUM(CASE WHEN extract (year FROM T4.date)=extract (year FROM current_date) AND extract (month FROM T4.date) IN (1,2,3,4,5,6) AND Name_O_d = '% НБ' THEN T4.Num_O*1000 END) AS "1-6", SUM(CASE WHEN extract (year FROM T4.date)=extract (year FROM current_date) AND extract (month FROM T4.date) NOT IN (1,2,3,4,5,6) AND Name_O_d = '% НБ' THEN T4.Num_O*1000 END) AS "6-12" FROM (SELECT Name_R, date, Num_O, Name_O_d FROM (SELECT *FROM Region FULL JOIN (SELECT *FROM Province, Oil_deport, PP WHERE Province.id_P = Oil_deport.id_P AND Oil_deport.id_O_d = PP.id_O_d) AS T2 ON Region.id_R = T2.id_R) AS T3 GROUP BY Name_R, date, Num_O, Name_O_d) AS T4;
SELECT SUM(CASE
WHEN extract (year FROM T4.date)=extract (year FROM current_date) AND extract (month FROM T4.date) IN (1,2,3,4,5,6) AND Name_O_d = '% НБ' THEN
T4.Num_O*1000
END) AS "1-6",
SUM(CASE WHEN extract (year FROM T4.date)=extract (year FROM current_date) AND extract (month FROM T4.date) NOT IN (1,2,3,4,5,6) AND Name_O_d = '% НБ' THEN
T4.Num_O*1000
END) AS "6-12"
FROM
(SELECT Name_R, date, Num_O, Name_O_d
FROM (SELECT *FROM Region FULL JOIN (SELECT *FROM Province, Oil_deport, PP WHERE Province.id_P = Oil_deport.id_P AND Oil_deport.id_O_d = PP.id_O_d) AS T2 ON Region.id_R = T2.id_R) AS T3
GROUP BY Name_R, date, Num_O, Name_O_d) AS T4;