views:

50

answers:

2
+1  Q: 

how to move data

I have a table with data as following:

id  col1  col2
1    c1
1         c2
2    b1
3         a3
4    e1
4         e2

Now I need to move this data in another table in such a way that the record that has same id goes as a single record in this new table.

so the new table data would look like this:

id     col1    col2
 1       c1      c2
 2       b1
 3               a3
 4       e1      e2

now how do we do that?

+2  A: 
select id, max(col1) col1, max(col2) col2
into NewTable
from [Table] t 
group by id
Denis Valeev
Shouldn't you include the `INSERT INTO` or `CREATE TABLE` that goes with this query? That might make a more complete answer.
S.Lott
@S.Lott Sure, how about 'into NewTable'?
Denis Valeev
That could work. As long as the person can guarantee there is only one record for each id in each column.
HLGEM
Then what? He will lose some intermediate values, but he said that he needs only one row per `id`, so it's inevitable, right?
Denis Valeev
Thank you Denis
mrp
`SELECT INTO` on Oracle is [for populating variables](http://www.dba-oracle.com/t_pl_sql_plsql_select_into_clause.htm), not a table. In SQL Server, the table defined in the INTO can't already exist. And [MySQL doesn't support the `SELECT INTO` syntax](http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html). `INSERT ... SELECT ...` is the most reliable syntax.
OMG Ponies
In Oracle I would write `create table tbl as select...`
Denis Valeev
A: 

i don't have a setup to test with, but something like the following should work:

select a.id,a.col1,b.col2
into fixed_table
from table1 a
inner join table1 b on a.id = b.id
where a.col1 is not null
and b.col2 is not null;

That should create fixed_table from table1. If you want to move the data back into table1, you could do:

drop table1;
select id, col1, col2
from fixed_table
into table1;
Tim Hoolihan