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)
 RESOLVED:Literally adding/joining/appending tables

Author  Topic 

Champinco
Yak Posting Veteran

54 Posts

Posted - 2007-01-18 : 01:46:33
Hi guys,
I tried doing a search in the forum and it has probably confused me a little more. i am trying to literally add three tables together that have the same columns. would i do this using an full outer join? i.e:
and i also think my code is wrong as im not sure how to do a full outer on 3 tables?


SELECT July.usage_type_code, July.Billed_date, July.amount,
aug.usage_type_code, aug.Billed_date, aug.amount,
sep.usage_type_code, sep.Billed_date, sep.amount

FROM PJWASH_RDEAL44_JUL06 July FULL OUTER JOIN PJWASH_RDEAL44_aug06 Aug and PJWASH_RDEAL44_sep06 sep
ON july.Billed_date = aug.Billed_date sep.billed_date
ORDER BY july.billed_date ASC

Champinco

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2007-01-18 : 01:54:12
you want a UNION

SELECT fieldList FROM TableA
UNION
SELECT fieldList FROM TableB
UNION
SELECT fieldList FROM TableC
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-18 : 02:07:16
If you want to keep duplicate values, write

UNION ALL

instead of just UNION.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Champinco
Yak Posting Veteran

54 Posts

Posted - 2007-01-18 : 02:10:31
perfect thats right! how do i also resolve this post?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-18 : 02:14:25
View your topic with this link http://www.sqlteam.com/forums/forum.asp?FORUM_ID=30
and then edit your topic header.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Champinco
Yak Posting Veteran

54 Posts

Posted - 2007-01-18 : 17:57:08
just one more quick one. how do i add the result into a new table?
i.e: this doesnt work.

(SELECT July.usage_type_code, July.Billed_date, July.amount FROM pjwash_rdeal44_JUL06 as July
UNION all
SELECT aug.usage_type_code, aug.Billed_date, aug.amount FROM pjwash_rdeal44_AUG06 as Aug
UNION all
SELECT sep.usage_type_code, sep.Billed_date, sep.amount FROM pjwash_rdeal44_SEP06 as Sep
)
into Rdeal44_06

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-01-18 : 18:17:02
select *
into Rdeal44_06
from
(SELECT July.usage_type_code, July.Billed_date, July.amount FROM pjwash_rdeal44_JUL06 as July
UNION all
SELECT aug.usage_type_code, aug.Billed_date, aug.amount FROM pjwash_rdeal44_AUG06 as Aug
UNION all
SELECT sep.usage_type_code, sep.Billed_date, sep.amount FROM pjwash_rdeal44_SEP06 as Sep
) t


Tara Kizer
Go to Top of Page
   

- Advertisement -