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

To Select only the latest first record from db2 table


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

New User


Joined: 06 Nov 2007
Posts: 84
Location: bangalore

PostPosted: Tue May 31, 2011 7:58 pm
Reply with quote

Hi,

Can anyone give me a query to select only the latest record from the db2 table and only one record.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue May 31, 2011 8:09 pm
Reply with quote

order by ? fetch first ?
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Tue May 31, 2011 8:11 pm
Reply with quote

Can you be more specific with your question? How can we give the query without specifying any data.

Before posting it directly here, do google it. You will find many solutions.
Are you so lazy that even this should be done by us.

Anyway you can use a query of this kind

SELECT * FROM table
where col = (SELECT MAX(col) FROM table)

Here 'col' can be one of the column of the table which increments each and every time a new record is added into the table.[/quote]
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue May 31, 2011 8:49 pm
Reply with quote

Quote:
Can anyone give me a query to select only the latest record from the db2 table and only one record.


Can't be done, there are no records in a db2 table
Back to top
View user's profile Send private message
gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Wed Jun 01, 2011 11:37 am
Reply with quote

kumar1234 wrote:
Hi,

Can anyone give me a query to select only the latest record from the db2 table and only one record.


What do you mean by latest record? Do you have a time stamp column in the table?
Back to top
View user's profile Send private message
kumar1234

New User


Joined: 06 Nov 2007
Posts: 84
Location: bangalore

PostPosted: Wed Jun 01, 2011 2:17 pm
Reply with quote

Hi,

Here is the query that i have coded but this will give sql -811, I want to get only the latest only one record based on the time stamp.

Can you tell me what else do I need to add to this query to fetch only one record.

SELECT ESCHEAT_IND
INTO :WHS-20-ESCHEAT-IND
FROM GIWHS$01.WHS_20_EXTRACT
WHERE DRAFT_OFFICE = WS-DRAFT-OFFICE
AND DRAFT_LOGICAL_NO = WS-DRAFT-NUM
ORDER BY ACTIVITY_TS DESC


Thanks.
Back to top
View user's profile Send private message
singhju

New User


Joined: 01 Dec 2010
Posts: 25
Location: Gurgaon

PostPosted: Wed Jun 01, 2011 2:22 pm
Reply with quote

You need to specify FETCH FIRST ROW ONLY after order by clause.
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Wed Jun 01, 2011 2:28 pm
Reply with quote

This query will fetch multiple rows from the table since there are multiple rows present in the talbe.

You can use this as cursor and fetch only the first record, then it is possible to fetch only one record.
Back to top
View user's profile Send private message
kumar1234

New User


Joined: 06 Nov 2007
Posts: 84
Location: bangalore

PostPosted: Wed Jun 01, 2011 2:37 pm
Reply with quote

Using Cursor is a good option but my customer wants me to fetch without using cursor in a single query in the program. Is there any ways to do this ?
Back to top
View user's profile Send private message
gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Wed Jun 01, 2011 2:40 pm
Reply with quote

Modify the query

SELECT ESCHEAT_IND
INTO :WHS-20-ESCHEAT-IND
FROM GIWHS$01.WHS_20_EXTRACT
WHERE DRAFT_OFFICE = WS-DRAFT-OFFICE
AND DRAFT_LOGICAL_NO = WS-DRAFT-NUM
ORDER BY ACTIVITY_TS DESC
Fetch first 1 row only ;
Back to top
View user's profile Send private message
kumar1234

New User


Joined: 06 Nov 2007
Posts: 84
Location: bangalore

PostPosted: Wed Jun 01, 2011 4:18 pm
Reply with quote

Using this query I compiled but I am getting an error as '"ORDER BY" CLAUSE NOT PERMITTED'.

I removed the host variables and ran in QMF this same query is working fine here.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 01, 2011 4:26 pm
Reply with quote

sounds as if you are developing under version 7
and your qmf is version 8.

pays to know your environment.
Back to top
View user's profile Send private message
kumar1234

New User


Joined: 06 Nov 2007
Posts: 84
Location: bangalore

PostPosted: Wed Jun 01, 2011 4:30 pm
Reply with quote

i checked now, both the versions are 7.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 01, 2011 4:35 pm
Reply with quote

Quote:
i checked now, both the versions are 7.


doubt that,
possibly qmf comes with pre-release sql but I doubt it.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Jun 01, 2011 5:51 pm
Reply with quote

if ACTIVITY_TS is unique (at least within DRAFT_OFFICE ,DRAFT_LOGICAL_NO )

SELECT ESCHEAT_IND
INTO :WHS-20-ESCHEAT-IND
FROM GIWHS$01.WHS_20_EXTRACT A
WHERE DRAFT_OFFICE = :WS-DRAFT-OFFICE
AND DRAFT_LOGICAL_NO = :WS-DRAFT-NUM
and ACTIVITY_TS =
(select max(ACTIVITY_TS) from GIWHS$01.WHS_20_EXTRACT B
where A.DRAFT_OFFICE = B.DRAFT_OFFICE
and a.DRAFT_LOGICAL_NO = b.DRAFT_LOGICAL_NO )




"ORDER BY" CLAUSE NOT PERMITTED probably has something to do with the precompiler version/settings.
Back to top
View user's profile Send private message
Jeevankumar.Pochammala

New User


Joined: 04 May 2011
Posts: 7
Location: INDIA

PostPosted: Sun Jun 12, 2011 10:20 pm
Reply with quote

Hello Kumar1234,

just small modification the query given by gylbharat

SELECT ESCHEAT_IND,ACTIVITY_TS
INTO :WHS-20-ESCHEAT-IND,
:WHS-20-ACTIVITY-TS
FROM GIWHS$01.WHS_20_EXTRACT
WHERE DRAFT_OFFICE = WS-DRAFT-OFFICE
AND DRAFT_LOGICAL_NO = WS-DRAFT-NUM
ORDER BY ACTIVITY_TS DESC
Fetch first 1 row only ;

I those this query work fine
Back to top
View user's profile Send private message
gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Mon Jun 13, 2011 1:34 pm
Reply with quote

Jeevankumar.Pochammala wrote:
Hello Kumar1234,

just small modification the query given by gylbharat

SELECT ESCHEAT_IND,ACTIVITY_TS
INTO :WHS-20-ESCHEAT-IND,
:WHS-20-ACTIVITY-TS
FROM GIWHS$01.WHS_20_EXTRACT
WHERE DRAFT_OFFICE = WS-DRAFT-OFFICE
AND DRAFT_LOGICAL_NO = WS-DRAFT-NUM
ORDER BY ACTIVITY_TS DESC
Fetch first 1 row only ;

I those this query work fine


Hi Jeevan,
How fetching timestamp in this query will help?
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 How to split large record length file... DFSORT/ICETOOL 10
No new posts Load new table with Old unload - DB2 DB2 6
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts FINDREP - Only first record from give... DFSORT/ICETOOL 3
No new posts Pulling a fixed number of records fro... DB2 2
Search our Forums:

Back to Top