473,396 Members | 1,804 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,396 software developers and data experts.

Simple question on case statements and aggregating data

Hi - from my limited expereince in DB2, it appears that you can't
include a case statement in a group by clause - is this correct?

For example, my SQL looked like this:

SELECT CASE WHEN GROUP_CD<'00001' THEN 'ITS' ELSE 'NON' END AS TYPE,
COUNT(*) AS CNT FROM tblCLAIMS GROUP BY CASE WHEN GROUP_CD<'00001' THEN
'ITS' ELSE 'NON' END

My solution was to not initially group the data, and then summarize
the results in another query once the TYPE field had been populated.
Was my syntax incorrect or does DB2 not allow case statements in a
group by clause?

Thanks much!

Dave

Nov 12 '05 #1
3 1895
recheck your code pls... does "SELECT CASE WHEN GROUP_CD<'00001' THEN
'ITS' ELSE 'NON' END AS TYPE FROM tblCLAIMS " work?

I make two tests on the sample database shipped with db2 udb and both
work fine.

select case when edlevel <18 then 'low' else 'high' end as edu,
count(*) from employee group by case when edlevel <18 then 'low' else
'high' end

or

with data as ( select case when edlevel <18 then 'low' else 'high' end
as edu from employee) select edu,count(*) from data group by edu

Nov 12 '05 #2
Could it be our version of DB2, or the fact that I'm running my query
as an Access Pass-through? The SQL I posted above works fine in our SQL
Server environment. It's no big deal to use a subquery first, but I was
hoping to gain some knowledge as to whether DB2 supports case
statements in group by clauses. Thanks for trying!

Nov 12 '05 #3
Fa******@gmail.com wrote:
Could it be our version of DB2, or the fact that I'm running my query
as an Access Pass-through? The SQL I posted above works fine in our SQL
Server environment. It's no big deal to use a subquery first, but I was
hoping to gain some knowledge as to whether DB2 supports case
statements in group by clauses. Thanks for trying!


Yes, DB2 LUW does support CASE expressions in GROUP BY clauses.

To understand what goes wrong in your system, you should provide the
information which DB2 you are using and what was the exact error message.
Also, have you tried your statement directly at the DB2 command line?

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #4

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

Similar topics

27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
3
by: Iain Miller | last post by:
Can anybody help me with some Access 2000 code? I don't do a lot of coding in Access & so every time I come back to do something I pretty much have to relearn the syntax from scratch so this is...
7
by: jmac | last post by:
Greetings fellow programmers, I have created a C program that has a few bugs and would like to get some help with working them out. Here is a list of the problems that I am experiencing: -...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
17
by: Navodit | last post by:
So I have some code like: if (document.Insurance.State.selectedIndex == 1) { ifIll(); } else if (document.Insurance.State.selectedIndex == 2) { elseKan(); }
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.