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
 General SQL Server Forums
 New to SQL Server Programming
 Inserting into single table from 4 different table

Author  Topic 

rds207
Posting Yak Master

198 Posts

Posted - 2009-10-19 : 23:49:59
Hi ,

i have 4 tables

table1:PHONE_VERSIONS
table2:SBABUILD_versions
table3:TARGETSBUILTVIEW
table4:SBATARGETSBUILTVIEW

the join condition i have is

phone_versions.phonebuild=targetsbuiltVIEW.phonebuild
and sbabuild_versions.phonebuild=sbatargetsbuilt.phonebuild

Now i have to construct a new table

DW_T_Build_Versions BY union of all the 4 tables data long with a new column, "BuildInd" which should display 'target' for the data coming from PHONE_VERSIONS and 'SBA' for the data coming from SBABUILD_VERSIONS

Please find the below link for the excel sheet for the list of coloumns ...

http://www.yousendit.com/download/Z01QTGs1Y3kxUUR2Wmc9PQ


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-20 : 12:29:42
do you mean this?


SELECT columns...,'Target' AS BuildInd
FROM PHONE_VERSIONS
JOIN TARGETSBUILTVIEW
ON phone_versions.phonebuild=targetsbuiltVIEW.phonebuild
UNION ALL
SELECT columns...,'SBA'
FROM SBABUILD_versions
JOIN SBATARGETSBUILTVIEW
ON sbabuild_versions.phonebuild=sbatargetsbuilt.phonebuild
Go to Top of Page

rds207
Posting Yak Master

198 Posts

Posted - 2009-10-20 : 12:57:40
yes, i can get all the colomns by joining the tables, but my question is how do i create this into a new table with a new column having the name "target" for data from targetsbuiltview and having "sba" for data from sabtargetsbuiltview, please see the excell sheet attached to my main query...

i cannot write create table query by insert these rows and having a specific coloumn with the above criteria..:-(

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-20 : 13:44:42
just use SELECT...INTO


SELECT * INTO Yourdestination table
FROM
(
SELECT columns...,'Target' AS BuildInd
FROM PHONE_VERSIONS
JOIN TARGETSBUILTVIEW
ON phone_versions.phonebuild=targetsbuiltVIEW.phonebuild
UNION ALL
SELECT columns...,'SBA'
FROM SBABUILD_versions
JOIN SBATARGETSBUILTVIEW
ON sbabuild_versions.phonebuild=sbatargetsbuilt.phonebuild
)t
Go to Top of Page
   

- Advertisement -