question

billj avatar image
billj asked

How to Ignore duplicate column?

Hello, I need to ignore the rows that contain duplicate values in columns. I do not want to delete any rows in my table, only need to ignore them in my report. A1, Z1, TESTA A2, Z2, TESTB A3, Z3, TESTA A4, Z4, TESTF So I need to ignore rows 3, and keep rows 1,2,4 only. The output will become like this: A1, Z1, TESTA A2, Z2, TESTB A4, Z4, TESTF Any help is appreciated. Thanks.
sql-server-2008-r2
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
ASK SQL Server Central runs by you voting. For all helpful answers below, please indicate this by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate this by clicking on the check mark next that answer.
0 Likes 0 ·
David Wimbush avatar image
David Wimbush answered
You can use SELECT DISTINCT for this.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

billj avatar image billj commented ·
Hi David, I think SELECT DISTINCT is used for duplicate rows. My issue is duplicate columns.
0 Likes 0 ·
David Wimbush avatar image David Wimbush commented ·
Sorry, you're quite right. I think I decided I knew what you were asking before I had read your question properly!
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
So this depends on knowing which columns have the duplicates in.. declare @YourTable table (col1 varchar(10), col2 varchar(10), col3 varchar(10)) insert into @YourTable select 'A1', 'Z1', 'TESTA' insert into @YourTable select 'A2', 'Z2', 'TESTB' insert into @YourTable select 'A3', 'Z3', 'TESTA' insert into @YourTable select 'A4', 'Z4', 'TESTF' select min(col1), min(col2), col3 from @YourTable group by col3 returns col3 ---------- ---------- ---------- A1 Z1 TESTA A2 Z2 TESTB A4 Z4 TESTF
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SSGC avatar image
SSGC answered
I did something like this for your reference; ;with yourtable1(A, Z, T) as ( values ('A1', 'Z1', 'TESTA'), ('A2', 'Z2', 'TESTB'), ('A3', 'Z3', 'TESTA'), ('A4', 'Z4', 'TESTF') ), yourtable2 as ( SELECT A, Z,T, row_number() OVER (PARTITION BY T ORDER BY A) as RN FROM yourtable1 ) SELECT A,Z,T FROM yourtable2 WHERE RN = 1
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.