2013-10-04 93 views
2

我困在SQL查询中。我正在使用PostgreSQL。我需要得到所有州的每个月的总和。用于计算每月总计的SQL查询作为列

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 

回答

7

有没有数据的所有状态表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

+0

感谢..它的作品! !实际上我从来没有使用过提取功能。它真的帮助我! – muhnizar

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); 
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;