|
|
Question : left outer join not working
|
|
this is the query i am trying to build: ===== select distinct grid.distrib, grid2.FiscYear, grid2.Quarter, grid2.Region, grid2.approved, count(grid2.Approved) as gridcount from grid left outer join grid as grid2 on grid.distrib = grid2.distrib where grid2.FiscYear = 2006 and grid2.Quarter = 2 and grid2.region = '1' group by grid.distrib,grid2.FiscYear,grid2.Quarter,grid2.Region,grid2.approved order by grid.distrib ------------------ RTE 2006 2 1 0 1263 =====
there are 2 unique records in the distrib column that i would like to see whether there are matching records in the join or not. ===== select distinct distrib from grid ------------------ HSE RTE =====
same issue with this query. i am only getting matching records like an inner join, but i am specifying a left outer. ===== select distinct grid.distrib, grid2.FiscYear, grid2.Quarter, grid2.Region, grid2.approved /*, count(grid2.Approved) as gridcount */ from grid left outer join grid as grid2 on grid.distrib = grid2.distrib where grid2.FiscYear = 2006 ------------------ HSE 2006 1 m 0 HSE 2006 2 m 0 HSE 2006 3 m 0 HSE 2006 4 m 0 RTE 2006 1 m 0 RTE 2006 2 1 0 RTE 2006 2 m 1 RTE 2006 3 m 0 RTE 2006 4 m 0 =====
it seems like the where clause is filtering on the left side of the join as well, even though i have specified the right side table for all the filters. i am joining the tables on the distrib column because thats the only similarity i would like to have between the tables. i have never had an issue like this calling from different tables. is this because i am trying to join the same table to itself?
|
Answer : left outer join not working
|
|
grid.distrib is not unique so the join brings back many more records than you expect hence your 1263 value I'd be tempted to rewrite this completly but irst you could try count(distinct grid2.Approved) as gridcount
otherwise I'd go for
select distinct grid3.distrib, grid2.FiscYear, grid2.Quarter, grid2.Region, grid2.approved , count(grid2.Approved) as gridcount from (select distinct grid.distrib from grid) as grid3
left join (select * from grid where FiscYear = 2006 and Quarter = 2 and region = '1') as grid2 on grid3.distrib = grid2.distrib group by grid3.distrib,grid2.FiscYear,grid2.Quarter,grid2.Region,grid2.approved order by grid3.distrib
I think that should cut down your duplicates.
any help?
|
|
|
|
|