Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with select statement 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi everyone,

I've a little problem with a select statement ... perhaps anyone can help me.

I've a statement that is similar to the statement below:

SELECT
table1.column1,
sum(<very:long_expression1>) bla1,
sum(<very:long_expression2>) bla2
FROM
... several tables ...
WHERE
... several contraints ...
GROUP BY
table1.column1

Now I would like to have the sum of the both returned sums:

SELECT
table1.column1,
sum(<very:long_expression1>) bla1,
sum(<very:long_expression2>) bla2,
(bla1 + bla2) bla3 -- here an error occures
FROM
... several tables ...
WHERE
... several contraints ...
GROUP BY
table1.column1

Does anyone know how to fix that?
 
create a view which assigns a column name to the very long expressions, then you can write

SELECT
table1.column1,
sum(bla1),
sum(bla2),
sum(bla1 + bla2)

otherwise, i think you will have to repeat the very long expressions where you currently are trying to use the aliases

rudy
 
You could try this:
SELECT
table1.column1,
sum(<very:long_expression1>) bla1,
sum(<very:long_expression2>) bla2,
sum(<very:long_expression2> + <very:long_expression2>) bla3 -- here an error occures
FROM
... several tables ...
WHERE
... several contraints ...
GROUP BY
table1.column1

good luck
web/sql developer
 
Alternatley, you may also try
Code:
Select column1
       bla1,
       bla2,
       bla1+bla2 as bla3
From   (
       SELECT table1.column1,
              sum(<very:long_expression1>) bla1,
              sum(<very:long_expression2>) bla2
       FROM
       ... several tables ...
       WHERE
       ... several contraints ...
       GROUP BY
             table1.column1)
This way, you only need to specify the long expressions once.




AA 8~)
 
Too bad I can't edit my own post, but to show the flexibility of the previous, consider this stretch of the imagination...
Code:
Select column1
       bla1,
       bla2,
       bla1+bla2 as bla3,
       bla1/bla2 as bla4,
       bla1*bla2 as bla5,
       bla1-bla2 as bla6,
       (bla1*3.14159)/(bla2*bla2) as bla7
       etc etc
From   (
       SELECT table1.column1,
              sum(<very:long_expression1>) bla1,
              sum(<very:long_expression2>) bla2
       FROM
       ... several tables ...
       WHERE
       ... several contraints ...
       GROUP BY
             table1.column1)
Again, showing you only need to build the complicated part of the expression once.

AA 8~)
 
Thank you all for your answers...

... but I think I've to go with the &quot;<very:long_expressions>&quot;.

The example I posted is only a small part of a very huge SQL statement. I tried to solved the problem by utilise your hints but there occured some not wanted side effects.

Thank you anyway for your efforts
 
yeah, &quot;not wanted side effects&quot; like what, exactly?

rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top