MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Select max - 4 of year field

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
mf_user
Intermediate


Joined: 01 Jun 2003
Posts: 372
Topics: 105

PostPosted: Thu Apr 19, 2012 6:16 am    Post subject: Select max - 4 of year field Reply with quote

Hi, can you help me to write a SQL to pick (MAX YEAR - 4) from a DB2 table that has YEAR field.

I tried below but ended up with SQLCODE -120
Code:

SELECT COUNT(*) FROM DB2P.YEAR_TBL
 WHERE YEAR_NBR = MAX(YEAR_NBR) - 4
WITH UR;                                       


The file has year values from 2008 to 2013 and YEAR_NBR is a character field.

Thanks.
_________________
MF
==
Any training that does not include the emotions, mind and body is incomplete; knowledge fades without feeling.
==
Back to top
View user's profile Send private message Send e-mail
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Apr 19, 2012 8:42 am    Post subject: Reply with quote

mf_user,
the description for an SQL Code of -120
implies,
you need a subquery...

Code:
SELECT COUNT(*)
 FROM DB2P.YEAR_TBL
WHERE YEAR_NBR = (
                  SELECT MAX(YEAR_NBR) - 4
                    FROM DB2P.YEAR_TBL
                  )
WITH UR;



the max(year_nbr) - 4
will need some work since it is a char field.
year_nbr in max(year_nbr) needs to be cast as integer
then the max - 4 needs to be cast as char
so that the comparision can be made with year_nbr on the left side of the where clause.

provide us with the length attribute for year_nbr
and we can provide suggested code.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
mf_user
Intermediate


Joined: 01 Jun 2003
Posts: 372
Topics: 105

PostPosted: Thu Apr 19, 2012 8:47 am    Post subject: attributes Reply with quote

Hi Dick, thanks a lot........

Here are the details..........

Code:

year_nbr   Character (4)


Thanks.
_________________
MF
==
Any training that does not include the emotions, mind and body is incomplete; knowledge fades without feeling.
==
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Apr 19, 2012 3:14 pm    Post subject: Reply with quote

use the following sql(untested)

Code:

SELECT COUNT(*)                                         
  FROM DB2P.YEAR_TBL                                       
 WHERE INT(YEAR_NBR) = (SELECT MAX(INT(YEAR_NBR)) - 4   
                          FROM DB2P.YEAR_TBL)               
  ;       
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mf_user
Intermediate


Joined: 01 Jun 2003
Posts: 372
Topics: 105

PostPosted: Mon Apr 23, 2012 3:16 am    Post subject: Reply with quote

Kolusu, thanks.......it has worked and got the expected results !
_________________
MF
==
Any training that does not include the emotions, mind and body is incomplete; knowledge fades without feeling.
==
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group