sql - Incorrect Summing Value -
i'm trying join table onto mapping table. mapping table has geographical data in , idea @ detail first i.e. postalcode , mapp, if postal code has not been provided @ zone:
id region country description zone county city postal_code 9324 australasia australia tasmania 70 western shore 7000 9325 australasia australia tasmania 70 western shore 7004 9326 australasia australia tasmania 70 western shore 7005 9327 australasia australia tasmania 70 western shore 7007 9328 australasia australia tasmania 70 western shore 7008 9329 australasia australia tasmania 70 western shore 7009 9330 australasia australia tasmania 70 western shore 7010 9331 australasia australia tasmania 70 western shore 7011 9332 australasia australia tasmania 70 western shore 7012 9333 australasia australia tasmania 70 eastern shore 7015 9334 australasia australia tasmania 70 eastern shore 7016 9335 australasia australia tasmania 70 western shore 7017 9336 australasia australia tasmania 70 eastern shore 7018 9337 australasia australia tasmania 70 eastern shore 7019 9338 australasia australia tasmania 70 eastern shore 7020 9339 australasia australia tasmania 70 eastern shore 7021 9340 australasia australia tasmania 70 eastern shore 7022 9341 australasia australia tasmania 70 eastern shore 7023 9342 australasia australia tasmania 70 eastern shore 7024 9343 australasia australia tasmania 70 sorell 7025 9344 australasia australia tasmania 70 sorell 7026 9345 australasia australia tasmania 70 sorell 7027 9346 australasia australia tasmania 70 western shore 7030 9347 australasia australia tasmania 70 kingston 7050 9348 australasia australia tasmania 70 kingston 7052 9349 australasia australia tasmania 70 kingston 7053 9350 australasia australia tasmania 70 kingston 7054 9351 australasia australia tasmania 70 kingston 7055
the idea comes zone , description joining dependent of data provided data i'm trying format is:
country state zone zip tiv australia null 70 null 1000000 australia null 70 null 1000000 australia null 70 null 1000000 australia null 70 null 1000000 australia null null 7023 100000
so answer looks this:
country zone name value australia 70 tasmania 4000000
however code have is:
select gr.country, gr.zone, gr.zone_desc name, sum(ta.tiv) [dbo].[tmp_format] ta inner join [dbo].[geores_mapping] gr on ta.country = gr.country , ta.zone = gr.zone ta.country = 'australia' group gr.country, gr.zone, gr.zone_desc, ta.tiv
and i'm getting is:
country zone name value australia 70 tasmania 112000000
it seems multiplying 4100000 28 different zone in mapping table? apologies being long winded i'm struggling
it not multiplying different zones, postal codes. have multiple rows per zone. perhaps solve problem:
select gr.country, gr.zone, gr.zone_desc name, sum(ta.tiv) [dbo].[tmp_format] ta inner join (select distinct country, zone, zone_desc [dbo].[geores_mapping] ) gr on ta.country = gr.country , ta.zone = gr.zone ta.country = 'australia' group gr.country, gr.zone, gr.zone_desc;
also, tiv
shouldn't in group by
.
Comments
Post a Comment