Solved

GROUP BY clause in DB2 UDB 7.2

Posted on 2004-04-19
4
947 Views
Last Modified: 2012-05-11
Hi,

Environment details:AIX 5.1,DB2 UDB 7.2 with FIX pack 11

I have a table with 140 columns containing 3 lakh records.

I am writing a query which includes all 140 columns but i need to group by only on 2 columns .

Select col1,col2,col3,col4...col140 from class_cov_code_t
group by col1,col14

The above statement raises error. (All statements in select should be include in group by clause but i require only few columns from the select list). How can this be achived .

Thanks in adv
Banu



0
Comment
Question by:banumaran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
BigSchmuh earned 75 total points
ID: 10861560
The GROUP BY syntax needs to have every expression not in the GROUP BY clause being an aggregate functions.
This just means that when you group your records, you can first group using the primary (or unique) columns of your non-grouped tables, then add every needed columns from those non-grouped tables and the aggregate functions for the grouped tables.

Example1:
Select TblCustomer.CUST_ID, TblCustomer.CUST_NAME, Sum(TblOrder.AMOUNT) TotalAmount
From TblCustomer, TblOrder
Where TblCustomer.CUST_ID = TblOrder.CUST_ID
Group By TblCustomer.CUST_ID, TblCustomer.NAME

Example2 considering that TblCustomer.CUST_ID is unique
Select TblCustomer.CUST_ID, Max(TblCustomer.CUST_NAME) CUST_NAME, Sum(TblOrder.AMOUNT) TotalAmount
From TblCustomer, TblOrder
Where TblCustomer.CUST_ID = TblOrder.CUST_ID
Group By TblCustomer.CUST_ID

In your case, I assume the 2 columns used to group are unique and you can easily use a Max(Col3), ... Max(Col140) to avoid writing the 140 columns in your group by clause.

Hope this helps.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10861817
yes what are the other 138 columns?
 
which values do you want returned for the grouped by columns?

do you want the max or min values for them    
or do you want to sum them ?


0
 

Author Comment

by:banumaran
ID: 10870502
Hi,

Thanks you  BigSchmuh & Lowfatspread.

I was able to solve this by using this statement

Select x.col1,col2,col3,col4,col5,col6...col140 from (select col1,col14 from stage.class_cov_t )x , stage.class_cov_t y
where x.col1=y.col1 and x.col14=y.col14

here none of the columns are unique ones.

Thanks
Banu
0
 

Author Comment

by:banumaran
ID: 10870548
Hi

A small correction in the above query ,group by is missing in the above one

This is the correct query

Select x.col1,col2,col3,col4,col5,col6....x.col14...col140 from (select col1,col14 from stage.class_cov_t  group by col1,col14 )x , stage.class_cov_t y
where x.col1=y.col1 and x.col14=y.col14 order by col20

Thanks
Banu
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question