选择计数(日期)并将空日期设置为零值

时间:2018-12-31 05:49:54

标签: mysql database mysqli

我的问题是我无法计算该月出现了多少数据,并且如果没有数据将默认值设为0,则无法计算。

id | data_plan | data_start
---------------------------
1  | 2018-7-29 | 2018-8-29
2  | 2018-7-29 | 2018-9-29
3  | 2018-9-29 | 2018-10-29
4  | 2018-3-29 | 2018-10-29
5  | 2018-7-29 | 2018-9-29
6  | 2018-9-01 | 2018-9-29
7  | 2018-4-29 | 2018-11-29

我的结果

cplan       | cstart
---------------------------
March       | October
April       | November
July        | August
July,July   | September,September
September   | September
September   | October

所需的结果。

monthdata | cplan       | cstart
---------------------------
January   | 0       | 0
February  | 0       | 0
March     | 1       | 0
April     | 1       | 0
May       | 0       | 0
June      | 0       | 0
July      | 3       | 0
Aug       | 0       | 1
Sept      | 2       | 3
October   | 0       | 2
November  | 0       | 1
December  | 0       | 0

这是我的查询

SELECT 
GROUP_CONCAT(Monthname(data_plan)) as cplan,
GROUP_CONCAT(Monthname(data_start)) as cstart
FROM 
data 
group by data_plan,data_start

可以帮我吗? My Fiddle

6 个答案:

答案 0 :(得分:1)

要获得所需的结果,首先必须创建一个月表,然后必须LEFT JOIN到每个月包含data_plandata_start计数的表:

SELECT MONTHNAME(CONCAT('2018-', m.month, '-01')) AS month, 
       COALESCE(cplan, 0) AS cplan,
       COALESCE(cstart, 0) AS cstart
FROM (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
                        UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
                        UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
                        UNION SELECT 11  UNION SELECT 12) m
LEFT JOIN (SELECT MONTH(data_plan) AS month, COUNT(*) AS cplan
           FROM data
           GROUP BY month) dp ON dp.month = m.month
LEFT JOIN (SELECT MONTH(data_start) AS month, COUNT(*) AS cstart
           FROM data
           GROUP BY month) ds ON ds.month = m.month
GROUP BY m.month

输出:

month       cplan   cstart
January     0       0
February    0       0
March       1       0
April       1       0
May         0       0
June        0       0
July        3       0
August      0       1
September   2       3
October     0       2
November    0       1
December    0       0

Updated SQLFiddle

答案 1 :(得分:0)

使用COUNT()函数:

SELECT 
    COUNT(Monthname(data_plan)) as cplan,
    COUNT(Monthname(data_start)) as cstart
FROM data
GROUP BY Monthname(data_plan)

答案 2 :(得分:0)

您可以对子项使用子查询来查找月份列表,并向左联接数据表

df_data["Code"] = df_data['Description'].apply(lambda x: codelookup(x))

答案 3 :(得分:0)

解决方案1:

WITH months(MonthNumber, Month_Name) AS
(
    SELECT 1, datename(M, '2018-01-01') Month_Name
    UNION ALL
    SELECT MonthNumber+1, datename(M, '2018-' + str(MonthNumber+1) + '-01') Month_Name
    FROM months
    WHERE MonthNumber < 12
)
select f0.Month_Name, f2.cplan, f4.cstart 
from months f0
outer apply
(
select count(*) cplan from data f1
where month(f1.data_plan)=f0.MonthNumber
) f2

outer apply
(
select count(*) cstart from data f3
where month(f3.data_start)=f0.MonthNumber
) f4

答案 4 :(得分:0)

解决方案2:

WITH months(MonthNumber, Month_Name) AS
(
    SELECT 1, datename(M, '2018-01-01') Month_Name
    UNION ALL
    SELECT MonthNumber+1, datename(M, '2018-' + str(MonthNumber+1) + '-01') Month_Name
    FROM months
    WHERE MonthNumber < 12
)
select f0.Month_Name, 
(select count(*) cplan from data f1 where month(f1.data_plan)=f0.MonthNumber) cplan,
(select count(*) cstart from data f1 where month(f1.data_start)=f0.MonthNumber) cstart
from months f0

答案 5 :(得分:0)

解决方案3:

WITH months(MonthNumber, Month_Name) AS
(
    SELECT 1, datename(M, '2018-01-01') Month_Name
    UNION ALL
    SELECT MonthNumber+1, datename(M, '2018-' + str(MonthNumber+1) + '-01') Month_Name
    FROM months
    WHERE MonthNumber < 12
),
CountPlan as (
select month(data_plan) MonthNumber, count(*) cplan 
from data 
group by month(data_plan)
),
CountStart as (
select month(data_start) MonthNumber, count(*) cstart 
from data 
group by month(data_start)
)
select f0.Month_Name, 
isnull(f1.cplan, 0) cplan,
isnull(f2.cstart, 0)  cstart
from months f0
left outer join CountPlan f1 on f1.MonthNumber=f0.MonthNumber
left outer join CountStart f2 on f2.MonthNumber=f0.MonthNumber