Delete the values from particular columns and insert new values to that columns - google-bigquery

I need to delete the values from Cost & Remaining column from the below table and insert new values from another table. Can anyone help please?

One possible way is you can delete the columns Cost and Remaining first then you can create it.
CREATE OR REPLACE TABLE `transactions.test_table` AS
SELECT
* EXCEPT (Cost, Remaining)
FROM
`transactions.test_table`;
The above will create the table without those 2 columns. Now you can insert the data from other table by creating the other columns.
A better way to do is as follows:
CREATE OR REPLACE TABLE `transactions.test_table` AS
SELECT
table1.Date, table1.Name, table2.Cost, table2. Remaining
FROM
`transactions.test_table` table1, `transactions.other_table` table2;

Related

How to delete all columns except first five?

I have a table with approximately 90 columns and want to delete all after the 5th. How to delete all columns except first five?
Given that you only want to keep a few columns, the option with least code would be to make a new table with those columns only. You should do this in a transaction to avoid losing data.
Example with two columns:
ALTER TABLE TableName RENAME TO TmpTableName;
CREATE TABLE TableName(Col1 INTEGER, Col2 INTEGER);
INSERT INTO TableName(Col1, Col2) SELECT Col1, Col2 FROM TmpTableName;
DROP TABLE TmpTableName;
Before version 3.35.0, SQLite did not support removing columns, so this was the only possible option.

How to replace all the NULL values in postgresql?

I found the similar question and solution for the SQL server. I want to replace all my null values with zero or empty strings. I can not use the update statement because my table has 255 columns and using the update for all columns will consume lots of time.
Can anyone suggest to me, how to update all the null values from all columns at once in PostgreSQL?
If you want to replace the data on the fly while selecting the rows you need:
SELECT COALESCE(maybe_null_column, 0)
If you want the change to be saved on the table you need to use an UPDATE. If you have a lot of rows you can use a tool like pg-batch
You can also create a new table and then swap the old one and the new one:
# Create new table with updated values
CREATE TABLE new_table AS
SELECT COALESCE(maybe_null_column, 0), COALESCE(maybe_null_column2, '')
FROM my_table;
# Swap table
ALTER TABLE my_table RENAME TO obsolete_table;
ALTER TABLE new_table RENAME TO my_table;

How to delete specific column values in multiple "insert into" query

Sorry if you think my question is too basic, because i'm a newbie.
Lets say i have 1000 lines of query like this
INSERT INTO table (column1,column2,column3) VALUES (1,2,3);
INSERT INTO table (column1,column2,column3) VALUES (3,33,333);
....
I want to delete column2 with its value, Is there any way so that i dont need to delete 1000 rows manualy. Thanks.
Perhaps you want to completely drop the second column:
ALTER TABLE yourTable DROP COLUMN column2;
Although you may have used many statements to build up the data in your table, dropping a particular column only requires a one-liner.

Insert into table sum of different columns with same schema

I´ve the following tables
I want to insert the sum of the columns [01],][02],[03]...etc. Into another table with the same schema, even if in a specific table exist only 1 record.
INSERT INTO TABLE1 VALUES AS SELECT * FROM
TABLE;
ALTER TABLE TABLE1
ADD COLUMN SUM_VALUES NUMBER(20)
DEFAULT( SUM(01,02,...N))
Is this what you are looking for or provide extra detail

Multiple row insert into two tables avoiding loops

I have a set of value which have to be inserted into two tables.Input has say 5 row and I have to insert these 5 rows into table A first.Table A has a identity column.Next i have to insert these 5 rows into table B with an extra column which is the identity from table A.
How this can be done with out using any loops?
Any help will be highly helpful.
INSERT INTO TABLE_A(COL2,COL3)
SELECT COL2,COL3 FROM #TEMP_TAB
set #identityval=##identity
INSERT INTO TABLE_B(COLA,COLB,COLC)
SELECT #identityval,COL2,COL3,COL4 FROM #TEMP_TAB
You cannot insert into multiple tables using a single statment.
What you could do is create an insert trigger on Table A so that after the insert occurs this performs the new insert with the identity of the value inserted into Table A and insert it into Table B.
Here is one solution.
take max of identity column from table TABLE_A
insert new records in table TABLE_A
then insert records on TABLE_B from TABLE_A with Identity greater than last max identity.
Thanks,
Gopal
What you want to do is not possible.
You can get only the value from the last insert using the ##identity variable. This way its possible to add to multiple tables setting the right foreign key without selecting the just inserted row again using a cursor. This approach is not useful when inserting multiple rows at once.
From the documentation:
Use the ##identity global variable to retrieve the last value inserted into an IDENTITY column. The value of ##identity changes each time an insert or select into attempts to insert a row into a table.
Here is a procedure which inserts a single row and you can use the return value to create a reference to the inserted data in another table:
create procedure reset_id as
set identity_insert sales_daily on
insert into sales_daily (syb_identity, stor_id)
values (102, "1349")
select ##identity
select ##identity
execute reset_id