【问题标题】:Creating a Summary Tracking Report from Details从详细信息创建摘要跟踪报告
【发布时间】:2011-09-12 21:55:21
【问题描述】:

我需要根据详细数据创建一份汇总的月度跟踪报告。数据样本如下:

公司 |国家 |加入日期
A公司|美国 | 2011 年 1 月 1 日
B公司|爱尔兰 | 2011 年 5 月 5 日
C公司|意大利 | 2011 年 7 月 11 日
D公司|德国 | 2011 年 6 月 14 日

我需要创建一份报告,提供给定国家/地区在给定月份加入的成员数量,格式如下:

国家 1 |总计(一月)|总计(二月)|总计(三月等)|总和(每月总计)
国家 2 |总计(一月)|总计(二月)|总计(三月等)|总和(每月总计)
国家 3 |总计(一月)|总计(二月)|总计(三月等)|总和(每月总计)

在每列的底部,我需要所有国家/地区每个月的总和。此外,此报告需要是滚动报告,因此当用户生成它时,它将提供最新信息。

【问题讨论】:

    标签: sql sql-server-2005


    【解决方案1】:

    您将要分两部分执行此操作。

    首先,您需要从数据库中获取相关数据。可能最好通过如下语句来完成:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多