我每天,每週,每月和每年報告。所有的數據將來自一個表,該表是這樣的:
表名:預訂每日,每週,每月和每年報告
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
結果上面似乎確定。只有空白部分和4和2的保留都是2016年。我不知道爲什麼第一行爲空,並且它沒有對屬於2016年(4和2)的數據進行分組。
注意:到達列採用varchar格式,因爲到達和離開來自使用日期選擇器(js)的用戶輸入。我使用str_to_date和date_format來格式化到達時間戳。
請以日期存儲日期。然後回到我們身邊。 – Strawberry
當我從varchar更改爲日期或甚至datetime數據類型時,即時獲取00/00/00值 – silver01