Change only one column of one row in SQL - 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.

Related

Best way to compare three columns in sql Hive

I need to do some comparison through 3 columns containing string dates 'yyyy-mm-dd', in Hive SQL. Please take in consideration that the table has more than 2 million records.
Consider three columns (col1; col2; col3) from table T1, I must guarantee that:
col1 = col2, and both, or at least one is different from col3.
My best regards,
Logically you have an issue.
col1 = col2
Therefore if col1 != col3 then col2 != col3;
There for it's really enough to use:
select * from T1 where col1 = col2 and col1 != col3;
It is appropriate to do this map side so using a where criteria is likely good enough.
If you wanted to say 2 out of the 3 need to match you could use group by with having to reduce comparisons.

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?

Insert Statement for List

I'm not too sure how to describe my SQL Insert statement so I will describe the expected result.
I'm building a data extract list and have a table that I've put all my data into. It's called _MATTER_LIST
What I am trying to Achieve is to have the Client_Number + Col1 combination repeat after every unique COL1+COL2+COL3 combination but not duplicate when there is already a CLIENT_NUMBER+COL1. So the end result would be:
thanks in advance for any tips.
Simple ORDER BY should work for you if i understand. Try this :
select Client_Number, Col1, Col2, Col3 from _MATTER_LIST
order by Client_Number, Col1
I've managed to fix my own issue. I added a unique key for the col1 + col2 + col3 , then make col2 repeat over each combination for example.
The result is: select * from _MATTER_LIST order by COL4, COL5

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')

SQL parametrised query containing multiple options

I would like to write a query
Select col1, col2
from table
where col1 = 'blah' or 'blah2' or 'blah3'
and col2 = 'blah' or 'blah2' or 'blah3'
I am used to writing them like this for a SINGLE option
select
col1, col2
from
table
where
col1 = :col1 and col2 = :col2
Parameters.AddWithValue(":col1", 'blah')
Parameters.AddWithValue(":col2", 'blah')
Now I want to add several options with OR between them and obviously the above code wont work. The SQL is for SQLite. Can anyone suggest how I could do this? I may potential have more then 3 different values for each parameter. I have tried searching but the answer is elusive.
You still have to use complete expressions, i.e., you need to write col1 = or col2 = every time.
Alternative, use IN:
SELECT ... WHERE col1 IN (:c11, :c12, :c13) AND col2 IN (:c21, :c22, :c23);