-3

My data set has a weight column and a variable of interest var1, for which I need to form a weighted average of all rows that satisfy either var1=1 and var1=2. Unfortunately I need to do this in SQL, where I have very limited knowledge. To compute the weighted average of all rows, I would write SUM(var1 * weight)/SUM(weight). But how can I do this computation for all rows where var1 IN (1,2)?

user239092
  • 29
  • 2
  • Show us some sample table data and the expected result - all as formatted text (not images.) [mcve]. – jarlh Dec 22 '21 at 16:13

1 Answers1

0

It seems like what you're looking for would be the following:

SELECT SUM(var1 * weight) / SUM(weight) 
FROM sample 
WHERE var1 in (1,2)

I created a dbfiddle where you can go and check it out.

https://www.db-fiddle.com/f/qytaC3augXijw956ReuYqb/1

DonkeyKongII
  • 431
  • 2
  • 8