Geeks Talk

Prepare for your Next Interview




Move columns from different tables to one table

This is a discussion on Move columns from different tables to one table within the SQL forums, part of the Databases category; How to select columns from different tables and move all those columns in one table for example we select sal from dept where sal>1000 (in this we get one ...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-07-2008
Junior Member
 
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ajit99 is on a distinguished road
Move columns from different tables to one table

How to select columns from different tables and move all those columns in one table for example we

select sal from dept where sal>1000
(in this we get one column regarding sal greater than 1000)
select deptno from dept;

overall we get two columns .my qns is how to put these two different columns in one table
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 447
Thanks: 20
Thanked 54 Times in 54 Posts
susarlasireesha is on a distinguished road
Re: Move columns from different tables to one table

Joins are used to join different tables
for about joins follow this link
create table deptwisesal(deptno number(2),sal number(5));


insert into deptwisesal select d.deptno,e.sal from emp e,dept d where e.deptno=d.deptno and e.sal>1000


drop table deptwisesal;
Reply With Quote
  #3 (permalink)  
Old 03-07-2008
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,432
Thanks: 8
Thanked 126 Times in 113 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: Move columns from different tables to one table

You can't move a column of existing table to another table. Just follow as suggested in the previous post for best alternative.
Reply With Quote
  #4 (permalink)  
Old 04-25-2008
Junior Member
 
Join Date: Apr 2008
Location: visakhaptnam
Posts: 18
Thanks: 2
Thanked 2 Times in 1 Post
ushalakshmi is on a distinguished road
Re: Move columns from different tables to one table

u can do this by using select into query.........

For Ex,

Select dno,sal into depsal from dept,emp where sal>1000
select * fom depsal


then we get the new table depsal with dno,sal whose sal >1000........
Reply With Quote
  #5 (permalink)  
Old 04-25-2008
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 64 Times in 63 Posts
krishnaindia2007 is on a distinguished road
Re: Move columns from different tables to one table

Quote:
Originally Posted by ushalakshmi View Post
u can do this by using select into query.........

For Ex,

Select dno,sal into depsal from dept,emp where sal>1000
select * fom depsal


then we get the new table depsal with dno,sal whose sal >1000........
Select into is used in PL/SQL to dump value of a column to a variable. It will not create a new table.

In a single statemt you can do it in the following way
CREATE TABLE deptwisesal AS
SELECT emp.sal, dept.deptno
FROM emp,dept
where emp.deptno = dept.deptno;
Reply With Quote
  #6 (permalink)  
Old 04-28-2008
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,432
Thanks: 8
Thanked 126 Times in 113 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: Move columns from different tables to one table

Quote:
Originally Posted by ushalakshmi View Post
Select dno,sal into depsal from dept,emp where sal>1000
select * fom depsal
that will not create a table for you .
Reply With Quote
  #7 (permalink)  
Old 05-16-2008
Junior Member
 
Join Date: Sep 2007
Location: Pune
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
glakshkar is on a distinguished road
Re: Move columns from different tables to one table

There are 2 condition for create table which consist of columns from 2 table

1. Table having common column to join 2 table
2. Table not having common column to join 2 table

If there is a linking column (common column on the basis of which 2 tables can be connected) between 2 table then u can create table like this

create table dept_emp as
select dept.dname, emp.ename
from emp, dept
where emp.deptno = dept.deptno;


If 2 table from which column is to be taken are not having same column
then u need to use joins

--say for example i have 2 table
create table t1 (num1 number, num2 number);
insert into t1 values(1,1);
insert into t1 values(2,2);
insert into t1 values(3,3);
insert into t1 values(4,4);

create table t2 (str1 varchar2(20), str2 varchar2(20));
insert into t2 values('a','a');
insert into t2 values('b','b');
insert into t2 values('c','c');
insert into t2 values('d','d');

-- To create table with data
create table t1_t2 as
select num1,NULL str1 from t1
UNION
select NULL,str1 from t2;

select * from t1_t2;
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
In my source table I have two columns Geek_Guest Data Warehousing 3 03-26-2008 04:20 AM
Move QTP Script sreekanthmangu QTP 1 08-21-2007 06:41 AM
Joining Tables and mapping columns JobHelper SQL 1 08-20-2007 02:30 PM
How much constraints can a table with 100 columns have Geek_Guest Oracle 3 07-05-2007 08:45 AM
How to move db to another server? Haitalk SQL Server 4 05-03-2007 11:53 AM


All times are GMT -4. The time now is 08:53 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved