I'm a new hive learner. My hive editor does not support update statements. I want to update a column in a table. Let's say I have a table like this.
Input: table_A
field_description
value
field1_name
Age
field2_name
Address
But I want to get something like follows. There are more than 100 rows in table_A
Expected output: table_out
field_description
value
field1
Age
field2
Address
How can I do this using common table expressions? Any suggestions are appreciated.
You can use the Insert overwrite method to achieve the desired result -
INSERT OVERWRITE TABLE table_A
SELECT SUBSTR(field_description, 1, INSTR(field_description, '_') - 1), value
FROM table_A;
Related
I need help to write a SQL query that will replace values in one column by another column values in the same table. For example, given the following table, I want to replace values in column 2 by values in column 1. I think the "UPDATE table SET ..." clause would help but don't know how to use it. Can any one help me, please ?
enter image description here
UPDATE table t1
SET column1 = (SELECT column2 FROM table t2 WHERE t1.column1 = t2.column1);
i have created a new column (exe. columnB) in my oracle table1, which i want to fill with values i get from the query:
select substr(acc_no, 10) from table1)
all the values i get from the query above i want to be inserted in columnB of table1.
Any help, how can i achive this? Thank you in advance
You would use update:
update table1
set columnB = substr(acc_no, 10);
You don't actually need to store the column. You can add it to the table as a virtual column:
alter table1 add columnB varchar2(255)
generated always as (substr(acc_no, 10)) virtual;
I have Audit table with more than 50 columns and need to insert desired row (duplicate) by changing just one column value (column CreatedDate, set value to GETDATE()). I know this can be achieved by INSERT INTO SELECT * FROM but as there is more han 50 columns, code would seem messy:
INSERT INTO Audit_Table (Col1, Col2, .....CreatedDate, ......, Col50, ...ColN)
SELECT (Col1, Col2, .....GETDATE(), ......, Col50, ...ColN) FROM Audit_Table
WHERE Audit_Table.Id = Desired_Id
If i shouldn't change CreatedDate column value, it would be very simple:
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
Is there any other way to duplicate row and change only one/desired column value?
You can insert the record into a temporary table, update the CreatedDate column to GETDATE(), then insert it into the Audit_Table.
No. There is no way to say * except column_foo in SQL.
The workaround would be to generate the
SELECT
col1
, col2
, [...]
, coln
FROM foo;
statement (or parts of it) by querying the database's system catalogue for the column names in their order. There is always a table with all tables and a table with all columns.
Then, make sure you put the necessary commas in the right place (or remove them where you don't need them, or generate the comma in all rows of the report but the first - by using the ROW_NUMBER() OLAP function and evaluating whether it returns 1 or something else). Finally, edit the right date column, by replacing it with CURRENT_DATE or whatever your database uses for the current day.
Good luck -
Marco
You can build upon your existing idea. Just duplicate the row (I assume, you have an auto-incrementing primary key column) and then in a separate query update the time i.e.
Do this :
INSERT INTO Audit_Table SELECT * FROM Audit_Table WHERE Audit_Table.ID = Desired_Id
And then :
UPDATE Audit_Table SET CreatedDate = GETDATE() WHERE primaryKeyID = newPrimaryKeyID
Hope this helps!!!
try below as reference
you can use below statement to copy the all rows,
mysql> insert into newstudent select * from students;
you can use below statement to copy the specific row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248;
you can use below statement to copy the either of the row from table,
mysql> insert into newstudent
-> select id, name, age, address
-> from students
-> where
-> id = 1248 or id=1249;
use limit clause also along with this
I want to select some data using simple sql and insert those data into another table. Both table are same. Data types and column names all are same. Simply those are temporary table of masters table. Using single sql I want to insert those data into another table and in the where condition I check E_ID=? checking part. My another problem is sometime there may be any matching rows in the table. In that time is it may be out sql exception? Another problem is it may be multiple matching rows. That means one E_ID may have multiple rows. As a example in my attachment_master and attachments_temp table has multiple rows for one single ID. How do I solve those problems? I have another problem. My master table data can insert temp table using following code. But I want to change only one column and others are same data. Because I want to change temp table status column.
insert into dates_temp_table SELECT * FROM master_dates_table where e_id=?;
In here all data insert into my dates_temp_table. But I want to add all column data and change only dates_temp_table status column as "Modified". How should I change this code?
You could try this:
insert into table1 ( col1, col2, col3,.... )
SELECT col1, col2, col3, ....
FROM table2 where (you can check any condition here on table1 or table2 or mixed)
For more info have a look here and this similar question
Hope it may help you.
EDit : If I understand your requirement properly then this may be a helpful solution for you:
insert into table1 ( col-1, col-2, col-3,...., col-n, <Your modification col name here> )
SELECT col-1, col-2, col-3,...., col-n, 'modified'
FROM table2 where table1.e_id=<your id value here>
As per your comment in above other answer:
"I send my E_ID. I don't want to matching and get. I send my E_ID and
if that ID available I insert those data into my temp table and change
temp table status as 'Modified' and otherwise don't do anything."
As according to your above statements, If given e_id is there it will copy all the columns values to your table1 and will place a value 'modified' in the 'status' column of your table1
For more info look here
You can use merge statement if I understand your requirement correctly.
Documentation
As I do not have your table structure below is based on assumption, see whether this cater your requirement. I am assuming that e_id is primary key or change as per your table design.
MERGE INTO dates_temp_table trgt
USING (SELECT * FROM master_dates_table WHERE e_id=100) src
ON (trgt.prm_key = src.prm_key)
WHEN NOT MATCHED
THEN
INSERT (trgt.col, trgt.col2, trgt.status)
VALUES (src.col, src.col2, 'Modified');
More information and examples here
insert into tablename( column1, column2, column3,column4 ) SELECT column1,
column2, column3,column4 from anothertablename where tablename.ID=anothertablename.ID
IF multiple values are there then it will return the last result..If not you have narrow your search..
If I type:
INSERT INTO table_b
SELECT rowid, somecolumn
FROM table_a
...the rowid column would copy into new table as ordinary column and this would most likely produce an error since columns wouldn't match.
But is there a way to copy exactly the same rowids from old table to new when I'm populating it fresh ?
I know it is possible to do it that way:
INSERT INTO table_b
(rowid, othercolumn)
VALUES (334, 'sometext')
...but that needs to be write row by row instead of one line sql command.
The first SQL you write is correct and will copy all information matching the columns.
You can also use a query like this:
INSERT INTO table2( rowId, rowValue)
SELECT rowId, rowValue FROM table1
Have you tried this:
INSERT INTO table_b (target_name, target_name2)
SELECT rowid, somecolumn
FROM table_a
Should work fine. But I've not done it on sqlite...