Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Table 1
  APPID  | CERTID       | 	CERTNAME
--------------------------------
11111    |	1	|	ABCD	
22222    |	2	|	EFGH
33333    |	3	|	IJKL
11111    |	4	|	XYZ
44444    |	5	|	JHJ
22222    |	6	|	KJHK
11111    |	7	|	TUHJ

Note:- here cert is is PK and its not repeatable where as App Id is repeatable columns values

In this table How do I get distinct AppId and corresponding certId ,

This is my query , here still I am getting duplicate AppId's

SQL
Select distinct(APPID),CERTID from Table1


Please Please Can any one help me.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 28-Aug-14 0:37am
v2

Try this:
SQL
SELECT APPID, CERTID
FROM (SELECT APPID, CERTID,
             ROW_NUMBER() OVER (PARTITION BY APPID ORDER BY APPID) AS RowNumber
      FROM   Table1) AS a
WHERE   a.RowNumber = 1
 
Share this answer
 
Comments
Sanchayeeta 28-Aug-14 7:22am    
I didn't see your answer before posting my answer Otherwise i wouldn't. Its working Perfectly.
For each distinct APPID there are Different CERTID, like for APPID 11111 there are three CERTID 1,4,7. You have to pic one of them otherwise APPID will repeat.

As per your requirement I think this will work for you.

SQL
select APPID, MAX(CERTID) from Table1
group by APPID
 
Share this answer
 
SQL
Select distinct APPID,CERTID 
from Table1
group by APPID,CERTID 
 
Share this answer
 
v2
Comments
OriginalGriff 28-Aug-14 6:51am    
Reason for my vote of one: did you try that at all? Because it generates exactly the same results as
SELECT APPID,CERTID FROM Table1
given that CERTID is the primary key and can't be contain duplicated values.

Please read the question before answering in future...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900