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
 
Want the maximum time stamp

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

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Fri Nov 21, 2008 11:24 am    Post subject: Want the maximum time stamp
Reply with quote

Hi

Is there any wrong with this sql query?

SELECT DATE(MAX(B.NOTE_RVSD_TMSTMP)), B.NTSBJ_CD
FROM KI_CUST_NOTE A
,KI_NOTE B
WHERE A.CU_ID = :HOSTVAR-CU-ID
AND A.NOTE_ID = B.NOTE_ID
AND B.NTSBJ_CD IN ('EA','TT')


Here I want the maximum time stamp with NTSBJ_CD. And the query should throw only one row.

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

tarun_bhardwaj

New User


Joined: 18 Jul 2003
Posts: 39
Location: delhi

PostPosted: Fri Nov 21, 2008 11:41 am    Post subject: Reply to: SQL Query
Reply with quote

This query will not work since there is a missing 'GROUP BY' clause. In a query, if an aggregate function (like 'MAX') is used with another column, you need to provide the 'GROUP BY' clause.
Back to top
View user's profile Send private message
tarun_bhardwaj

New User


Joined: 18 Jul 2003
Posts: 39
Location: delhi

PostPosted: Fri Nov 21, 2008 11:46 am    Post subject: Reply to: SQL Query
Reply with quote

This query should work for you:
Code:
SELECT DATE(B.NOTE_RVSD_TMSTMP), B.NTSBJ_CD
FROM KI_CUST_NOTE A
,KI_NOTE B
WHERE A.CU_ID = :HOSTVAR-CU-ID
AND A.NOTE_ID = B.NOTE_ID
AND B.NTSBJ_CD IN ('EA','TT')
AND B.NOTE_RVSD_TMSTMP = (SELECT MAX(B.NOTE_RVSD_TMSTMP) FROM KI_NOTE)
Back to top
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Fri Nov 21, 2008 11:49 am    Post subject:
Reply with quote

Is it mandatory to use the group by clause with the two columns which we are planning to select?

Or the only one column which is in aggregate function??

ref:
SELECT DATE(MAX(B.NOTE_RVSD_TMSTMP)), B.NTSBJ_CD
FROM KI_CUST_NOTE A
,KI_NOTE B
WHERE A.CU_ID = :HOSTVAR-CU-ID
AND A.NOTE_ID = B.NOTE_ID
AND B.NTSBJ_CD IN ('EA','TT')
GROUP BY B.NOTE_RVSD_TMSTMP, B.NTSBJ_CD
Back to top
View user's profile Send private message
tarun_bhardwaj

New User


Joined: 18 Jul 2003
Posts: 39
Location: delhi

PostPosted: Fri Nov 21, 2008 12:13 pm    Post subject: Reply to: SQL Query
Reply with quote

Hi Raja,

With reference to my 2 postings above, there are 2 ways to get the desired results:

1st:
Code:
SELECT DATE(MAX(B.NOTE_RVSD_TMSTMP)), B.NTSBJ_CD
FROM KI_CUST_NOTE A
,KI_NOTE B
WHERE A.CU_ID = :HOSTVAR-CU-ID
AND A.NOTE_ID = B.NOTE_ID
AND B.NTSBJ_CD IN ('EA','TT')
GROUP BY B.NTSBJ_CD


The above query would get you the date of the maximum timestamp for each NTSBJ_CD code with value EA and TT. Thus the result of the above query would get you two rows.

The second query is:
Code:
SELECT DATE(B.NOTE_RVSD_TMSTMP), B.NTSBJ_CD
FROM KI_CUST_NOTE A
,KI_NOTE B
WHERE A.CU_ID = :HOSTVAR-CU-ID
AND A.NOTE_ID = B.NOTE_ID
AND B.NTSBJ_CD IN ('EA','TT')
AND B.NOTE_RVSD_TMSTMP = (SELECT MAX(B.NOTE_RVSD_TMSTMP) FROM KI_NOTE)

The above query would result in just 1 row being fetched for the date of maximum timestamp for either NTSBJ_CD (with value EA or TT) which ever has the highest timestamp.
Depends on your requirement which query you want to execute.
Back to top
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Fri Nov 21, 2008 1:22 pm    Post subject:
Reply with quote

Thank You for your timely suggestion.....
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
This topic is locked: you cannot edit posts or make replies. 00D3003B - time outs in DB2 when invo... chavinash2004 DB2 2 Mon Oct 09, 2017 4:39 pm
No new posts VSAM RLS Wait time blayek JCL & VSAM 2 Mon Oct 02, 2017 9:05 pm
No new posts Comparing 2 Files using Current time arunsoods SYNCSORT 9 Fri Sep 22, 2017 6:00 pm
No new posts Regarding time parameter shanthi gude JCL & VSAM 7 Mon Sep 04, 2017 2:31 pm
No new posts DSNACCOX (can it be run on 1 db/ts, t... SRICOBSAS DB2 5 Sat May 06, 2017 12:59 am

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