I would like to number all rows of my table according to how many times a given row has occured in the table. I tried to define a table of my columns as follows:
select COLUMN_NAME as cl from information_schema.columns where table_name = 'TEST'
Then I would like to run the following query.
SELECT *,
ROW_NUMBER() OVER(PARTITION BY Cols IN cl ORDER BY (SELECT 0)) AS rn
FROM TEST
The second query works nicely if I manually specify the column names for partition. But otherwise it fails. Is there a way of doing this? The motivation behind all this is that I have a table without an ID, where there can be row duplicates which I want to get rid of. The second query is inspired by answers to this question. I use MS SQL 2008.
Sample data before:
| Column1 | Column2 | Column3 |
|---------|---------|------------|
| aaaa | 1111 | 23.04.2018 |
| bbbb | 2222 | 24.04.2018 |
| cccc | 3333 | 25.04.2018 |
| dddd | 4444 | 26.04.2018 |
| bbbb | 4445 | 27.04.2018 |
| aaaa | 1111 | 23.04.2018 |
| aaaa | 1234 | 23.04.2018 |
Sample data after:
| Column1 | Column2 | Column3 |
|---------|---------|------------|
| aaaa | 1111 | 23.04.2018 |
| bbbb | 2222 | 24.04.2018 |
| cccc | 3333 | 25.04.2018 |
| dddd | 4444 | 26.04.2018 |
| bbbb | 4445 | 27.04.2018 |
| aaaa | 1234 | 23.04.2018 |