首页 > 解决方案 > How to replace NULL values without updating the table or creating temp table

问题描述

I have a sick day table where the data only show when the employee is sick but there is no input when he/she is healthy. I am trying to get the full count of all employees, sick or not. I have the query below and I was able to create a new field (present_cnt) and the only fields populated on each row were the present_cnt=1 and the calendar date and the rest were null. I don't know how to populate the rest of the fields (for employee 921 and 922) to replace the nulls when they are not sick. I don't have access to update the table or create a temp table. I only need the result of the query to populate the null values for each employees. Thanks!

SELECT
employeeId,
sick_date,
sick_cnt,
CASE WHEN calendar_date = sick_date THEN 0 ELSE 1 END AS present_cnt,
calendar_date

FROM
sick_day_table

This is the result I have:

employeeId  sick_date   sick_cnt    present_cnt calendar_date
921         4/29/2020 0:00  1              0    4/29/2020 0:00
921         4/30/2020 0:00  1              0    4/30/2020 0:00
NULL        NULL          NULL             1    5/1/2020 0:00
NULL        NULL          NULL             1    5/2/2020 0:00
922         4/29/2020 0:00  1              0    4/29/2020 0:00
NULL        NULL          NULL             1    4/30/2020 0:00
NULL        NULL          NULL             1    5/1/2020 0:00
NULL        NULL          NULL             1    5/2/2020 0:00

Desire result:

employeeId  sick_date   sick_cnt    present_cnt calendar_date
921         4/29/2020 0:00  1              0    4/29/2020 0:00
921         4/30/2020 0:00  1              0    4/30/2020 0:00
921         5/1/2020 0:00   0              1    5/1/2020 0:00
921         5/2/2020 0:00   0              1    5/2/2020 0:00
922         4/29/2020 0:00  1              0    4/29/2020 0:00
922         4/30/2020 0:00  0              1    4/30/2020 0:00
922         5/1/2020 0:00   0              1    5/1/2020 0:00
922         5/2/2020 0:00   0              1    5/2/2020 0:00

标签: mysql

解决方案


推荐阅读