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
 How to combine two tables togeter?

Author  Topic 

CoolMoon2011
Starting Member

31 Posts

Posted - 2012-01-10 : 10:59:38
I have two table (1000+ rows each) with the same structure. I want to put them together. Anyone would like to help me out for this issue? I list a sample for what I want to get in the combined table.
Thank you ahead here!

Table 1:

groupcode runtype
FCPOOL_R_C EV
FCPOOL_R_C EV

Table 2:
groupcode runtype
1_TX_C_N Run
1_OK_C_N Run

I want to combine them like below:

groupcode runtype
FCPOOL_R_C EV
FCPOOL_R_C EV
1_TX_C_N Run
1_OK_C_N Nun

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 11:12:45
[code]
SELECT groupcode,runtype
FROM table1
UNION ALL
SELECT groupcode,runtype
FROM table2
[/code]

use UNION if you want distinct set of values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-01-10 : 11:14:43
create MyNewTable(groupcode varchar(30), runtype varchar(30))

insert MyNewTable
select groupcode, runtype from MyOldTable1
union all
select groupcode, runtype from MyOldTable2



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

CoolMoon2011
Starting Member

31 Posts

Posted - 2012-01-10 : 11:20:40
quote:
Originally posted by visakh16


SELECT groupcode,runtype
FROM table1
UNION ALL
SELECT groupcode,runtype
FROM table2


use UNION if you want distinct set of values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

CoolMoon2011
Starting Member

31 Posts

Posted - 2012-01-10 : 11:22:20
I really appreciate for your great ideas. Thanks all experts here.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 11:27:15
did it work?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

CoolMoon2011
Starting Member

31 Posts

Posted - 2012-01-10 : 11:54:43
yup. It works well. I use union to distinct them. Thank you visakh16.
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


[/quote]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-10 : 11:57:20
quote:
Originally posted by CoolMoon2011

yup. It works well. I use union to distinct them. Thank you visakh16.
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




[/quote]
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-10 : 12:23:38
How about putting them together WITHOUT making redundant and stale data

CREATE VIEW myView99
AS
SELECT groupcode,runtype
FROM table1
UNION
SELECT groupcode,runtype
FROM table2
GO

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -