每日,每周,每月和每年的报告

时间:2016-02-12 12:47:02

标签: mysql report

我每天,每周,每月和每年都会报告。所有数据都来自一个表格,即: 表名:预订

reservation_id  firstname   lastname    arrival     departure   numNights   payable     downPayment balance 
216             Liam        Ree         22/03/2016  31/03/2016  9           30600.00    9180.00     21420.00 
215             Yuu         Yuu         08/03/2016  19/03/2016  11          69300.00    20790.00    48510.00
214             Kim         Kim         01/03/2016  04/03/2016  3           15300.00    4590.00     10710.00 
213             Jan         Jan         29/02/2016  02/03/2016  4           20400.00    6120.00     14280.00 
211             Wesley      Lin         17/02/2016  19/02/2016  2           6800.00     2040.00     4760.00 
212             Jan         Jan         23/02/2016  26/02/2016  3           20700.00    6210.00     14490.00 
218             Ram         Ram         03/01/2017  07/01/2017  4           20400.00    6120.00     14280.00 

我对每日的查询:

SELECT YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) AS 'year',
COUNT(reservation_id) AS 'reservations',
SUM(payable) AS 'total'
FROM `reservation`
GROUP BY YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s'))
ORDER BY YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) ASC

以下是查询结果(每日):

 Day    Reservations    Total   
NULL    4               78500.00
1       1               20400.00
3       2               84600.00

我不明白为什么第一行/结果为空,加上不是所需的输出,加上根据预约表中的数据,结果有些不对。我想看到的是像这一天(当天)的预订和这一天(仅当天)的总数(应付)。

每周报告

SELECT FROM_DAYS(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) 
-MOD(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) -1, 7)) AS week,
SUM(payable) AS total, COUNT(*) AS reservations FROM reservation GROUP BY FROM_DAYS(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) 
-MOD(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) -1, 7))
ORDER BY FROM_DAYS(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s'))
-MOD(TO_DAYS(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) -1, 7))

结果:

week        total       reservations    
NULL        78500.00    4
2016-01-03  15300.00    1
2016-07-31  69300.00    1
2017-02-26  20400.00    1

然后这不应该是什么。 期望的结果应该是:

week    reservations    total
week 1  4               10000.00
week 2  3               2000.00
week 3  2               100.00
week 4  5               25000.00

它应该显示一个月内的所有星期,无论有无保留的几周。



对于月度报告查询:

SELECT count(reservation_id) AS reservations,
YEAR(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) AS 'year',
MONTH(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) AS 'month',
SUM(payable) AS 'total'
FROM `reservation` 
 GROUP BY YEAR(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')),
MONTH(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) 
ORDER BY YEAR(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) ASC,
MONTH(DATE_FORMAT(STR_TO_DATE(arrival, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) ASC

这是查询的结果(每月)

reservations    year    month   total   
4               NULL    NULL    78500.00
1               2016    1       15300.00
1               2016    8       69300.00
1               2017    3       20400.00

再次,结果在第一行中为空:(我甚至不知道为什么。期望的结果应该是:

reservations    year    month     total     
1               2016    january   1000.00
0               2016    april     00.00
0               2016    january   1000.00
-----------up to december----------------
1               2016    december  1000.00

它还会显示没有预订的月份(未包含在预订表中)。

现在年度报告

SELECT YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) AS 'year',
COUNT(reservation_id) AS 'reservations',
SUM(payable) AS 'total'
FROM `reservation`
GROUP BY YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s'))
ORDER BY YEAR(DATE_FORMAT(STR_TO_DATE(`arrival`, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) ASC

查询结果:

 year   reservations    total   
NULL    4               78500.00
2016    2               84600.00
2017    1               20400.00

上面的结果似乎没问题。只有null部分和4和2的预留都是2016.我不知道为什么第一行为空,它没有对属于2016年(4和2)的数据进行分组。

注意:到达列是varchar格式,因为到达和离开来自使用datepicker(js)的用户输入。我使用str_to_date和date_format来格式化格式化时间戳的到达。

1 个答案:

答案 0 :(得分:0)

如果从str中提取的日期,时间或日期时间值是非法的,STR_TO_DATE()将返回NULL并产生警告。您是否检查过数据的一致性? MySQL是否会发出警告?

使用SHOW WARNINGS语句启用警告。