question

itsj88 avatar image
itsj88 asked

Dividing 2 sums created from 2 different variable values

In the queries below, I am trying to get the value of the first query divided by the second query...all in one query statement. Any thoughts? Thanks: SELECT SUM(COLUMN 1) FROM TABLE 1 WHERE (COLUMN 2 = A) SELECT SUM(COLUMN 1) FROM TABLE 1 WHERE (COLUMN 2 = B)
sum
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
You can use a simple case statement to partition the data select sum(case when column2 = 'A' then column1 else 0 end), sum(case when column2 = 'B' then column1 else 0 end), sum(case when column2 = 'A' then column1 else 0 end)/ sum(case when column2 = 'B' then column1 else 0 end) from @Table1 ------ Edit: If the base queries re not as simple as sums then you can do Select (Query1) as Query1, (Query2) as Query2, (Query1)/(Query2)
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

itsj88 avatar image itsj88 commented ·
That got me in the right direction, but I think I worded it wrong. Basically I want query1 divided by query2, but in one select statement. Would that be the same answer? Thanks.
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Added an edit above.....
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.