Koray Kırdinli

Yazılım ve İş Yaşamı Hakkında Paylaşımlar

SQL Tabloda Duplike Kayıtları Silmek (lockres)

Aşağıdaki sql örneği ile duplike kayıtları silmek mümkün. lockres pseudo kodu o rowun fiziksel adresini gösteriyor.

CREATE TABLE TabloAdi (
Column1 varchar(1),
Column2 int
)

INSERT INTO TabloAdi VALUES (‘A’, 1);
INSERT INTO TabloAdi VALUES (‘A’, 1); — duplicate
INSERT INTO TabloAdi VALUES (‘A’, 2);
INSERT INTO TabloAdi VALUES (‘B’, 1);
INSERT INTO TabloAdi VALUES (‘B’, 2);
INSERT INTO TabloAdi VALUES (‘B’, 2); — duplicate
INSERT INTO TabloAdi VALUES (‘B’, 2); — duplicate
INSERT INTO TabloAdi VALUES (‘B’, 2); — duplicate
INSERT INTO TabloAdi VALUES (‘C’, 2)
SELECT *,%%lockres%%
FROM TestTable a
ORDER BY a.Column1, a.Column2

DELETE
FROM TabloAdi
WHERE TabloAdi.%%lockres%%
NOT IN (SELECT MIN(b.%%lockres%%)
FROM TabloAdi b
GROUP BY b.column1, b.Column2)

SELECT Column1,Column2,%%lockres%%
FROM TabloAdi a
ORDER BY a.Column1, a.Column2

Herkese iyi günler dilerim…

December 21, 2012 - Posted by | MSSql | , ,

1 Comment »

  1. Bugn ie gitmedim evden alyorum :)

    Semih Krdinli Software Engineer

    ___________________________________________ Istanbul Technical University Ar Teknokent 2B Maslak, stanbul

    Tel: +90 212 257 8890 Fax: +90 212 257 8070

    http://www.p1m1.com

    Comment by semihkirdinli | December 21, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s