Remove duplicates from two list

Remove duplicates from two list

  

Hi all,

     I have a situation. I have two SQL's. I am appending the results of both SQL's to a local variable(list).

Now I have to remove the duplicate records in the appended local variable(list). Is there a way to do this.


Thanks,

Gowtham

Hi Gowtham,

Why do you not create only one SQL with join between tables?

Regards

Solution

There are at least 2 ways to do that.

The first is to use a single SQL query, using the UNION command, something like:

SELECT ...

UNION

SELECT

Doing this removes the duplicates at the query level, and SQL Server will do the job for you.

The other way to remove duplicates is to do use a For Each loop to check if the items in the second SQL Query already exist on the first on one by one, and only append the ones that don't. Note that this will require a For Each loop inside a For Each loop so it not only will look a bit messy, it will also not scale very well.

Solution

Carlos Ribeiro da Fonseca wrote:

There are at least 2 ways to do that.

The first is to use a single SQL query, using the UNION command, something like:

SELECT ...

UNION

SELECT

Doing this removes the duplicates at the query level, and SQL Server will do the job for you.

The other way to remove duplicates is to do use a For Each loop to check if the items in the second SQL Query already exist on the first on one by one, and only append the ones that don't. Note that this will require a For Each loop inside a For Each loop so it not only will look a bit messy, it will also not scale very well.


I am getting an error in SQL if I use the UNION for two SQL's

Could you share the SQL and the error, please?
Also, what is the Output Structure of your SQL?

Cheers.

I found the issue in the SQL while using the UNION and I have fixed it.


Thanks All for your Valuable inputs.