Skip to main content

Remove Duplicate Row in Sql Query

Method 1:


-- Create Sample Table
DECLARE @table TABLE (data VARCHAR(20))

-- Insert Some Data
INSERT INTO @table VALUES ('not duplicate row')
INSERT INTO @table VALUES ('duplicate row')INSERT INTO @table VALUES ('duplicate row')


-- Find out Duplicate rows in table:
SELECT  data, COUNT(data) nrFROM    @tableGROUP BY dataHAVING  COUNT(data) > 1


-- Remove Duplicate rows from table
SET NOCOUNT ONSET ROWCOUNT 1WHILE 1 = 1   BEGIN      DELETE   FROM @table
      WHERE    data IN (SELECT  data                               FROM    @table
                               GROUP BY data                               HAVING  COUNT(*) > 1)      IF @@Rowcount = 0          BREAK ;   ENDSET ROWCOUNT 0

-- See Out put after remove duplicate record from table.
SELECT * FROM  @table



Method 2:


DECLARE  @tempData TABLE (data VARCHAR(20))
INSERT INTO @tempData VALUES ('not duplicate row')
INSERT INTO @tempData VALUES ('duplicate row')
INSERT INTO @tempData VALUES ('duplicate row')
INSERT INTO @tempData VALUES ('second duplicate row')
INSERT INTO @tempData VALUES ('second duplicate row')
;
WITH    numbered
          AS ( SELECT   data
                      , row_number() OVER ( PARTITION BY data ORDER BY data ) AS nr
               FROM     @tempData
             )
    SELECT  data
    FROM    numbered
    WHERE   nr > 1



Comments

Popular posts from this blog

Change Key Dynamically in Web.Config

System.Xml; private void SetConfigSettings() { string path = Server.MapPath("Web.config"); string newConnectionString = @"Server=local;Database="+txtDatabaseName.Text+";Trusted_Connection=true"; XmlDocument xDoc = new XmlDocument(); xDoc.Load(path); XmlNodeList nodeList = xDoc.GetElementsByTagName("appSettings"); XmlNodeList nodeAppSettings = nodeList[0].ChildNodes; XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes; xmlAttCollection[0].InnerXml = txtKey.Text; // for key attribute xmlAttCollection[1].InnerXml = newConnectionString; // for value attribute xDoc.Save(path); // saves the web.config file }

ASP.Net Page Life Cycle

The ASP.Net life cycle could be divided into two groups: Application Life Cycle Page Life Cycle ASP.Net Application Life Cycle: The application life cycle has the following stages: User makes a request for accessing application resource, a page. Browser sends this request to the web server. A unified pipeline receives the first request and the following events take place: An object of the ApplicationManager class is created. An object of the HostingEnvironment class is created to provide information regarding the resources. Top level items in the application are compiled. Response objects are created . the application objects: HttpContext, HttpRequest and HttpResponse are created and initialized. An instance of the HttpApplication object is created and assigned to the request. The request is processed by the HttpApplication class. Different events are raised by this class for processing the request. ASP.Net Page Life Cycle: When a page is requested, it is loaded...