Tuesday, 15 June 2010

sql - SUM() from a multi-part JOIN -



sql - SUM() from a multi-part JOIN -

select sum(t.optlevel) + sum(o.reqlevel1) + sum(b.noptvalue) _inventory left bring together _items t on t.id64 = i.itemid left bring together _refobjcommon o on o.id = t.refitemid left outer bring together _bindingoptionwithitem b on b.nitemdbid = i.itemid i.charid = 7843 , i.slot between 0 , 12 , i.itemid != 0

i'm having problem query, not quite experienced joins be.

t.optlevel >= 0 _items , row there

o.reqlevel1 between 1 , 101 _refobjcommon , row there

however, b.noptvalue _bindingoptionwithitem either null, 1, or 2 row not there... when b.noptvalue = 1 or 2 nowadays in 1 of 12 row results ( i.slot between 0 , 12 ) script runs perfectly: sum total if b.noptvalue returns null in 12 row results sum gets returned null whole query.

i know there simple solution, cannot find it.

without understanding question, there's cool function coalesce(), returns first of parameters not null.

select coalesce(sum(t.optlevel), 0) + coalesce(....

sql join sum

No comments:

Post a Comment