Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 7th, 2006, 09:15 PM
bsimmons01@gmail.com
Guest
 
Posts: n/a
Default Easy Group By Question (I think/hope)

Hi All,

I'm completely green to DB2, so please pardon my ignorance if this is
an extremely easy question.

I'm converting some queries in our application and it's choking on the
Group-By clause.

Here's the query (part of it):
SELECT a.org_lc || b.oros_rsrc || 'M' as ref_no, b.activity_desc,
sum(a.org_bud_amt)
FROM ab.vbudget_r01 a, ab.vabc_rsrc_xref b,
WHERE a.gl_yr = 2005 and a.id = b.id
GROUP BY ref_no, b.activity_desc

It doesn't like the ref_no in the Group-By and I tried "a.org_lc ||
b.oros_rsrc || 'M' " in there as well, and it didn't like that either.
I tried GROUP 1, 2 and it didn't like that either.

Anybody able to help me out?

Thanks,
Brian

  #2  
Old March 7th, 2006, 09:35 PM
johnl
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

Use a "nested table expression"

select nte.group_of_cols, nte.activity_desc, sum(nte.org_bud_amt) from
(SELECT a.org_lc || b.oros_rsrc || 'M' as group_of_cols ,
b.activity_desc, a.org_bud_amt
FROM ab.vbudget_r01 a, ab.vabc_rsrc_xref b,
WHERE a.gl_yr = 2005 and a.id = b.id ) as nte
GROUP BY nte.group_of_cols, nte.activity_desc

  #3  
Old March 7th, 2006, 09:35 PM
Serge Rielau
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

bsimmons01@gmail.com wrote:[color=blue]
> Hi All,
>
> I'm completely green to DB2, so please pardon my ignorance if this is
> an extremely easy question.[/color]
Don't worry. We'll make you blue.[color=blue]
> Here's the query (part of it):
> SELECT a.org_lc || b.oros_rsrc || 'M' as ref_no, b.activity_desc,
> sum(a.org_bud_amt)
> FROM ab.vbudget_r01 a, ab.vabc_rsrc_xref b,
> WHERE a.gl_yr = 2005 and a.id = b.id
> GROUP BY ref_no, b.activity_desc[/color]
Makes sense ref_no is not available within the query.[color=blue]
>
> It doesn't like the ref_no in the Group-By and I tried "a.org_lc ||
> b.oros_rsrc || 'M' " in there as well, and it didn't like that either.[/color]
That's odd. what is the error mesage you get and which version/platform
of DB2 are you on:
Here is what I get (DB2 Viper, but should work since at least DB2 UDB V5
for LUW):
db2 => create table TTT(c1 varchar(10), c2 varchar(20), c3 INT)@
DB20000I The SQL command completed successfully.
db2 => select c1 || c2 || 'M', c3 FROM TTT GROUP BY c1 || c2 || 'M', c3@

1 C3
------------------------------- -----------

0 record(s) selected.[color=blue]
> I tried GROUP 1, 2 and it didn't like that either.[/color]
That's correct. Allowing column numbers in ORDER BY was a mistake to
begin with.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
  #4  
Old March 8th, 2006, 07:15 AM
Knut Stolze
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

johnl wrote:
[color=blue]
> Use a "nested table expression"
>
> select nte.group_of_cols, nte.activity_desc, sum(nte.org_bud_amt) from
> (SELECT a.org_lc || b.oros_rsrc || 'M' as group_of_cols ,
> b.activity_desc, a.org_bud_amt
> FROM ab.vbudget_r01 a, ab.vabc_rsrc_xref b,
> WHERE a.gl_yr = 2005 and a.id = b.id ) as nte
> GROUP BY nte.group_of_cols, nte.activity_desc[/color]

That's a "subselect" (in case someone wants to look things up in the
manual).

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
  #5  
Old March 8th, 2006, 02:35 PM
bsimmons01@gmail.com
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

Hi Serge,

Thanks for your input, we're on:
DB2 UDB v7 for z/OS

The exact error that I'm getting when I run the query with "a.org_lc ||
b.oros_rsrc || 'M' " as the first Group By parameter:
SQLSTATE = 42601
[IBM][CLI Driver][DB2] SQL0104N
An unexpected token "||" was found following "". Expected tokens may
include:
"FOR WITH FETCH ORDER UNION EXCEPT QUERYNO OPTIMIZE ". SQLSTATE=42601

Any thoughts?

Thanks again,
Brian

  #6  
Old March 8th, 2006, 03:16 PM
Brian Tkatch
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

>and I tried "a.org_lc || b.oros_rsrc || 'M' "

No reason for the M, since it's a constant, and no reason for the ||
since GROUPing with it or without it, is the same thing.

Try:

SELECT a.org_lc || b.oros_rsrc || 'M' as ref_no, b.activity_desc,
sum(a.org_bud_amt)
FROM ab.vbudget_r01 a, ab.vabc_rsrc_xref b,
WHERE a.gl_yr = 2005 and a.id = b.id
GROUP BY a.org_lc, b.oros_rsrc, b.activity_desc


B.

  #7  
Old March 8th, 2006, 03:16 PM
Brian Tkatch
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

>Don't worry. We'll make you blue.

With all the bad documentation? :)

Oh, oh, that world-domination thing...

B.

  #8  
Old March 8th, 2006, 03:25 PM
Serge Rielau
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

bsimmons01@gmail.com wrote:[color=blue]
> Hi Serge,
>
> Thanks for your input, we're on:
> DB2 UDB v7 for z/OS[/color]
I see... There is a reason why DB2 V8 for zOS was such a big release.
Push the expression into a subquery:
SELECT x, y FROM (SELECT c(1 + c2) as x, y FROM T) AS S GROUP BY x, y

That should do it.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
  #9  
Old March 9th, 2006, 01:15 PM
johnl
Guest
 
Posts: n/a
Default Re: Easy Group By Question (I think/hope)

I think it is a "nested table expression". This is copied straight
from the Information Center for Z/OS V7 website...

Nested Table Expressions
A nested table expression is a temporary view where the definition
is nested (defined directly) in the FROM clause of the main query.

The following query uses a nested table expression to find the
average total pay, education level and year of hire, for those with an
education level greater than 16:


SELECT EDLEVEL, HIREYEAR, DECIMAL(AVG(TOTAL_PAY),7,2)
FROM (SELECT EDLEVEL, YEAR(HIREDATE) AS HIREYEAR,
SALARY+BONUS+COMM AS TOTAL_PAY
FROM EMPLOYEE
WHERE EDLEVEL > 16) AS PAY_LEVEL
GROUP BY EDLEVEL, HIREYEAR
ORDER BY EDLEVEL, HIREYEAR

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles