469,602 Members | 1,826 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

How Do You DO This in DB2

I have been using oracle for quite some time now, but I am switching
gears and am working in DB2.

Here is what I want to do. I want to sum the results of a query which
is unioning two answer set together. In Oracle, I could accomplish
this by writing the following:

Select Fielda, Fieldb, Sum(Fieldc), Sum(Fieldd)
From
(
My Union Query.....
)
Group by Fielda, FieldB

DB2 does not seem to lik this....How do you do this in DB2?

Jul 3 '06 #1
2 1129

OldButLearning wrote:
Select Fielda, Fieldb, Sum(Fieldc), Sum(Fieldd)
From
(
My Union Query.....
)
Group by Fielda, FieldB

DB2 does not seem to lik this....How do you do this in DB2?
That works in DB2. What you need to do is name the sub-query (alias
it). So:

Select Fielda, Fieldb, Sum(Fieldc), Sum(Fieldd)
From
(
My Union Query.....
) subquery
Group by Fielda, FieldB

(note - any name would work instead of subquery.)

-Chris

Jul 3 '06 #2
ChrisC wrote:
OldButLearning wrote:
>Select Fielda, Fieldb, Sum(Fieldc), Sum(Fieldd)
From
(
My Union Query.....
)
Group by Fielda, FieldB

DB2 does not seem to lik this....How do you do this in DB2?

That works in DB2. What you need to do is name the sub-query (alias
it). So:

Select Fielda, Fieldb, Sum(Fieldc), Sum(Fieldd)
From
(
My Union Query.....
) subquery
Group by Fielda, FieldB

(note - any name would work instead of subquery.)
In addition make sure the columns out of the UNION [ALL] are named.
that either the columns in each branch agree on the name (not sure about
Oracle semantics here). You can also extend Chris' suggestion to
subquery(field1, ....)

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by James | last post: by
5 posts views Thread by Scott D | last post: by
2 posts views Thread by Nick | last post: by
2 posts views Thread by Alexander Ross | last post: by
reply views Thread by Dan Foley | last post: by
5 posts views Thread by Lee Redeem | last post: by
5 posts views Thread by christopher vogt | last post: by
6 posts views Thread by Phil Powell | last post: by
1 post views Thread by Michel | last post: by
11 posts views Thread by Maciej Nadolski | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.