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
 

 

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
No new posts DD DUMMY & CPU TIME Nileshkul Testing & Performance analysis 16 Mon Dec 19, 2016 11:35 pm
This topic is locked: you cannot edit posts or make replies. Get a job submitted itself every time... arunsoods JCL & VSAM 3 Tue Nov 22, 2016 4:05 pm
No new posts Check System time(Minute) Using TIME1... balaji81_k DFSORT/ICETOOL 5 Fri Nov 11, 2016 10:53 am
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 cics & real time monitoring icehat CICS 5 Wed Sep 07, 2016 2:29 am


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