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
 
need distinct/unique occurance value

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Wed Sep 16, 2009 9:39 pm    Post subject: need distinct/unique occurance value
Reply with quote

Hi

i have a table as shown below:

Code:

col1       col2     col3    col4
a           1         xx      xx1
b           2         yy      yy2
c           2         zz      zzc
d           3         pp      ppk
e           3         kk      kkk
f           3         jj      jjj


I need output like below
Code:

col1       col2     col3    col4
a           1         xx      xx1
b           2         yy      yy2
d           3         pp      ppk


I need distinct/unique occurance value of just col2.

Thanks,
Back to top
View user's profile Send private message

muthuvel

Active User


Joined: 29 Nov 2005
Posts: 217
Location: Chennai

PostPosted: Fri Sep 18, 2009 10:28 am    Post subject:
Reply with quote

i guess this may help

Code:
select *
from table a
where col2 in (select distinct col2 from table a);
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Sep 18, 2009 12:07 pm    Post subject:
Reply with quote

Hi Muthuvel,

i don't think this will work....

the sub query will get the distinct values but the main query will select all the rows...
Back to top
View user's profile Send private message
Escapa

Senior Member


Joined: 16 Feb 2007
Posts: 1399
Location: IL, USA

PostPosted: Fri Sep 18, 2009 12:10 pm    Post subject:
Reply with quote

Hi muthuvel,
Have you tried executing this query?
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Sep 18, 2009 12:22 pm    Post subject:
Reply with quote

Actually the fourth column is a date datatype. So i got using co-related subquery as below

SELECT A.COL1,A.COL2,A.COL3,A.COL4
FROM TAB1 A
WHERE A.COL4 = (SELECT MAX(B.COL4) FROM TAB1 B
WHERE B.COL2=A.COL2)

it worked for me, as i will get the most current inserted row from the table. But still, if anyone has any other idea please it.

Thanks,
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Sep 18, 2009 3:18 pm    Post subject:
Reply with quote

What if the dates are same for 2 COL2 ?
You may have to check the same for other cols as well with DISTINCT.
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 column with count of rows within dist... ronald wouterson DB2 4 Sun Sep 17, 2017 9:48 pm
No new posts How to generate a new unique Input fi... for1ranjith CLIST & REXX 11 Sat Jul 01, 2017 12:09 pm
No new posts Row-Numbers of distinct rows? Auryn DB2 1 Thu Oct 20, 2016 4:38 pm
No new posts Command UNIQUE CONSTRAINT on any tabl... rohanthengal DB2 2 Thu Aug 18, 2016 3:48 pm
No new posts Unique sort situation.... dbecker SYNCSORT 6 Wed Apr 27, 2016 8:53 pm

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