sql - Sum IF Query for Multiple Tables -


i have table every month has column need add. need make query show in 1 table. first thing did union tables unique countries. how configure ms access query started produce output need?

example tables:

table 1: january 2014

 | country | headcount |  |   usa   |    100    |  |  china  |     50    |  |  russia |     75    |  |   usa   |     10    | 

table 2: february 2014

 | country | headcount |  |   usa   |    100    |  |  china  |     50    |  |  mexico |     100   |  |  china  |     75    | 

table 3: countries

 | country |  |   usa   |  |  china  |  |  mexico |  |  russia | 

desired output:

 | country | january   |  february  |  |   usa   |    110    |  100       |  |  china  |    50     |  125       |  |  russia |     75    |  0         |  |  mexico |     0     |  100       | 

this query far doesn't seem work..

select a.[country] country,  sum(iif(a.[country] = b.[country], b.[headcount],0)) january  sum(iif(a.[country] = c.[country], c.[headcount],0)) february [all countries] a,  (select [headcount], [country] [january 2014]) b, (select [headcount], [country] [february 2014]) c group a.[country] 

this approach should work -- (edited)

    select a.[country] country,  sum(iif(a.[month] = "jan", b.[headcount],0)) january  sum(iif(a.[month] = "feb", c.[headcount],0)) february      ( select [headcount], [country], "jan" [month] [january 2014]  union select [headcount], [country], 'feb' [month]  [february 2014] union ... ) group a.[country] 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -