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

Query required


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Guru Nandu

New User


Joined: 16 Jul 2009
Posts: 6
Location: India

PostPosted: Fri Feb 14, 2014 2:57 pm
Reply with quote

Hi Team,

I have a table that contains Name, Date of access, salary and Rate

Lets say table contains below values

Name Date of access Salary Rate
ABC 2014-02-13 111 0.5
CDE 2010-01-11 222 0.1
ABC 2010-01-01 333 0.2
ZZZ 2009-01-06 444 0.6
XXX 2014-01-30 555 0.1
CDE 2013-12-12 666 0.1

I want the output which select rows of latest date for a given name.
Output expected will be

Name Date of access Salary Rate
ABC 2014-02-13 111 0.5
ZZZ 2009-01-06 444 0.6
XXX 2014-01-30 555 0.1
CDE 2013-12-12 666 0.1

Help required in writing this DB2 query
Back to top
View user's profile Send private message
TheMFKid

New User


Joined: 20 Nov 2013
Posts: 91
Location: India

PostPosted: Fri Feb 14, 2014 3:11 pm
Reply with quote

post the query you have been trying and we will help you to get it right.
Back to top
View user's profile Send private message
Guru Nandu

New User


Joined: 16 Jul 2009
Posts: 6
Location: India

PostPosted: Fri Feb 14, 2014 3:34 pm
Reply with quote

SELECT Name FROM Table where
Name = (SELECT Name FROM Table WHERE
Date of access = (SELECT MAX(Date of access ) FROM Table))
Back to top
View user's profile Send private message
sureshpathi10

Active User


Joined: 03 May 2010
Posts: 154
Location: Kuala Lumpur

PostPosted: Fri Feb 14, 2014 3:39 pm
Reply with quote

Can you try with this?

Code:
SELECT NAME, MAX(DATE OF ACCESS)
FROM TABLE
GROUP BY NAME
Back to top
View user's profile Send private message
Guru Nandu

New User


Joined: 16 Jul 2009
Posts: 6
Location: India

PostPosted: Fri Feb 14, 2014 3:57 pm
Reply with quote

Hi Suresh,

It works with the query that you provided if i retriece name and Date of access.

However I require rate along with Name and date of access.

SELECT Name, Rate, MAX(Date of access)
FROM Table
GROUP BY Name, Rate
WITH UR;

I am getting duplicate rows having same name ABC.
Back to top
View user's profile Send private message
sureshpathi10

Active User


Joined: 03 May 2010
Posts: 154
Location: Kuala Lumpur

PostPosted: Mon Feb 17, 2014 8:05 am
Reply with quote

I'm guessing, Name and Date of access will be unique.

Here is the query.
Code:
SELECT *
FROM (SELECT NAME, MAX(DATE OF ACCESS) AS DATE_1 FROM TABLE GROUP BY NAME) A, TABLE B
WHERE A.NAME = B.NAME AND A.DATE_1 = B.DATE OF ACCESS


Please let us know if this working for you...
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top