试图填写mysql中的日期空白

时间:2014-10-15 18:21:51

标签: mysql date

我一直在努力解决这个问题,并且不能让这个工作

我有以下表格

日历 - 包含50年的日期

+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Date  | date | NO   | PRI | NULL    |       |
+-------+------+------+-----+---------+-------+

市场 - 属于这些部门的部门和市场清单,即一个部门包含多个市场但市场属于一个部门

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| ID       | int(11)      | NO   | PRI | NULL    | auto_increment |
| Market   | varchar(255) | NO   | PRI | NULL    |                |
| Division | varchar(255) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

每月总案例数 - 包含针对某个市场和特定平台的特定平台开设的问题数量

+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| Case Month | varchar(10)  | YES  |     | NULL    |       |
| Division   | varchar(255) | YES  |     | NULL    |       |
| Market     | varchar(255) | YES  |     | NULL    |       |
| Platform   | varchar(50)  | YES  |     | NULL    |       |
| Count      | bigint(21)   | NO   |     | 0       |       |
+------------+--------------+------+-----+---------+-------+

我正在尝试制作一个列表,其中包括日历中的每个月,每个部门,每个市场以及每个平台的数量

2012-01-01 Central Atlanta Type A 3 2012-01-01 Central Atlanta Type B 0 2012-01-01 Central Michigan Type A 1 2012-01-01 Central Michigan Type B 1 2012-01-01 Central Nashville Type A 0 2012-01-01 Central Nashville Type B 1

问题是Total Monthly Case Counts仅包含存在的日期和平台的值 如果某个市场中的给定平台没有计算一个案例,并且在给定月份内没有记录,则没有记录。在上述期望输出中,0值不存在。

我一直在尝试将日历表加入到每月总案例表中,但是每个月都不能显示所有部门和市场,更不用说如果记录真的不存在则包括0计数月案例表。 我已经做到这一点,但它没有填补部门,市场和平台

    SELECT
    Calendar.Date AS Date,
    `Total Monthly Case Count`.Division,
    `Total Monthly Case Count`.Market,
    `Total Monthly Case Count`.Platform,
    IFNULL(`Total Monthly Case Count`.Count,0) AS Count
    FROM
    Calendar
    LEFT JOIN `Total Monthly Case Count` ON Calendar.Date = `Total Monthly Case Count`.`Case Month` ,
    Markets
    WHERE
    (Calendar.`Date` BETWEEN (SELECT MIN(`Total Monthly Case Count`.`Case Month`) FROM `Total Monthly Case Count`) AND (SELECT MAX(`Total Monthly Case Count`.`Case Month`) FROM `Total Monthly Case Count`)) AND Calendar.`Date` = `Total Monthly Case Count`.`Case Month`
    ORDER BY
    Date ASC,
    Markets.Division ASC,
    Markets.Market ASC

这会产生以下结果

2012-01-01 Central Atlanta Type A 3 2012-01-01 Central Michigan Type A 1 2012-01-01 Central Nashville Type A 1 2012-01-01 Central Pompano Type A 1 2012-01-01 East BAD Type B 1 2012-01-01 East BAD Type A 2 2012-01-01 East BAD Type C 1

非常感谢任何帮助

0 个答案:

没有答案