Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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: 1817
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: 1817
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: 1281
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: 1817
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: 1817
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: 1817
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: 1817
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: 1779
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: 1817
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 Date in where clause - Windows Karthikeyan Subbarayan DB2 9 Wed Nov 15, 2017 9:07 pm
No new posts Compare yesterday's date to the one o... migusd SYNCSORT 11 Fri Sep 22, 2017 11:35 pm
No new posts Validate the Date girishb2 DFSORT/ICETOOL 9 Tue Sep 19, 2017 1:12 am
No new posts Julian Date to CICS ABSTTIME blayek CICS 3 Wed Aug 30, 2017 11:15 pm
No new posts How to add header with Date(YYMMDD) i... Rajan Moorthy DFSORT/ICETOOL 2 Thu Jul 06, 2017 11:44 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us