Update specific values in more than one column - SQL - 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'

Related

DISTINCT for large table

I have got a very large table and in one column I have some strings like TypeA, TypeB, etc I would like to do a query with CASE operator using that column
CASE WHEN col1 = 'TypeA' Then '25'
WHEN col1 = 'TypeB' Then '28'
...
WHEN col1 = '????' Then '15'
END
but I do not know how many unique values that column has and what are they (they are words/sentences up to 3 words).
I know I could find those unique values by
SELECT DISTINCT col1 FROM table1
or
SELECT col1 FROM table1 GROUP BY col1
but due to the size of table it's executing endlessly
Can I do it in efficient way? I want to find all unique values just from 1 column
It seems one should better create a table with unique values. Then you can join on that table, and the value domain is open-ended. You can then replace the field with a reference to the value table.
As there does not seem to exist an index on col1 DISTINCT on the original table is slow. Have an index / primary key on the col1 of the value table.

TSQL: Creating a duplicate table and modifying values in the column properly?

I have a master table that I wish to keep as it is. I want to duplicate this table then find a specific record with a where clause and set a column value to null. I then want to reinsert it into the duplicated table without anything getting modified in the master table.
Right now these are the steps that I have taken, however, for some reason the changes propagate all the way to the master table:
select * into Table_Duplicate from Table_Master
create view vw_Filtered as
select Col1, null as Col2 from Table_Master where Col1 = 'Condition'
update
set Table_Duplicate.Col2 = vw_Filtered.Col2
inner join vw_Filtered
on Table_Duplicate.Col1 = vw_Filtered.Col1
Once Statement 3) has been executed, when I do:
select * from Table_Master where Col1 = 'Condition'
I get the modified value in Col2 but I want to get the value before updating it.
Please do let me know if there are any other way to achieve this.
I think you're over-complicating this.
I wont pretend I know why you need to do this in the first place, but if I had to do such a thing I would probably do it like this:
SELECT Col1,
IIF(Col1 = 'Condition', null, Col2) As Col2
[,Coln]
INTO Table_Duplicate
FROM Table_Master
Then you get all the process in a single select into statement.

INSERT INTO Table WHERE

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?

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.