Skip to content
Advertisement

Can I multiply the output of a SQL query from two separate tables within the same query?

I am taking two values (A, B) from similar but different tables. E.g. A is the count(*) of Table R, but B is a complex calculation based off a slightly adapted table (we can call it S).

So I did this:

SELECT
(SELECT count(*)*60 FROM R) AS A,
[calculation for B] AS B
FROM R
WHERE
[modification to R to get S]

Not sure if this was the smartest way to do it (probably was not, I’m a new user).

Now I want to do some multiplications:

  • A*B
  • B-(A*0.75)
  • B-(A*0.8)
  • B-(A*0.85)

etc.

Is there a way to do this within the same query?

Thanks.

Advertisement

Answer

The simplest way,

SELECT A*B p1, B-(A*0.75) p2, B-(A*0.8) p3, B-(A*0.85) p4, ...
FROM (
 -- your original query producing columns A, B  ...
) t
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement