IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Date calculations


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Fri Mar 15, 2013 12:03 am
Reply with quote

Team,

After execution of the below query

Code:
 SELECT DISTINCT AB_DT             
 FROM XYZ                           
 WHERE AB_DT  <'03/14/2013'
 AND AB_F = 'B'               
 ORDER BY 1 DESC                         
 WITH UR 


output is

Code:
AB_DT
 ----------
 2013-03-13
 2013-03-12
 2013-03-11
 2013-03-08
 2013-03-07
 2013-03-06
 2013-03-05
 2013-03-04
 2013-03-01
 2013-02-28
 2013-02-27
 2013-02-26
 2013-02-25
 2013-02-22
 2013-02-21


Now requirement is I want to get 7th row alone from the about result, I did this by using cursor and fetching it 7 times but can you please suggest to do this using single query?

so the output should only be
Code:
2013-03-05
date
Back to top
View user's profile Send private message
saiprasadh

Active User


Joined: 20 Sep 2006
Posts: 154
Location: US

PostPosted: Fri Mar 15, 2013 7:54 am
Reply with quote

Rohit,

Please try below mentioned query

Code:
SELECT * FROM
(SELECT DISTINCT AB_DT,DENSE_RANK() OVER (ORDER BY AB_DT  DESC) AS RN
FROM XYZ
WHERE AB_DT  < '2013-03-14') X
WHERE RN=7
WITH UR
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Fri Mar 15, 2013 9:35 am
Reply with quote

Sai,

Perfect, This is working. Thanks.
I hope we can be able to use this in the application programming, but in SPUFI it is working as expected.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Fri Mar 15, 2013 12:39 pm
Reply with quote

Why do you think it won't work with COBOL (application programming)? The only restriction is that 'DB2 should be in NFM'. Apparently you are using NFM in the LPAR you've tried with SPUFI.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Mar 15, 2013 1:59 pm
Reply with quote

should be more performant :
Code:
SELECT ab_dt FROM
(SELECT DISTINCT AB_DT FROM XYZ WHERE AB_DT  < '2013-03-14' order by ab_dt desc fetch first 7 rows only) X
order by ab_dt asc
fetch first row only
WITH UR
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Fri Mar 15, 2013 2:11 pm
Reply with quote

Thanks GuyC, a nice point you put forward.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Fri Mar 15, 2013 9:14 pm
Reply with quote

GuyC, Thanks.

Quote:
Code:
SELECT ab_dt FROM
(SELECT DISTINCT AB_DT FROM XYZ WHERE AB_DT  < '2013-03-14' order by ab_dt desc fetch first 7 rows only) X
order by ab_dt asc
fetch first row only
WITH UR


Can we use host variable instead of 7 in
Quote:
fetch first 7 rows only
?

As 7 is just the example I have provided, but this is a dynamically changing value.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Fri Mar 15, 2013 11:28 pm
Reply with quote

Don't take it otherwise - perhaps this is something from your side to research on Rohit, yes?
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Wed Mar 20, 2013 1:09 am
Reply with quote

Anuj, yeah .
And The query provided by Sai gives the correct result but COST*RATE is considerably larger than using the cursors. So I am dropping this plan to use OLAP fuction.
Back to top
View user's profile Send private message
Anuj Dhawan

Superior Member


Joined: 22 Apr 2006
Posts: 6250
Location: Mumbai, India

PostPosted: Wed Mar 20, 2013 11:06 am
Reply with quote

Have you checked with what GuyC has suggested, that should give you better (read it as 'rather less') COST*RATE?
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Wed Mar 20, 2013 8:45 pm
Reply with quote

Yes Anuj, shortly I will post the results.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Wed Mar 20, 2013 8:56 pm
Reply with quote

I am getting the below message when I tried using the host variable instead of hard coded "7",
Code:

ILLEGAL SYMBOL ":". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: <INTEGER> ROW ROWS DB2 SQL PRECOMPILER         STATISTICS


Any more suggestions.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Wed Mar 20, 2013 9:03 pm
Reply with quote

Rohit Umarjikar wrote:
I am getting the below message when I tried using the host variable instead of hard coded "7",
Code:

ILLEGAL SYMBOL ":". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: <INTEGER> ROW ROWS DB2 SQL PRECOMPILER         STATISTICS


Any more suggestions.

I suggest that this means that you can't use a host variable to supply the number of rows to be fetched.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3048
Location: NYC,USA

PostPosted: Thu Mar 21, 2013 3:12 am
Reply with quote

yeah, true.
So I have only one choice left to use cursors (ofcource when we talk about better performance). Thanks all.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts Need to convert date format DFSORT/ICETOOL 20
No new posts Need help to append a date&tsp at... DFSORT/ICETOOL 9
No new posts Fetch data from programs execute (dat... DB2 3
No new posts Insert system time/date (timestamp) u... DFSORT/ICETOOL 5
Search our Forums:

Back to Top