Jessica R
Jessica R

Reputation: 13

Can I update a column based on COUNT? (SQL)

I have 101k records but some of these are duplicates. So when I do a COUNT function I get 50,369 records. Is there a way to put the COUNT in a column so I can see the occurrence number for each record?

Upvotes: 1

Views: 43

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

If your database supports analytic functions, then COUNT can do what you want:

SELECT *,
    COUNT(*) OVER (PARTITION BY col1, col2, col3) dup_cnt
FROM yourTable;

This assumes that your table has three columns which constitute whether or not a record is duplicate. You may adjust this logic accordingly.

Upvotes: 1

Related Questions