How to remove duplicated rows by holding other columns

Asked

Viewed 368 times

4

In the example below, I am interested in removing duplicates of rows in columns title and time no matter how many columns.

In the example below I am interested that the query returns rows 32, 34 and 36 and all columns.

id | title   | time  | domain
32   title1    12:30   domain1.com
33   title1    12:30   domain2.com
34   title2    14:20   domain1.com
35   title2    14:20   domain2.com
36   title3    15:30   domain55.com
  • Would it be possible, post as you are trying to do? and the tables? is just a table? explain better your problem.

  • 1

    @Gokussjgod Problem solved, thank you very much!

  • @Gokussjgod Oracle

  • 1

    @Marconciliosouza again, the tag added is irrelevant to the question

1 answer

5


User the Group by and the min(),

declare @tabela table
(
   id int,
   title varchar(20),
   time time,
   domain varchar(30)
)

insert into @tabela values

(32, 'title1',    '12:30' ,  'domain65.com'),
(33, 'title1',    '12:30' ,  'domain2.com'),
(34, 'title2',    '14:20' ,  'domain1.com'),
(35, 'title2',    '14:20' ,  'domain2.com'),
(36, 'title3',    '15:30' ,  'domain55.com')

select min(id) as ID, title, time, (select domain from @tabela where id = min(t1.id)) 
from @tabela t1
group by title, time

Or

select min(id) as ID, title, time, min(domain) from @tabela
group by title, time
  • Could you explain in your code how this works? I’m interested in the question, but your answer presents only the codes, I don’t know what they’re doing.

  • 2

    @Florida, The part that really matters for question is the select... I am practically grouping the fields title, time, or whatever is equal in these fields will be grouped in a single line. MIN() as the name is already intuitive takes the minimum value of the lines that are grouped in group by. Nothing complicated, so I didn’t explain here. references can help in this part.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.