Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GROUP BY clause in DB2 UDB 7.2

Posted on 2004-04-19
4
Medium Priority
?
953 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 300 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

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 (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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

660 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