473,473 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Group By with a scalar function or to find shorter way of coding this query

1 New Member
Hi,

I am trying to create a query where I can retrieve average monthly balances for a client for all the months that we have data stored for the year ending 2007.

The table has a balance, for every business day of each month (AIGP_ENTRY_D) +- 24 entries per month. The result I need is a list of the month and the average balance for that month e.g.
Expand|Select|Wrap|Line Numbers
  1. MNTH              AVG_BAL
  2. -----             ---------------
  3.  01                  1000.00
  4.  02                  1200.00
  5.  03                   900.00
  6.  04                   300.00
The only way I can get the average balance is to use the 'union all' up to 11 times, and to hard code the month.
Expand|Select|Wrap|Line Numbers
  1. SELECT AVG(AIGP_GRP_CR_BAL_A )  AS AVG_BAL
  2. FROM CATS.AIA_GRP                                 
  3. WHERE AIGP_GRP_C = '00160'                        
  4. AND YEAR(AIGP_ENTRY_D) = 2007                     
  5. AND MONTH(AIGP_ENTRY_D) = 1     
  6.  
  7.      UNION ALL                                         
  8.  
  9. SELECT AVG(AIGP_GRP_CR_BAL_A )  AS AVG_BAL
  10. FROM CATS.AIA_GRP                                 
  11. WHERE AIGP_GRP_C = '00160'                        
  12. AND YEAR(AIGP_ENTRY_D) = 2007                     
  13. AND MONTH(AIGP_ENTRY_D) = 2           
  14.  
  15.     UNION ALL       
  16. ...
I have tried this:
Expand|Select|Wrap|Line Numbers
  1. SELECT MONTH(AIGP_ENTRY_D)      AS MNTH,
  2.        AVG(AIGP_GRP_CR_BAL_A)      AS AVG_BAL
  3. FROM CATS.AIA_GRP 
  4. WHERE AIGP_GRP_C = '00160'
  5. AND YEAR(AIGP_ENTRY_D) = 2007
  6. GROUP BY MNTH
I get the following error message:
Expand|Select|Wrap|Line Numbers
  1. SQLCODE -206
  2. MNTH IS NOT A COLUMN OF AN INSERTED TABLE, UPDATED TABLE, OR ANY TABLE IDENTIFIED IN A FROM CLAUSE,
  3.  
There has to be a shorter way to do this in DB2.
Feb 5 '08 #1
2 1839
sakumar9
127 Recognized Expert New Member
One possible solution:
Expand|Select|Wrap|Line Numbers
  1. SELECT AVG(AIGP_GRP_CR_BAL_A ) AS AVG_BAL
  2. FROM CATS.AIA_GRP
  3. WHERE AIGP_GRP_C = '00160'
  4. AND YEAR(AIGP_ENTRY_D) = 2007
  5. AND MONTH(AIGP_ENTRY_D) IN (1,2,3,4,5,6,7,8,9,10,11) 
Let me know if this is what you were looking for.

Regards
-- Sanjay
Feb 7 '08 #2
docdiesel
297 Recognized Expert Contributor
Hi,

first, I added some code tags to your posts for better readability. Second, I'd do it in two steps, using a temporary table:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   tmptab.month as month,
  3.   avg(tmptab.balance) as avg_balance
  4. FROM
  5. (
  6.   SELECT
  7.     Month(AIGP_ENTRY_D) as month,
  8.     AIGP_GRP_CR_BAL_A as balance
  9.   FROM
  10.     CATS.AIA_GRP
  11.   WHERE
  12.     YEAR(AIGP_ENTRY_D) = 2007
  13. ) tmptab
  14. GROUP BY
  15.   tmptab.month
  16. ORDER BY
  17.   tmptab.month ASC
  18. ;
  19.  
Regards,

Bernd
Feb 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
21
by: kimimaro | last post by:
Is there anymore methods in exiting your program using pure C language other than return 0?
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
6
by: Carsten | last post by:
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: ...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.