Rename a single column while using select * in oracle - sql

I am selecting all column of my table (eg 40 columns) and i want to rename only one column (e.g col20 ). how can i rename this column in my select query while selecting all column using select *. I don't want to write the name of all column. one more thing i also don't want to change the order of column in my table

Short answer is, you can't.
Either you have to select all the columns individually, using
select col1, col2, col3, ..., col20 as NewCol, ...., col40 from table
or use
select * from table
You can't have both.
Another option is there which you can use like below, but this will add an extra column in the output.
select t.*, t.col20 as NewCol from table t

Related

why dbeaver puts ""(quotes) to my column name?

I created a table in my SQLite database in DBeaver. When I generate SELECT query:
SELECT "col1", col2, col3, col4 FROM mytable;
there is double quotes on my first column name. I directly created it as col1 and in table view it shows as col1. But in SQL query it "col1". There is something that i don't know related to SQLite? col1,3,4 are TEXT by the way.
I rewrite its name in table view but its still same.

Transpose row data to one column based on unique column identifier in SQL

This is a sample table I created in Excel. My desired output is in cell E7.
I wanted a transpose of all my rows in one single column based on the ID in column A. I tried the same in Excel with a VBA, but did not work as my dataset was large. Is there a way I can do this in SQL
Use UNION ALL :
SELECT id, col1 AS guid
FROM table t
UNION ALL
SELECT id, col2
FROM table t
UNION ALL
SELECT id, col3
FROM table t;

archive one table date in another table with archive date in Oracle

i have one table test it has 10 column with 20 rows.
I need to move this data to archive_test table which has 11 column (10 same as test table plus one column is archive date).
when i tried to insert like below its shows error because number of column mismatch.
insert into archive_test
select * from test;
Please suggest the better way to do this.Thanks!
Well, obviously you need to supply values for all the columns, and although you can avoid doing so you should also explicitly state whic value is going to be inserted into which column. If you have an extra column in the target table you either:
Do not mention it
Specify a default value as part of its column definition in the table
Have a trigger to populate it
Specify a value for that column.
eg.
insert into table archive_test (col1, col2, col3 ... col11)
select col1,
col2,
col3,
...
sysdate
from test;
assuming that archive_date is the last column:
INSERT INTO archive_test
SELECT test.*, sysdate
FROM test

PostgreSql Function "Insert data from a table to an other"

I want to make a function in PostgreSQL that would make the following:
First of all read some data from a table lets say “Select col1,col2 from table1”
Then for each row of the above selection I want to make an insert to an other table lets say table2 (that contains some extra columns like date and so on).
For each insertion I want a unique key that starts from a given number and is increased in every new row.
Can someone give me an example about how I can do it?
I need to be more specific
I want to do what is discribed below:
For(every row in table1)
if(table1.col1>0)
insert into table2 (c1,c2,c3,c4) nalues (id,table1.col1,table1.col2,'oposite',current_timestamp)
else if(table1.col1<0)
insert into table2 (c1,c2,c3,c4) nalues (id,table1.col1,table1.col2,'negative',current_timestamp)
id+=1
insert table2(id, col1, col2)
select startingpoint-1+row_number() over (order by col1, col2),
col1,
col2
from table1
You can use select into to do this, just set up the second table with an autoincrementing field and it will give you your unique key.

Exporting Result Set to Another Table?

How can you export a result set given by a query to another table using SQL Server 2005?
I'd like to accomplish this without exporting to CSV?
INSERT INTO TargetTable(Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM SourceTable
insert into table(column1, columns, etc) select columns from sourcetable
You can omit column list in insert if columns returned by select matches table definition. Column names in select are ignored, but recommended for readability.
Select into is possible too, but it creates new table. It is sometimes useful for selecting into temporary table, but be aware of tempdb locking by select into.
SELECT col1, col2, ...
INTO dbo.newtable
FROM (SELECT ...query...) AS x;