26 septembrie 2012

SQL Server: stergerea randurilor duplicate

ex:
SELECT companyName, contactName, address, country, postings, counting=count(*)
FROM TestTable
GROUP BY companyName, contactName, address,country, postings
HAVING count(*) > 0

- selectul are loc pe toate coloanele lui TestTable
- se obtin randurile unice, alaturi de nr. de aparitii pt fiecare

SELECT companyName, contactName, address, country, postings, counting=count(*)
INTO NewTable
FROM TestTable
GROUP BY companyName, contactName, address,country, postings
HAVING count(*) > 0

- se creeaza un nou tabel cu aceste randuri unice

DROP TABLE TestTable;

SELECT companyName, contactName, address, country, postings
INTO TestTable
FROM NewTable

DROP TABLE NewTable;

Si acum in TestTable se afla doar randurile unice.

Niciun comentariu: