0

I have a SQL Server table that has duplicate entries in one of the columns e.g.:

 +----+-----------+------------+ | id | object_id | status_val | +----+-----------+------------+ | 1 | 1 | 0 | | 2 | 1 | 0 | | 3 | 1 | 0 | | 4 | 2 | 0 | | 5 | 3 | 0 | | 6 | 4 | 0 | | 7 | 4 | 0 | +----+-----------+------------+ 

I need the output to be like this:

 +----+-----------+------------+ | id | object_id | status_val | +----+-----------+------------+ | 4 | 2 | 0 | | 5 | 3 | 0 | +----+-----------+------------+ 

How to resolve this?

1
  • i think Matt & yossi answer are good so far. Commented Dec 5, 2017 at 11:09

6 Answers 6

1

Is this what you are looking for?

SELECT * FROM <yourTable> t1 WHERE t1.object_id NOT IN ( SELECT t2.object_id FROM <yourTable> t2 GROUP BY t2.object_id HAVING COUNT(object_id) > 1 ) 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

select min(id), object_id, min(status_val) from table group by object_id having count(*) = 1 

Comments

0

Use HAVING and GROUP BY

SELECT MIN(id) id, object_id, MIN(status_val) status_val FROM yourtable GROUP BY object_id HAVING COUNT(object_id) = 1 

Output

id object_id status_val 4 2 0 5 3 0 

SQL Fiddle: http://sqlfiddle.com/#!6/7f643f/9/0

Comments

0

You can use group by for unique record like below :-

SELECT * from TABLENAME group by TABLE_COLOUM_NAME 

This query give you only unique value from your Table.

Comments

0

Give a row number for each row partitioned and ordered by the columns [object_id], [status_val]. Then from the result set select the rows which having maximum row number 1.

Query

;with cte as( select [rn] = row_number() over( partition by [object_id], [status_val] order by [object_id], [status_val] ), * from [your_table_name] ) select min([id]) as [id], [object_id], [status_val] from cte group by [object_id], [status_val] having max([rn]) = 1; 

Find a demo here

Comments

0
SELECT COUNT(*) FROM( SELECT DISTINCT object_id FROM object_table ) as row_count, status_val,id,object_id FROM object_table where row_count = 1; 

I think you are looking for that

1 Comment

Remove quote from object_table please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.