SQL查询以每月计算总数作为列

时间:2013-10-04 12:44:17

标签: sql postgresql pivot

我被困在一个SQL查询上。我正在使用PostgreSQL。我需要获得所有州的每月总数。

table A 
--------------------------------------------------------
created    |    Name    | Agent_id  | Total
--------------------------------------------------------
3/14/2013  |    Harun   | 1A        |  5                
3/14/2013  |    Hardi   | 2A        | 20
4/14/2013  |    Nizar   | 3A        | 30
5/14/2013  |    moyes   | 4A        | 20

table B 
----------------------------
Agent_id| state_id
----------------------------
1A      | 1
2A      | 1 
3A      | 1
4A      | 2

table C 
----------------------------
state_id   |    State   
----------------------------
   1       |    Jakarta 
   2       |    Singapore
   3       |    Kuala lumpur

期望的结果:

-----------------------------------------------------------------------------------------------
No  |State          | Januari | February | March | April | Mei  ... December| Total
-----------------------------------------------------------------------------------------------
1   |Jakarta        |0        |0         |25     |  30   |  0   ...         | 55
2   |Singapore      |0        |0         | 0     |   0   | 20   ...         | 20
3   |Kuala Lumpur   |0        |0         | 0     |   0   |  0   ...         |  0

3 个答案:

答案 0 :(得分:8)

要使表A / B中没有数据的所有州都必须使用OUTER JOIN

完成@bma回答

  select  
  no,
  state,
  sum(case when month = 1 then total else 0 end) as januari,
  sum(case when month = 2 then total else 0 end) as februari,
  sum(case when month = 3 then total else 0 end) as mars,
  sum(case when month = 4 then total else 0 end) as april,
  sum(case when month = 5 then total else 0 end) as may,
  sum(case when month = 6 then total else 0 end) as juni,
  sum(case when month = 7 then total else 0 end) as juli,
  sum(case when month = 8 then total else 0 end) as august,
  sum(case when month = 9 then total else 0 end) as september,
  sum(case when month = 10 then total else 0 end) as october,
  sum(case when month = 11 then total else 0 end) as november,
  sum(case when month = 12 then total else 0 end) as december,
  sum(coalesce(total,0)) as total
from (
    select  
      c.state_id as no,
      extract(month from created) as month,
      state,
      sum(total) as total
    from tablec c 
      left join tableb b on ( b.state_id = c.state_id)
      left join tablea a on ( a.agent_id = b.agent_id)
    group by c.state_id,state,month
    ) sales
    group by no,state;

SQL Fiddle demo

答案 1 :(得分:0)

其实我对postgres sql不太了解这是一个尝试看看是否有效

试试这个

Select EXTRACT(MONTH FROM TIMESTAMP table A.created) , 
table C.State , SUM(Total) From table A , table B , table C 
Where table A.Agent_id = table B.Agent_id
And  table B.state_id = table C.state_id
Group by table C.State , EXTRACT(MONTH FROM TIMESTAMP table A.created);

答案 2 :(得分:0)

以下内容应该会为您提供结果,例如您的示例结果。我不确定“无”栏目是什么。此查询未经测试。

select  state,
        sum(case when mm = 1 then total else 0 end) as jan,
        sum(case when mm = 2 then total else 0 end) as feb,
        sum(case when mm = 3 then total else 0 end) as mar,
        sum(case when mm = 4 then total else 0 end) as apr,
        sum(case when mm = 5 then total else 0 end) as may,
        sum(case when mm = 6 then total else 0 end) as jun,
        sum(case when mm = 7 then total else 0 end) as jul,
        sum(case when mm = 8 then total else 0 end) as aug,
        sum(case when mm = 9 then total else 0 end) as sep,
        sum(case when mm = 10 then total else 0 end) as oct,
        sum(case when mm = 11 then total else 0 end) as nov,
        sum(case when mm = 12 then total else 0 end) as dec,
        sum(total) as total
from (
    select  extract(month from created) as mm,
            state,
            sum(total) as total
    from table_a
    group by state,mm
    ) s
group by state;