mysql - 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
解决方案
推荐阅读
- python - 获取新列之前的列中的元素
- sql - 如何不插入两个相同的值
- c - 为什么两个相同的指针不等于-O1?
- reporting-services - 微软报告生成器中的 IIF
- html - 如何将 Scrapy XPath 与 XML 命名空间一起使用?
- r - 在时间序列中,当个体长度不同时,如何对个体的最后一次观察进行子集化
- ios - SiriKit INSearchForAccountsIntent 不使用 CFBundleSpokenName 进行应用程序名称发音
- cuda - 将 CUDA 内核调用作为参数宏传递时,预处理器宏中的参数数量不正确
- node.js - 存储和检索数据,通过 REST 到 mongodb 使用节点上的 fetch 实现
- c# - 内存冲突在运行时动态附加到方法