2

I am stuck on a SQL query. I am using PostgreSQL. I need to get the total for each month for all states.

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

DESIRED RESULT:

-----------------------------------------------------------------------------------------------
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
4

3 回答 3

8

to have all state with no data in table A / B you have to use OUTER JOIN

to complete @bma answer

  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

于 2013-10-04T14:17:24.297 回答
0

Actually i do not know much about postgres sql this is a try see if this works

try this

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);
于 2013-10-04T13:01:58.537 回答
0

Something like the following should give you results like your sample results. I'm not sure what the "No" column was though. This query is untested.

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;
于 2013-10-04T13:50:40.370 回答