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

Unique row.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Thu Oct 29, 2009 5:52 pm
Reply with quote

Hi all,
i have the below table

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


and i want only one occurance of col2.

output:
Code:



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


i already asked this question but the solution was also provided my me. but was manipulated to get the result.

Is there any query to achive unique row for col2.

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

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Thu Oct 29, 2009 6:20 pm
Reply with quote

Hi Arvind,
For unique col2 which of the matching col1,col3,col4 should be retrieved in query.
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Thu Oct 29, 2009 6:22 pm
Reply with quote

any column....i need only one occurance of col2...
Back to top
View user's profile Send private message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Thu Oct 29, 2009 6:28 pm
Reply with quote

Why dont you use DB2 - function distinct on col2 while quering from database
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Oct 29, 2009 6:30 pm
Reply with quote

doesn't make sense, but here a possible solution:
Code:
select min(col1), col2, min(col3), min(col4) from table1 group by col2

or if col1 is primary key
Code:
select t1.col1,t1.col2,t1.col3,t1.col4 from table1 t1
where t1.col1 = (select min(t2.col1) from table1 t2 where t1.col2=t2.col2)
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Thu Oct 29, 2009 6:44 pm
Reply with quote

what if input is like this....col1 has same value.

Code:

col1       col2     col3    col4
a           1         xx      xx1
b           2         yy      yy2
b           2         zz      zzc
d           3         pp      ppk
e           3         kk      kkk
f           3         jj      jjj
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Oct 29, 2009 6:51 pm
Reply with quote

then col1 is not the primary key :p

What is the primary key of the table ?
Back to top
View user's profile Send private message
raam2smart

New User


Joined: 31 May 2007
Posts: 19
Location: Chennai

PostPosted: Fri Oct 30, 2009 12:54 pm
Reply with quote

Try this...

select * from t1 where col2 in(select distinct(col2) from t1)

Ramesh
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Oct 30, 2009 1:14 pm
Reply with quote

raam2smart wrote:
Try this...

select * from t1 where col2 in(select distinct(col2) from t1)

Ramesh

uhm, NO.
This will give the same result as: select * from t1
Back to top
View user's profile Send private message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Fri Oct 30, 2009 1:42 pm
Reply with quote

Raam
Try your query before posting it on this site.

Not a Smart work icon_lol.gif icon_lol.gif

Regards
Bhairon Singh Rathore
Back to top
View user's profile Send private message
dejunzhu

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Fri Oct 30, 2009 5:32 pm
Reply with quote

I'm afraid the only way you can use to achieve your expected result is using cursor.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Nov 02, 2009 2:27 pm
Reply with quote

in DB2 9 you could use fetch first in a subselect.

something like this :
Code:
select C.* from
(select creator from sysibm.systables group by creator) A
,
(select * from sysibm.systables B where b.creator = a.creator
fetch first row only) C


I can't test it, because we're not yet in NFM.
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 combine multiple unique records into ... DFSORT/ICETOOL 2
No new posts Any JCL or VSAM Utility to get number... JCL & VSAM 1
No new posts Can we Insert duplicates in Primary U... DB2 2
No new posts SEPARATE UNIQUE and NON-UNIQUE in SEP... SYNCSORT 6
No new posts Unique IMS DB identifier IMS DB/DC 1
Search our Forums:

Back to Top