2011-09-12 96 views
0

我需要根據詳細數據創建彙總每月追蹤報告。樣本數據如下:根據詳細信息創建彙總跟蹤報告

公司|國家|加入日期
公司A | USA | 1/1/2011
公司B |愛爾蘭| 5/5/2011
公司C |意大利| 7/11/2011
公司D |德國| 6/14/2011

我需要創建一份報告,給我,在給定月份加入來自某一特定國家成員的數量,在下面的格式:

國家1 |總計(一月)|總計(二月)|總計(三月等)|總和(月總計)
國家2 |總計(一月)|總計(二月)|總計(三月等)|總和(每月總計)
國家3 |總計(一月)|總計(二月)|總計(三月等)|總和(每月總計)

在每列的底部,我需要每個月的所有國家的總和。此外,此報告需要是滾動報告,因此當用戶生成報告時,報告將提供最新的信息。

回答

0

您將要分兩部分來完成此操作。

首先,您將要從數據庫中獲取相關數據。也許最好的一份聲明中實現,例如:

SELECT country, year(join_date) as year, month(join_date) as month, count(*) 
FROM trackingTable 
WHERE join_date between :start_date and :end_date 
GROUP BY country, year(join_date), month(join_date) 
ORDER BY country, year, month 

爲您給出的數據,這將產生下面的示例:

Country Year Month Count 
================================== 
Germany 2011 6  1 
Ireland 2011 5  1 
Italy  2011 7  1 
USA  2011 1  1 

二(我假設你正在創建/格式化用外部語言報告),順序讀取數據。鑑於ORDER BY,每個國家將是一個單獨的序列。您需要的僞代碼:

for each row in set { 
    if different country { 
     total up row, start new line 
    } 
    add month cell to column; 
    add month cell to month grand total; 
} 
total up grand total row, print/save