Get One row in duplicates

Hi ,
  I have an Sql Server database with table Documents
  There are many duplicates rows with same Document_No  . But I need to get only one row from each document no

From the following sample data , I am looking for a query

   Doc1 Proj1 Path1 ......
   Doc1 Proj1 Path2
   Doc2 Proj1 Path2
   Doc3 Proj1 Path3

Query Should return rows with  Doc2 and Doc3 and one of the Doc1 row (it does not matter which one)
Sam OZAsked:
Who is Participating?
 
Ryan ChongCommented:
try:

;with cte as
(
  select a.*, row_number() over (partition by docfield order by projfield, pathfield ) idx from yourtable
)
select * from cte where idx = 1
0
 
Sam OZAuthor Commented:
Thanks , In fact I have some 20 fields in the Table  . Will I have to do order by on all except DocNO ?
     DocTable has fields like
         DocNo
         ProjNo
         Path
         RevNo
          ...... More fields

Can you please write the query for this ?
0
 
Ryan ChongCommented:
>>Will I have to do order by on all except DocNO

not necessarily, what I did is for as an example. you can also try like this instead:

row_number() over (partition by docfield order by (select 1)) idx
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ryan ChongCommented:
"partition by" will determine what criterion to group the records while "order by" will determine the sequence orders
1
 
Mark WillsTopic AdvisorCommented:
And, while Ryan is absolutely right, I would add you need to partition by all the columns that identify the other column that has the duplicated values.

All the other fields pertain to the columns as defined in the partition by, so revision will be per docfield, projfield

Now, I can well imagine multiple revisions, so you may want to include that in your order by RevNo desc  (keeping the most recent) In which case we will need to know a bit more about your requirements.

In your case (borrowing Ryans code)
;with cte as
(
  select a.*, row_number() over (partition by docfield, projfield  order by pathfield ) rn from yourtable
)
select * from cte where rn = 1  -- and you should only select the columns you want

Open in new window

0
 
Mark WillsTopic AdvisorCommented:
With due respect, it seems you didnt understand my comments about partitioning by all columns that generates the column with the dupe values... There is a profound difference in partitioning and ordering and it will make or break

By way of example
create table #projects (Docfield varchar(20), ProjField Varchar(20), Pathfield varchar(50))

insert #projects values ('Doc1','Proj1','Path1')
insert #projects values ('Doc1','Proj5','Path5')

;with ryan_cte as
(
  select a.*, row_number() over (partition by docfield order by projfield, pathfield ) idx from #projects a
)
select * from ryan_cte where idx = 1

;with mark_cte as
(
  select a.*, row_number() over (partition by docfield, projfield order by pathfield ) rn from #projects a
)
select * from mark_cte where rn = 1

Open in new window

And if we consider the prospect of RevNo it could become even more complex.

Just saying, be careful.... And we havent even discussed datatypes or sort sequences (use of collation)
0
 
Mark WillsTopic AdvisorCommented:
Hello ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.