Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Date calculations

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Fri Mar 15, 2013 12:03 am    Post subject: Date calculations
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    Post subject:
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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Fri Mar 15, 2013 9:35 am    Post subject:
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

Senior Member


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

PostPosted: Fri Mar 15, 2013 12:39 pm    Post subject:
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: 1278
Location: Belgium

PostPosted: Fri Mar 15, 2013 1:59 pm    Post subject:
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

Senior Member


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

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

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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Fri Mar 15, 2013 9:14 pm    Post subject:
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

Senior Member


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

PostPosted: Fri Mar 15, 2013 11:28 pm    Post subject:
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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Wed Mar 20, 2013 1:09 am    Post subject:
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

Senior Member


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

PostPosted: Wed Mar 20, 2013 11:06 am    Post subject:
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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

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

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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Wed Mar 20, 2013 8:56 pm    Post subject:
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: 1738
Location: Bloomington, IL

PostPosted: Wed Mar 20, 2013 9:03 pm    Post subject:
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

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Mar 21, 2013 3:12 am    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts TOT & DATE parameter in ICEMAN hemanthj642 DFSORT/ICETOOL 4 Mon Nov 14, 2016 5:19 am
This topic is locked: you cannot edit posts or make replies. How to pass the previous month date i... Suganya87 DFSORT/ICETOOL 5 Mon Oct 31, 2016 4:13 pm
This topic is locked: you cannot edit posts or make replies. Get correct date and time when curren... balaji81_k DB2 24 Fri Oct 14, 2016 10:40 pm
No new posts Change date (DD/MM/YY) in 2nd record ... uday kiran DFSORT/ICETOOL 12 Wed Sep 07, 2016 10:57 pm
No new posts Add system date inside of a PS file a... pramitdas DFSORT/ICETOOL 5 Sun Aug 28, 2016 12:06 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us