473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL question

Hi,

Can anyone help me in making the following SQL work ?
Basically I am trying to use a computed filed (CASE statement) in group by
clause and struggling
with syntax (new to DB2)

select A, CASE B WHEN 1 THEN 'AAA' WHEN 2 THEN 'BBB' ELSE 'ZZZ' END AS
D, sum(C)
from TEST group by A, D

-Thanks
Nov 12 '05 #1
9 8342
I forgot to mention..... I am having problems on m/f (OS/390) DB2 ver
7.x

The following syntax is working fine on Linux DB2 but not on m/f
DB2.......
select A,
CASE B WHEN 1 THEN 'AAA' WHEN 2 THEN 'BBB' ELSE 'ZZZ' END AS
D,
sum(C)
from TEST
group by A, CASE B WHEN 1 THEN 'AAA' WHEN 2 THEN 'BBB' ELSE 'ZZZ' END

Nov 12 '05 #2
Murty <ad***@comcast.net> wrote:
Hi,

Can anyone help me in making the following SQL work ?
Basically I am trying to use a computed filed (CASE statement) in group
by clause and struggling
with syntax (new to DB2)

select A, CASE B WHEN 1 THEN 'AAA' WHEN 2 THEN 'BBB' ELSE 'ZZZ' END
AS D, sum(C)
from TEST group by A, D


You must put the same expression in the GROUP BY clause and can't just refer
to the alias given to the column. Two options that I see right away:

SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d, SUM(c)
FROM test
GROUP BY a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d

SELECT a, d, SUM(c)
FROM ( SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d,
c
FROM test ) AS t(a, d, c)
GROUP BY a, d

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3
The first one is not working. The second option is working but somehow
it doesn't
seem to be a cleaner solution. We have tons of very lengthy & complex
queries
(converting from Oracle) and this syntax will further coplicate them.

Is there any way of tweaking the first option and making it work ?

Thanks

You must put the same expression in the GROUP BY clause and can't just refer to the alias given to the column. Two options that I see right away:

SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d, SUM(c)
FROM test
GROUP BY a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d

SELECT a, d, SUM(c)
FROM ( SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d,
c
FROM test ) AS t(a, d, c)
GROUP BY a, d

--
Knut Stolze
Information Integration
IBM Germany / University of Jena

Nov 12 '05 #4
The first one is not working. The second option is working but somehow
it doesn't
seem to be a cleaner solution. We have tons of very lengthy & complex
queries
(converting from Oracle) and this syntax will further coplicate them.

Is there any way of tweaking the first option and making it work ?

Thanks

You must put the same expression in the GROUP BY clause and can't just refer to the alias given to the column. Two options that I see right away:

SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d, SUM(c)
FROM test
GROUP BY a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d

SELECT a, d, SUM(c)
FROM ( SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d,
c
FROM test ) AS t(a, d, c)
GROUP BY a, d

--
Knut Stolze
Information Integration
IBM Germany / University of Jena

Nov 12 '05 #5
AK
have you tried wrapping option 2 in a view?
Nov 12 '05 #6
This is the restriction od DB2 UDB for z/OS Version 7.
Please see IBM Redbook "DB2 UDB for z/OS Version 8 Technical Preview"
http://w3.itso.ibm.com/itsoapps/Redb...light=0,DB2,V8

5.10 Expressions in GROUP BY
DB2 V7 does not allow expressions to be specified in the GROUP BY
clause which is supported by DB2 on the UNIX, Windows, and AS/400
platforms.
The current work-around is for the applications to code nested table
expressions or a view to first provide a result table with the
expression as a column of the result, then specify the column in the
GROUP BY clause.
The GROUP BY clause specifies an intermediate result table that
consists of a grouping of the rows of R. R is the result of the
previous clause of the subselect.
The first one is not working. The second option is working but somehow
it doesn't
seem to be a cleaner solution. We have tons of very lengthy & complex
queries
(converting from Oracle) and this syntax will further coplicate them.

Is there any way of tweaking the first option and making it work ?

Thanks

You must put the same expression in the GROUP BY clause and can't just

refer
to the alias given to the column. Two options that I see right away:

SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d, SUM(c)
FROM test
GROUP BY a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d

SELECT a, d, SUM(c)
FROM ( SELECT a,
CASE b
WHEN 1 THEN 'AAA'
WHEN 2 THEN 'BBB'
ELSE 'ZZZ'
END AS d,
c
FROM test ) AS t(a, d, c)
GROUP BY a, d

--
Knut Stolze
Information Integration
IBM Germany / University of Jena

Nov 12 '05 #7
> This is the restriction od DB2 UDB for z/OS Version 7.
Please see IBM Redbook "DB2 UDB for z/OS Version 8 Technical Preview"

http://w3.itso.ibm.com/itsoapps/Redb...light=0,DB2,V8

The w3-address is quite useless outside IBM.

Regards Rolf
Nov 12 '05 #8
> > This is the restriction od DB2 UDB for z/OS Version 7.
Please see IBM Redbook "DB2 UDB for z/OS Version 8 Technical Preview"

http://w3.itso.ibm.com/itsoapps/Redb...light=0,DB2,V8
The w3-address is quite useless outside IBM.

Regards Rolf


Link to Redbooks:
http://www.redbooks.ibm.com/

Most DB2 manuals and Redbooks are available on the IBM website.
Nov 12 '05 #9
> > This is the restriction od DB2 UDB for z/OS Version 7.
Please see IBM Redbook "DB2 UDB for z/OS Version 8 Technical Preview"


The w3-address is quite useless outside IBM.

Sorry. Please try this URL:
http://publib-b.boulder.ibm.com/Redb...6871.html?Open
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: love91501 | last post by:
I was wondering if anyone would be able to help me figure out how to make my Q010 have the ability to chose 2 answers, I'm hoping to stay away from checkboxes but I am willing to take in any advice....
6
semanticnotion
by: semanticnotion | last post by:
Hi sir i want to transform the data of one table into another through foreign key but the following error come to my browser Here is my code and data base structure. CREATE TABLE IF NOT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.