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
 keep one record of duplicated record

Author  Topic 

goligol
Posting Yak Master

128 Posts

Posted - 2011-11-29 : 13:27:55
Hi,
I would like to create a view of the following table where it keeps only one record only if "port is duplicating:

port port_name type
01 PORTLAND MAINE District
01 PORTLAND, ME
0101 Portland, Maine Port
0101 PORTLAND, ME Port
0102 Bangor, Maine Port
0102 BANGOR, ME Port
0103 Eastport, Maine Port
0103 EASTPORT, ME Port
0104 JACKMAN, ME Port
0104 USA US 201/HWY 6 JACKMAN//CAN HWY 173 ARMSTRONG Port
0105 Vanceboro, Maine Port

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-11-29 : 13:48:05
Probably the easiest way is to use row_number function like this. You can use row_number function in views:
WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY port ORDER BY (SELECT NULL)) AS RN
FROM
YourTable
)
SELECT port, port_name, [type] FROM cte WHERE RN=1;
Go to Top of Page

goligol
Posting Yak Master

128 Posts

Posted - 2011-11-29 : 14:13:00
Wow, that works, thank you
Go to Top of Page
   

- Advertisement -