2016-03-17 343 views
1

我正在設計一個需要生成報告的網站。該報告必須生成詳細交易,並按天交易分組的總交易已完成。數據表格式如下所示。使用MySQL生成自定義報告

_______________________________________ 
 

 
    |Name | Check in Date | Chec out Date | 
 
    --------------------------------------- 
 
    |  |    |    | 
 
    |  |    |    | 
 
-------------------------------------------

我希望報告是在由見下表格式。

_______________________________________ 
 

 
    |Name | Check in Date | Chec out Date | 
 
    --------------------------------------- 
 
    |  |    |    | 
 
    |  |    |    | 
 
    _______________________________________ 
 

 
    | Total for Day 1 |     | 
 
    --------------------------------------- 
 
    |  |    |    | 
 
    |  |    |    | 
 
    _______________________________________ 
 
    | Total for Day 2 |     | 
 
    ---------------------------------------

當我運行下面

SELECT check_in_time , chec_out_time , COUNT(check_in_time) FROM 
 
table_name GROUP BY check_in_time ORDER BY check_in_time DESC

SQL查詢

然後我得到以下結果。

 _______________________________________________________________ 
 
    
 
     |Name | Check in Date | Chec out Date | COUNT(check in time) | 
 
     -------------------------------------------------------------- 
 
     |  |    |    | 
 
     |  |    |    | 2     | 
 
     ________________________________________________________________ 
 

 
     |  |    |    | 
 
     |  |    |    |  5     | 
 
     ________________________________________________________________ 
 

 
     |  |    |    | 
 
     |  |    |    |   3   | 
 
     ________________________________________________________________ 
 

 
    

其實我是想在一個新行中的行數和交易總額呈現了一天每一筆交易。

回答

0

我使用mysql關鍵字ROLLUP來獲得所需的結果。下面是sql腳本。

SELECT check_in_time , chec_out_time , COUNT(check_in_time) FROM 
 
table_name GROUP BY check_in_time DESC , id WITH ROLLUP;