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

Want the maximum time stamp


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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
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
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
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the the current time DFSORT/ICETOOL 13
No new posts RC query -Time column CA Products 3
No new posts C Compile time time stamps Java & MQSeries 10
No new posts Parallelization in CICS to reduce res... CICS 4
No new posts Insert system time/date (timestamp) u... DFSORT/ICETOOL 5
Search our Forums:

Back to Top