Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply columns and add the results?

Tags:

sql

sqlite

Assume that a table has two columns named "x" and "y" filled with numbers; for example, x contains the values (1, 2, 3), and y contains (5, 8, 20).

How can I calculate the sum of the products of the columns, i.e. (1 × 5) + (2 × 8) + (3 × 20) = 81?

like image 901
Marko Petričević Avatar asked Mar 25 '15 18:03

Marko Petričević


1 Answers

As Siyual notes in the comments, this should do the trick:

SELECT SUM(x * y) FROM Table

Here's a live demo on SQLize, just to confirm that this does work.

like image 128
Ilmari Karonen Avatar answered Oct 06 '22 03:10

Ilmari Karonen