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
No new posts How syntax check at Bind time is diff... Poha Eater DB2 5 Wed Mar 21, 2018 9:16 pm
No new posts Merging records, but not all the time prino DFSORT/ICETOOL 17 Sat Feb 17, 2018 4:20 pm
No new posts Need to identify top 10 costly or tim... ashek15 DB2 4 Wed Feb 14, 2018 3:20 am
No new posts Logic to skip Maximum time-stamp and ... vickey_dw DB2 2 Thu Feb 01, 2018 11:15 pm
No new posts Query to skip Max time-stamp and fetc... vickey_dw DB2 8 Sun Jan 28, 2018 1:09 am

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