2
I have a code that returns the activities that have been completed within a 12 month period.
SELECT
empresas,
tipo_atividade,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 1 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_1,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 2 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_2,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 3 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_3,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 4 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_4,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 5 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_5,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 6 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_6,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 7 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_7,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 8 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_8,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 9 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_9,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 10 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_10,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 11 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_11,
CASE WHEN (SUM(CASE WHEN MONTH(dt_vencimento) = 12 THEN 1 ELSE 0 END)) > 0 THEN 'OK' ELSE 'PENDENTE' END AS MES_12
FROM
tbl_atividades
WHERE
YEAR(dt_fim) = '2018'
GROUP BY
empresas, tipo_atividade;
Upshot
However there is a flaw in this code, the case is if I registered a company and reported that the activities will start from July for example, from January to June should not appear as pending, should appear as Null or Empty.
My question is as follows as I can inform in the query to check if there is any activity on that date and if it has not returned null or empty.
You could not exchange the "PENDING" then for "NULL", in this case?
– Denied
TRY CASE WHEN (SUM(CASE WHEN dt_maturity IS NULL THEN ELSE (CASE WHEN MONTH(dt_maturity) = 1 THEN 1 ELSE 0 END))) > 0 THEN 'OK' ELSE 'PENDING' END AS MES_1,
– Motta
@Motta - Apparently there is some error in this query did not run.
– Andrews Gomes
CASE WHEN (SUM(CASE WHEN dt_maturity IS NULL THEN NULL ELSE (CASE WHEN MONTH(dt_maturity) = 1 THEN 1 ELSE 0 END) END)) > 0 THEN 'OK' ELSE 'PENDING' END AS MES_1,
– Motta
the basic idea is if it is null to generate a null because null does not sum
– Motta
The idea is good, but it didn’t work, the closest thing was CASE WHEN (SUM(CASE WHEN MONTH(dt_maturity) = 1 THEN 1 ELSE NULL END)) > 0 THEN STATUS ELSE NULL END AS MES_1
– Andrews Gomes
What went wrong ?
– Motta