sql - How do I properly join to do aggregation on the same column twice? -


grain of table similar state_id | county_id | city_id | eventtype. eventtype binary; equal 1 or 2. want group 3 columns , see aggregate sums of eventtypes equals 1 , 2. how this?

when inner join of

select *  (state_id, county_id, city_id, sum(eventtype) views select  poptable eventtype = 1  group state_id, county_id, city_id) l inner join (state_id, county_id, city_id, sum(eventtype) passes select  poptable eventtype = 2 group state_id, county_id, city_id) r on l.state_id = r.state_id , l.county_id = r.county_id , l.city_id = r.city_id 

i 500 rows. if full outer join, 3000 rows. understand there missing combinations, how them appear together?

i think want conditional aggregation:

select state_id, county_id, city_id,        sum(case when eventtype = 1 1 else 0 end) views_1,        sum(case when eventtype = 2 1 else 0 end) views_2 poptable group state_id, county_id, city_id; 

i'm not sure why doing sum(eventtype). seems strange want "1" summed in first case , "2" in second.


Comments