Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Union making Duplicates

Author  Topic 

tekmonkey
Starting Member

3 Posts

Posted - 2008-03-12 : 05:22:47
Hi -
I am a newbie to t-sql and have an issue that I am not sure is me or sql
I want to merge several tables from several databases.
I have created a union statement:

create view myViewas
select *, ‘1’ as compId, ‘AAA’ as SiteID from ClientAAA.dbo.stats
UNION
(select *, ‘2’ as compID, ‘ABC’ as SiteID from clientABC.dbo.stats
UNION
(select *, ‘3’ as compID, ‘ABD’ as SiteID from clientABD.dbo.stats
UNION
(select *, ‘4’ as compID, ‘ABF’ as SiteID from clientABF.dbo.stats
UNION
(select *, ‘5’ as compID, ‘AGG’ as SiteID from clientAGG.dbo.stats
))))
Its ok till the last statement then they repeat from the 4th line stats.
Any help would be great

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-12 : 05:31:32
Of course!

You add a unique value for each record in same table so that they cannot be distinct (union) with data from other tables.
SELECT	*,
'1' AS CompID,
'AAA' AS SiteID
FROM ClientAAA.dbo.Stats

UNION

SELECT *,
'2' AS CompID,
'ABC' AS SiteID
FROM ClientABC.dbo.Stats

UNION

SELECT *,
'3' AS CompID,
'ABD' AS SiteID
FROM ClientABD.dbo.Stats

UNION

SELECT *,
'4' AS CompID,
'ABF' AS SiteID
FROM ClientABF.dbo.Stats

UNION

SELECT *,
'5' AS CompID,
'AGG' AS SiteID
FROM ClientAGG.dbo.Stats




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

tekmonkey
Starting Member

3 Posts

Posted - 2008-03-12 : 06:03:54
Cheers
- Thanks -- so I took the brackets off and it works
Go to Top of Page
   

- Advertisement -