0

I'm dealing with queries that manages a lot of data consolidation and I'm having to repeat large blocks of code to get the desired result.
I'm wondering if that is any way to make it simple.

My queries looks like this:

SELECT
  (A + B) AS C,
  (D + E) AS F,
  (G + H) AS I,
  (A + B) * (D + E) AS total1,
  (D + E) * (G + H) AS total2,
  (A + B) * (G + H) AS total3,
  (A + B) * (D + E) * (G + H) AS total
FROM table

Can I somehow declare each part of the expression and make it something like this?

SELECT
  (A + B) AS C,
  (D + E) AS F,
  (G + H) AS I,
  C * F AS total1,
  F * I AS total2,
  C * I AS total3,
  C * F * I AS total
FROM table

Maybe it seems silly, but each sub-expression, on the real life, includes a lot of CASEs and aggragate functions and my codes are getting complex and confuse.

Since I have lots of code pieces that I repeat more than once, I would like to make it simpler.

If anyvody knows how to help me I appreciate!!!

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by hollystyles
0

You can do it with a sub query:

SELECT 
	C,
	F,
	I,
  	C * F AS total1,
  	F * I AS total2,
  	C * I AS total3,
 	C * F * I AS total 
FROM (
	SELECT
 		(A + B) AS C,
		(D + E) AS F,
		(G + H) AS I
	FROM 
		table 
	) t
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.