December 16, 2016 at 10:36 am
Hello,
I have a table,where it sits in 7 different databases. it has 7 different country information.
I want to find duplicate records from all the databases how to write the query?
eg: dbo.emp this table structure same in all 7 databases (Note - all these databases are in same server)
December 16, 2016 at 11:04 am
it depends on what you define as a duplicate; a few columns, or every column, including identities and CreatedDates?
also, is a duplicate in two databases, or must be in ALL seven?
EXCEPT and INTERSECT are really handy here to data like this.
here's a crappy example:
--same data in two tables; anybody working for both companies?
SELECT ColumnList FROM [CocaCola].[dbo].[emp]
INTERSECT
SELECT ColumnList FROM [Pepsi].[dbo].[emp]
Lowell
December 16, 2016 at 11:08 am
All the column data should be same in all 7 databases. - basically select *..from table
do we need to use intersect?
December 16, 2016 at 11:20 am
mcfarlandparkway (12/16/2016)
All the column data should be same in all 7 databases. - basically select *..from tabledo we need to use intersect?
you said find duplicates in the first post.
INTERSECT finds items that are the same.
if you want to find things that are NOT the same, you would use EXCEPT, so the same style of example, but using EXCEPT, and SELECT * or SELECT ColumnList where appropriate;
Lowell
December 16, 2016 at 12:23 pm
I want to exclude one column from select list how to do this?
Column is empname(need to exclude from selct *)
December 16, 2016 at 12:28 pm
mcfarlandparkway (12/16/2016)
I want to exclude one column from select list how to do this?Column is empname(need to exclude from selct *)
my example contained the word [ColumnList] as a placeholder for your real columns.
just write out the columns...you can copy paste from a query results is probably the easiest.
SELECT Column1,Column2,Column3
Lowell
December 18, 2016 at 9:58 pm
This was removed by the editor as SPAM
December 18, 2016 at 10:01 pm
mcfarlandparkway (12/16/2016)
Hello,I have a table,where it sits in 7 different databases. it has 7 different country information.
I want to find duplicate records from all the databases how to write the query?
eg: dbo.emp this table structure same in all 7 databases (Note - all these databases are in same server)
What are you going to do once you find the duplicates?
Also, do any of the tables have an IDENTITY column on them? It would be really helpful if you'd post the CREATE TABLE statement for one of the tables along with all constraints and indexes. That's pretty easy to do if your scripting options are setup correctly.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 19, 2016 at 12:58 pm
Can you create a Temporary table and insert all the records into the temporary table and remove duplicates using the CTEs (Common Table Expressions)?
CREATE TABLE #employee (RowID INT IDENTITY(1,1), Country VARCHAR(10))
--ASSUMING YOU ARE INSERTING FROM MULTIPLE DATABASES
INSERT INTO #employee(Country)
VALUES ('CANADA'),('CANADA'),('CANADA'),('UK'),('US'),('FRANCE')
GO
SELECT * FROM #employee
GO
WITH Employee_CTE
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY COUNTRY ORDER BY COUNTRY ) AS RowNum , *
FROM #employee
)
SELECT COUNTRY
FROM Employee_CTE
WHERE RowNum = 1
DROP TABLE #employee
GO
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply