4

Removing rows in SQL that have a duplicate column value

 2 years ago
source link: https://www.codesd.com/item/removing-rows-in-sql-that-have-a-duplicate-column-value.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Removing rows in SQL that have a duplicate column value

advertisements

I have looked high and low on SO for an answer over the last couple of hours (subqueries, CTE's, left-joins with derived tables) to this question but none of the solutions are really meeting my criteria..

I have a table with data like this :

   COL1  COL2 COL3
    1     A    0
    2     A    1
    3     A    1
    4     B    0
    5     B    0
    6     B    0
    7     B    0
    8     B    1

Where column1 1 is the primary key and is an int. Column 2 is nvarchar(max) and column 3 is an int. I have determined that by using this query:

select name, COUNT(name) as 'count'
FROM [dbo].[AppConfig]
group by Name
having COUNT(name) > 3

I can return the total counts of "A, B and C" only if they have an occurrence of column C more than 3 times. I am now trying to remove all the rows that occur after the initial value of column 3. The sample table I provided would look like this now:

   COL1  COL2 COL3
    1     A    0
    2     A    1
    4     B    0
    8     B    1

Could anyone assist me with this?


If all you want is the first row with a ColB-ColC combination, the following will do it:

select min(id) as id, colB, colC
from tbl
group by colB, colC
order by id

SQL Fiddle


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK