我有一个只包含日期数据的表,我想计算该数据在同一月份的次数,对于该问题,该问题已经结束,现在又出现了一个新问题,那就是缺少的月份,这是因为该月没有数据。现在,我想添加默认值为0的空月份,即使该月份不在表中。有人可以根据我的查询为我提供帮助吗?
这是我的数据
Rooms
这是我的查询
House
结果是
Rooms
并且我想添加新查询,这样我的结果将是
Room-House-Person
答案 0 :(得分:1)
您需要一个包含所有月份的表格。您可以使用UNION ALL
通过子查询创建一个临时对象。然后,将count子查询加入该表。
SELECT m.month month_table,
coalesce(s.count, 0) cstart,
coalesce(e.count, 0) cend
FROM (SELECT 1 month
UNION ALL
SELECT 2 month
UNION ALL
SELECT 3 month
UNION ALL
SELECT 4 month
UNION ALL
SELECT 5 month
UNION ALL
SELECT 6 month
UNION ALL
SELECT 7 month
UNION ALL
SELECT 8 month
UNION ALL
SELECT 9 month
UNION ALL
SELECT 10 month
UNION ALL
SELECT 11 month
UNION ALL
SELECT 12 month) m
LEFT JOIN (SELECT month(n.start_date) month,
count(*) count
FROM newdata n
GROUP BY month(n.start_date)) s
ON s.month = m.month
LEFT JOIN (SELECT month(n.end_date) month,
count(*) count
FROM newdata n
GROUP BY month(n.end_date)) e
ON e.month = m.month
ORDER BY m.month;
答案 1 :(得分:0)
这应该是此问题的答案:
library(stringr)
str_detect(df1$BusName, as.character(df1$FirstName))
#[1] TRUE FALSE TRUE FALSE
作为您在注释中的请求,我更改了代码(现在它没有错误,并且月份号更改为月份名称)
我希望它完成了!