2

我正在写一份报告,显示自去年以来每个月的代码总量。

目前,如果我只计算过去一年中的所有代码,那么我的结果集将如下所示

name    | code  | total   |  date
build1    x1      10        04-2013
build1    x50     60        05-2013
build1    x1      80        06-2013
build1    x90     450       07-2013

我能够转置所有的行,所以所有的列都是月份,下面是总数。我的更新结果现在看起来像这样

name    |  code | apl |  may | jun | jul
build1     x1      10    0     80     0
build1     x50     0     60     0     0
build1     x90     0     0      0     450   

上面的代码是我正在寻找的结果,但我现在想要做的是在当月之前订购所有东西,然后从当月起倒退一年。

因此,如果当前月份是 7 月,那么我的结果集将像这样排序

name    |  code | jul |  jun | may | apl
build1     x1      0     80     0     10
build1     x50     0      0     60     0
build1     x90     450    0     0      0 

我遇到的问题是我使用别名作为月份名称。而且您无法从别名中获取月份。此外,据我所知,别名是静态的,因此一旦设置它们就无法更改。将月份作为列名的唯一方法是从数据集中提取它。但是当我将行转换为列时,我必须使用别名,因为我使用案例语句来获取每个月的所有总数。

编辑:对不起,postgresql 版本是 8.4,这是我到目前为止的查询

SELECT 


pname,
code,
SUM(totaljanurary)  AS "Janurary",
SUM(totalfebruary)  AS "February",
SUM(totalmarch)     AS "March",
SUM(totalapril)     AS "April",
SUM(totalmay)       AS "May",
SUM(totaljune)      AS "June",
SUM(totaljuly)      AS "July",
SUM(totalaugust)    AS "August",
SUM(totalseptember) AS "September",
SUM(totaloctober)   AS "October",
SUM(totalnovember)  AS "November",
SUM(totaldecember)  AS "December"

FROM(

SELECT 

    pname,
    code,
    SUM(case when extract (month FROM checked_date)=01 then total else 0 end) AS totaljanurary,
    SUM(case when extract (month FROM checked_date)=02 then total else 0 end) AS totalfebruary,
    SUM(case when extract (month FROM checked_date)=03 then total else 0 end) AS totalmarch,
    SUM(case when extract (month FROM checked_date)=04 then total else 0 end) AS totalapril,
    SUM(case when extract (month FROM checked_date)=05 then total else 0 end) AS totalmay,
    SUM(case when extract (month FROM checked_date)=06 then total else 0 end) AS totaljune,
    SUM(case when extract (month FROM checked_date)=07 then total else 0 end) AS totaljuly,
    SUM(case when extract (month FROM checked_date)=08 then total else 0 end) AS totalaugust,
    SUM(case when extract (month FROM checked_date)=09 then total else 0 end) AS totalseptember,
    SUM(case when extract (month FROM checked_date)=10 then total else 0 end) AS totaloctober,
    SUM(case when extract (month FROM checked_date)=11 then total else 0 end) AS totalnovember,
    SUM(case when extract (month FROM checked_date)=12 then total else 0 end) AS totaldecember



    FROM (

        --START HERE
        SELECT

            pname,
            code,
            COUNT(code)AS total,
            date_trunc('month',checked_date)::date AS checked_date


            FROM table1

            AND checked_date >= current_date-365
            AND checked_date <= current_date


            GROUP BY pname, code, date_trunc('month',checked_date)
    )T1
    GROUP BY pname, code, date_trunc('month',checked_date)

)T2
GROUP BY pname, code
ORDER BY pname, code
4

1 回答 1

1

您想要crosstab()的,由附加模块提供tablefunc

假设“日期”是类型date(应该是)。
其他详细信息取决于您忘记提供的详细信息。

SELECT * FROM crosstab(
     $$SELECT name, code, to_char("date", 'mon'), total
       FROM   tbl
       WHERE  "date" <  now()
       AND    "date" >= now() - interval '1 year'
       ORDER  BY name, extract(month from now()) DESC$$

    ,$$VALUES
      ('dec'::text), ('nov'), ('oct'), ('sep'), ('aug'), ('jul')
    , ('jun'),       ('may'), ('apr'), ('mar'), ('feb'), ('jan')$$
   )
AS ct (name text, code text
   , dec int, nov int, oct int, sep int, aug int, jul int
   , jun int, may int, apr int, mar int, feb int, jan int);

有关其他说明,请参阅此密切相关的答案:
Sum by month and put months as columns

您不应该date用作标识符,它是一个保留字。我双引号了。
您也不应该使用非描述性名称name

于 2013-07-09T16:21:19.813 回答