I've done my best to strip down code to bare minimum and provided concise explanation of problem (I hope)
I have a db scheme as indicated below.
def a_method(info_hash)
...
db.execute2 "create table if not exists Table1(Id INTEGER PRIMARY KEY, Item TEXT, Amount FLOAT, Type TEXT, AvgInc FLOAT, AvgX FLOAT, Total FLOAT)"
...
end
This creates my schema. Once I populate the database, I run the following method which gives me the #{@total}
value grouped by my Type
column:
def get_total(info_hash)
...
get_amt = db.execute2 "SELECT sum(Amount) from Table1 WHERE Type = :Type", info_hash[:category]
puts "foo"
db.execute2 "UPDATE Table1 SET Total = :Total WHERE Type = :Type", get_amt[1][0], info_hash[:category]
@total=get_amt[1][0]
...
@total
end
I want to run the following method to compute an Item
average in the event the item did not exist. Basically the impact each Item
has on the Type
average.
Please see the in code comment for where I think my logic is faulty
def method_excluding(info_hash)
...
get_num = db.execute2 "SELECT Amount from Table1 WHERE Type = :Type", info_hash[:category]
i = 0
get_num.each do |item|
#purpose of block to compute an average value for Type as if #{item} did not exist. So: the average value for Type EXLCUDING #{item}
if i == 1
@avgx = (@total - get_num[1][0]) / i
elsif i > 1
@avgx = (@total - get_num[i][0]) / (i - 1)
end
i+=1
db.execute2 "UPDATE Table1 SET AvgX = :AvgX WHERE Type = :Type", @avgx, info_hash[:category]
...
end
Please advise on how I can get to my desired outcome.