Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 distinct data

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-04-11 : 02:37:26
col1 col2 col3
AAA A01 hello
AAA A01
BBB B01
CCC C01
DDD D01

how can i only take those duplicate in col1 to appear once. Result should be as below:

col1 col2 col3
AAA A01 hello
BBB B01
CCC C01
DDD D01

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-11 : 02:41:14
[code]SELECT Col1, col2, col3
FROM (SELECT Col1, col2, col3, ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY col2,col3 DESC) RN
FROM Table
) t
WHERE t.RN = 1[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-11 : 02:43:13
shouldnt it be the below



SELECT Col1, col2, col3
FROM (SELECT Col1, col2, MAX(col3) OVER (PARTITION BY Col1,COl2) AS Col3, ROW_NUMBER() OVER(PARTITION BY Col1,COl2 ORDER BY col3) RN
FROM Table
) t
WHERE t.RN = 1


check with this sample data


col1 col2 col3
AAA A01 hello
AAA A01
AAA A02 hi
AAA A02
BBB B01
CCC C01
DDD D01


what should be your output?
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

UnemployedInOz
Yak Posting Veteran

54 Posts

Posted - 2013-04-11 : 02:43:15
When would you choose the second entry?
i.e. the results could be
col1 col2 col3
AAA A01
BBB B01
CCC C01
DDD D01
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-04-11 : 02:53:53
is working fine now.
should use row number.

thanks
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-11 : 03:00:50
quote:
Originally posted by peace

is working fine now.
should use row number.
thanks

Which one is suitable to you? ( mine or visakh's?)


--
Chandu
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-04-11 : 09:38:16
yours bandi.

thanks
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-12 : 00:18:18
quote:
Originally posted by peace

yours bandi.
thanks

welcome

--
Chandu
Go to Top of Page
   

- Advertisement -