sql - Update query: Sum divided by Count between 2 different columns in 2 different tables -
i'd able sum financial data column on 1 table (containing both negative , positive values) , divide equally total number of accounts in table.
i tried several variations on nesting select statements, must not issue continue message "an aggregate may not appear in set list of update statement."
'cost' blank column insert values. it's decimal(18,2) type. 'financial' , 'types' tables each have acct # use join data; 'master' table has unrelated account numbers , doesn't share unifying data financial , type tables.
update mastertable set cost = (select sum(financialtable.jan) financialtable left join typestable on financialtable.acct2 = typestable.acct2) (typestable.type = 'overhead') , (financialtable.unit = 123)) / (select count(mastertable.acct1))
there 2 issues code in question. firstly has 1 many closing brackets. secondly,
select count(mastertable.acct1)
is not valid query in , of itself. if was, code free of syntax issues.
if want aggregate update statement, have use subqueries , aggregation in them. there. change left join inner join readability - inner join referencing 1 of fields of typestable in clause.
all of gives rise this sql-fiddle have set you.
finally though - don't think fixed code have made want to. setting cost field every field in mastertable - want do? suspect want join onto perhaps acct1 field? if can clarify question might answer want.
Comments
Post a Comment