Subject | Sum as last record in query result? |
---|---|
Author | Joe Martinez |
Post date | 2002-08-27T20:27:15Z |
I'm constructing a query as follows:
select A.field1, A.field2, B.fieldX, B.fieldY, A.field2 * B.fieldY as
Total
from table1 A
left outer join table 2 B on A.field3=B.fieldZ
Ok. That's fine.
Now, I want to add one more row to the end of the result, that has the
first 4 fields blank/null, and the last column be the sum of the Total
column from the above query.
I figured that I could do it with a UNION. I can create a second query:
select sum(A.field2 * B.fieldY) as Total
from table1 A
left outer join table 2 B on A.field3=B.fieldZ
The problem is that this only returns one column, and you can't do a
UNION unless both Selects return the same number of columns. I can't
just pick 4 other fields because then I'd have to do a Group By, and
those columns would have different values, and I'd get more than one
row.
Can anyone think of a way to do this?
The reason that I need to do this, is that I'm feeding the result into a
report generator that only allows one query, so I need the individual
values and the sum to come out as the result of a single query.
-Joe
select A.field1, A.field2, B.fieldX, B.fieldY, A.field2 * B.fieldY as
Total
from table1 A
left outer join table 2 B on A.field3=B.fieldZ
Ok. That's fine.
Now, I want to add one more row to the end of the result, that has the
first 4 fields blank/null, and the last column be the sum of the Total
column from the above query.
I figured that I could do it with a UNION. I can create a second query:
select sum(A.field2 * B.fieldY) as Total
from table1 A
left outer join table 2 B on A.field3=B.fieldZ
The problem is that this only returns one column, and you can't do a
UNION unless both Selects return the same number of columns. I can't
just pick 4 other fields because then I'd have to do a Group By, and
those columns would have different values, and I'd get more than one
row.
Can anyone think of a way to do this?
The reason that I need to do this, is that I'm feeding the result into a
report generator that only allows one query, so I need the individual
values and the sum to come out as the result of a single query.
-Joe