Solved

GROUP BY clause in DB2 UDB 7.2

Posted on 2004-04-19
4
945 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
remove db2tschg.his file 1 129
DB2 CONCAT FAILING 4 111
iSeries DB2 query across local and remote site 4 85
filter big file based upon on multipe criteria 2 60
November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

823 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