Tuesday, April 27, 2010

How to remove duplicate rows in sqlserver

IF OBJECT_ID('EmployeeDetails') IS NOT NULL
DROP TABLE EmployeeDetails
CREATE TABLE [dbo].[EmployeeDetails]
(
[Employee] [varchar](10) NULL,
[JoiningDate] [datetime] NULL,
[DeptID] [int] NULL
)
GO
INSERT INTO EmployeeDetails(Employee, JoiningDate, DeptID)
SELECT 'Mukesh','1919-03-18 ',1008
UNION ALL
SELECT 'Kamlesh','1927-03-18 ',91
UNION ALL
SELECT 'Bikram','1927-04-01 ',139
UNION ALL
SELECT 'Mukesh','1919-03-18 ',1008
UNION ALL
SELECT 'Kamlesh','1927-03-25 ',92
UNION ALL
SELECT 'Bikram','1927-03-25 ',108
UNION ALL
SELECT 'Mukesh','1919-04-01 ',150
UNION ALL
SELECT 'Kamlesh','1927-04-01 ', 123
UNION ALL
SELECT 'Bikram','1927-04-01 ', 139
UNION ALL
SELECT 'Mukesh','1919-04-08 ', 168

-- query to check duplicate rows
SELECT Employee, JoiningDate, DeptID,Ranking = row_number() OVER(PARTITION BY Employee, JoiningDate, DeptID ORDER BY NEWID() ASC)
FROM EmployeeDetails

-- query to delete duplicate rows

WITH RemoveDuplicate(Employee, JoiningDate, DeptID, Ranking)
AS(
SELECT Employee, JoiningDate, DeptID,Ranking = row_number() OVER(PARTITION BY Employee, JoiningDate, DeptID ORDER BY NEWID() ASC)
FROM EmployeeDetails
)
DELETE FROM RemoveDuplicate WHERE Ranking > 1

Friday, April 23, 2010

what is the difference between interface and abstract class


what is the difference between interface and abstract class?
Answer: -
(1) An abstract class may contain complete or incomplete methods. Interfaces can contain only the Signature of a method but no body. Thus an abstract class can implement methods but an interface cannot implement methods.
(2) An abstract class can contain fields, constructors, or destructors and implement properties. An Interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
(3) An abstract class cannot support multiple inheritances, but an interface can support multiple
Inheritance. Thus a class may inherit several interfaces but only one abstract class.
(4) A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
(5) Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in Abstract Classes but not in interfaces.
(6) Abstract classes are faster than interfaces.
(7) One class can inherit multiple interfaces but it can't inherit multiple abstract classes

OOPS Concepts

Inherited Methods
A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses
using System;

namespace Polymorphism

{

class A

{

public void Foo()

{

Console.WriteLine("A::Foo()");

}

}

class B : A

{}

class Test

{

static void Main(string[] args)

{

A a = new A();

a.Foo();

B b = new B();

b.Foo();



} } }

Output :-

// output --> "A::Foo()"

// output --> "A::Foo()"


The method Foo() can be overridden in classes B and C:





using System;

namespace Polymorphism

{

class A

{

public void Foo()

{

Console.WriteLine("A::Foo()");

}

}

class B : A

{

public void Foo()

{

Console.WriteLine("B::Foo()");

}

}

class Test

{

static void Main(string[] args)

{

A a;

B b;

a = new A();

b = new B();

a.Foo();

b.Foo();

a = new B();

a.Foo();

}

}

}

Output :-

// output --> "A::Foo()"

// output --> "B::Foo()"

// output --> "A::Foo()"

Optimized Program for Prime number in c#

public bool IsPrime(int x)
{
if (x == 2 x==3)
return true;
else
if (x % 2 == 0 x < 2 x % 3 == 0)
return false;
Double t=Math.Sqrt((Double)x);
for (int i = 3; i {
if (x % i == 0)
return false;
}
return true;
}