i need a query

Ask question on - DataBase Concepts, IBM Mainframe DB2, DB2/UDB, DB2 Tools, SQL , DB2 programming with languages like COBOL, PL1.

Moderators: Kalicharan, Moderator Group

Post Reply
sukamal
Member
Posts: 7
Joined: Wed Oct 22, 2008 12:49 pm

i need a query

Post by sukamal » Tue Sep 01, 2009 5:03 pm

Hi,

Any one help me to write a query.

I have a table with the details of products.One product have more than one record in it.I have to extract the latest record for each product.I have the time filed also to find when the record is entered.I tried wiht MAx() but its working for one product .please give me the query to extract the details of mor than one product.

Query working for one prod is

SELECT * FROM PROD WHERE TIME_PS =
(SELECT MAX(TIME_PS) FROM PROD WHERE
PROD_NO ='ABCD')

I also tried one more query also which is not selecting all the fields
SELECT PROD_NO,MAX(TIME_PS)
FROM PROD
WHERE PROD_NO IN ('ABCD','XYZ')
GROUP BY PROD_NO;

this query is displaying the two fileds(i.e PROD_NO and TIME_PS_ for all the product.But i don't know how to extract all the fileds.help me



Thanks,
Suhaniya.

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Using correlated sub query - db2 sql

Post by Natarajan » Wed Sep 02, 2009 11:27 am

You need to use correlated subquery in this case. it is very similar to your first query.

Try following query.

Code: Select all

SELECT * FROM PROD P WHERE TIME_PS = 
(SELECT MAX(TIME_PS) FROM PROD I WHERE 
I.PROD_NO = P.PROD_NO) 

Let me know, if dont understand this query.
Natarajan
Chennai

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others