INSERT INTO Table WHERE - sql

I am working with Postgres and Python (psycopg2).
I am looking for a way to INSERT data into a table.
Assuming a table with 10 rows. id going from 1 to 10. Taking a row (i.e id = 3) with a WHERE condition, all my columns are filled with some value, except 2 columns (col3 and col4). Meaning col1,col2 and col5 have values in it. col3 and col4 have NULL conditions, explaining why they are empty in the first place.
I would like to fill these 2 columns with some Data.
I am looking for something like:
INSERT INTO table_a (col3,col4) WHERE id = 3 VALUES ...
Bottom line, I would like to find the row I should fill my two empty columns with the Data I would like.

Sounds like you're looking for an update statement, not an insert statement:
UPDATE table_a
SET col3 = 'some_value', col4 = 'some_other_value
WHERE id = 3

I think you want UPDATE, not INSERT:
UPDATE table_a
SET col3 = ?,
col4 = ?
WHERE id = 3;
INSERT inserts new rows into a table. UPDATE updates existing rows.

If the row already exist, you are looking for an update rather than an insert.
UPDATE table
SET col3 = valueY,
col4 = valueX
WHERE id = 3
Does this make sense to you?

Related

SQL - get only colums from a table where not all values are nulls

SQL question:
How do I get all column values from columns where not all values are null?
Table A
COL1 COL2 COL3 COL4 COL5
---------------------------------------
abc 1 NULL NULL NULL
def 2 NULL testA NULL
NULL 3 NULL testB NULL
jkl 4 NULL NULL NULL
I want to get
COL1 COL2 COL4
-----------------------
abc 1 NULL
def 2 testA
NULL 3 testB
jkl 4 NULL
Is there a sql or plsql solution achieve this this?
To avoid answers that are irrelevant: assume I have a million rows.
I want the result to be a view or a result table.
Not a written output.
I found a similar question, but it does not satisfy my need:
How to select columns from a table which have non null values?
The column names can be quickly grabbed through this query
select column_name
from all_tab_columns
where lower(table_name)='tableA' and num_distinct > 0;
I understand I could create a script with a cursor and then loop through it, adding the result to a new table or view.
This is not what I need. I wondered if this could be done using a single query, using pivot/unpivot or something else.
What you are asking for is not possible in plain SQL, unless you know ahead of time which columns only have NULL everywhere. (It seems that you don't want to assume that you know that.)
Which columns are included in the output - how many columns, their names, and in what order they appear - must be hard-coded in the SELECT clause, it can't be determined at runtime. On the other hand, you will only know which columns are all-NULL only after reading the data (meaning, at runtime) - or else you must have that information from an external source.
The only way to do what you seem to want to do is with dynamic SQL. That is an advanced topic, and a technique generally considered a poor business practice.
WHY do you not want to show columns with all-NULL values? Are you sure that requirement is meaningful?
try these steps, it may help:
Create table temp as (Select * from TableA)
Declare NbrRows Number(10);
plsql_block VARCHAR2(1000);
CountNullRows Number (10)
Select count(*) as nbr
into NbrRows
from TableA
Select count(COL1) as nbr
into CountNullRows
from TableA where COL1 is null
if (NbrRows = CountNullRows) then
Alter table Temp drop column COL1
endif
Select count(COL2) as nbr
into CountNullRows
from TableA where COL2 is null
if (NbrRows = CountNullRows) then
Alter table Temp drop column COL2
endif
Select count(COL3) as nbr
into CountNullRows
from TableA where COL3 is null
if (NbrRows = CountNullRows) then
Alter table Temp drop column COL3
endif
...etc...
Do the same thing for all your columns
You have the desired result in the Tem table.

Reject a row based on 2 column values

Below is the output of a simple join query. All the 3 columns are from different tables.
Col1 Col2 Col3
Manual Y-Yes Include
MC Y-Yes Include
Manual Y-Yes Exclude
Manual Y-Yes Exclude
I need to get the rows with 'Include' only if there is no 'Exclude' for the same Col1 value.
If there is no 'Exclude' for the Col1 value, then its fine to display 'Include'.
So the query should not display the first row according to the requirement since the Col1 value 'Manual' has 'Exclude'.
Your sql query should look a lot like what your question would be in English:
You want all the rows where there is no row for the same col1 value that has 'Exclude' in the col3 value, right?
I cannot give exact sql since you do not provide table or column names, but if all three columns were in the same table, it would look like this:
Select * from mytable
where not exists
(select * from mytable
where col1 = t.col1
and col3 = 'Exclude')

Change only one column of one row in SQL

Let's say I have a SQLite database of 1000 rows with 10 columns named col1, col2, ..., col9, id.
How to change only the col7 of a specific row? Here is more or less what I want to achieve:
WITH mytable CHANGE col7 = 'newvalue' WHERE id = '156'
Is it possible to do it in 1 query? i.e. without having to read the whole row first, and repost the whole row, etc.
The syntax you're looking for is an update statement:
UPDATE mytable
SET col7 = 'newvalue'
WHERE id = '156'
As Siyual said (and should make an answer), it's very straightforward:
UPDATE myTable SET col7 = 'newvalue' WHERE id = 156
Here is documentation on SQLite's version of the UPDATE statement.

Update specific values in more than one column - SQL

I have a table that contains two columns, both have email values.
I want to create a query that update a specific data in both columns.
For example if I have two records of the email 'a#aa.aa' in one column and three records of 'a#aa.aa' in the other column I want them both to be updated.
Here for example I want that all the 'g#gg.ggg' will be 'a#aa.aa':
My question is how the query should look like.
The simplest way is to run two update statements:
update table
set col1 = <newval>
where col1 = <oldval>;
update table
set col2 = <newval>
where col2 = <oldval>;
This begs of the question of why two columns are storing the same data. Perhaps you need to review your data structure and use a junction table for this information.
If you insist of one statement:
update table
set col1 = iif(col1 = 'aa', 'bb', col1),
col2 = iif(col2 = 'aa', 'bb', col2)
where col1 = 'aa' or col2 = 'aa'

Updating timestamp column with new value - value of existing column in oracle

I have a table lets say test having three columns objid, col1(Timestamp) and col2(Number). I want to update col1 with (new value - value of col2).
I can use below query for updating a single row -
update test set col1 = TO_TIMESTAMP('"+newDate+" 00.00.00.000000000','DD-MM-YYYY HH24: MI:SS:FF') - (select col2 from test where objid = 1) where objid = 1;
But i am facing problem while updating multiple rows by passing objid using IN.
update test set col1 = TO_TIMESTAMP('"+newDate+" 00.00.00.000000000','DD-MM-YYYY HH24: MI:SS:FF') - (select col2 from test where objid IN (1,2)) where objid IN (1,2);
I am not getting any clues to get it done.
Please help.
Thanks in advance.
Since both columns belong to the same table, you can write your query like this:
update test
set col1 = TO_TIMESTAMP('"+newDate+" 00.00.00.000000000','DD-MM-YYYY HH24: MI:SS:FF') - col2
where objid IN (1,2);