Update SQL column with SELECT statement data - sqlite

I am currently working with DB Browser and I have a problem with updating columns.
I have a table which contains two columns: one is filled with strings and the other one is completely empty.
I have trimmed these strings with rtrim():
SELECT rtrim(column1, 'e') FROM table1
Now I want to place these trimmed strings into the empty column:
UPDATE table1 SET column2 = (SELECT rtrim(column1, 'e') FROM table1)
But now it fills the empty column with only the first trimmed string of column1, because it won't see the SELECT statement as a column, but as a string.
I also tried:
INSERT INTO table1 (column2) SELECT rtrim(column1, 'e') FROM table1
But this just doubles the amount of rows, as column2 is not 'empty', but filled with null.
I want to get something like
| column1 | column2 |
---------------------
| apple | appl |
| orange | orang |
| grape | grap |
Any ideas on how to solve this?

Related

Compare multiple numbers row by row of same table in teradata

Here is my table data from which I want to assign values to the record.
Member_ID | Claim_ID | Codes | Pull
123 | Y | 12,23,35,78 | Y
123 | N | 12,35 | Y
123 | N | 23,34 | N
123 | N | 33,34 | N
I am using the teradata to assign 'Y' or 'N' to Pull depending on the codes and claims.
SEL A.MEMBER_ID,A.CLAIM_ID,A.CODES,
'Y' AS PULL
FROM (SEL * FROM DBC.PULL_COMP WHERE CLAIM_ID='Y') A
INNER JOIN ((SEL * FROM DBC.PULL_COMP WHERE CLAIM_ID='N') B
ON A.MEMBER_ID=B.MEMBER_ID
UNION
SEL B.MEMBER_ID,B.CLAIM_ID,B.CODES,
CASE WHEN OREPLACE(A.CODES,B.CODES,B.CODES)=A.CODES THEN 'Y'
ELSE 'N' END AS PULL
FROM (SEL * FROM DBC.PULL_COMP WHERE CLAIM_ID='Y') A
INNER JOIN ((SEL * FROM DBC.PULL_COMP WHERE CLAIM_ID='N') B
ON A.MEMBER_ID=B.MEMBER_ID
If the Claim_id is 'Y' the Pull will remain 'Y'. I want to compare the records whose claim_id is 'Y' with those whose claim_id id 'N'. The second record contains no new numbers when comparing with 1st record so Pull='Y'. The 3rd record contains one new number(34) hence Pull='N'. The 4th record contains all new numbers compared to 1st record hence 'N'. Even if there is one new number then Pull='N'. If all the numbers(Codes) of Claim_id='N' matches with the Codes of Claim_id='Y' then only Pull='Y'. I am populating the Pull column looking at member_id, claim_id and codes.
I am getting not the desired result with above query.

Sqlite INSERT OR REPLACE is incrementing rowid

I am using the INSERT OR REPLACE syntax to either insert or update a row that may exist based on a unique column constraint within the table.
The table structure is
| Name | Age | CustomerId (unique) |
When I print out the table the first time, I get something like
| Name | Age | CustomerId | rowid |
|"Bob" | 22 | 5 | 1 |
Then I run INSERT OR REPLACE INTO MyTable(Name, Age, CustomerId) VALUES ("Bob", 23, 5);"
Without fail, this will increment the rowid column each time it is run. So now the result is
| Name | Age | CustomerId | rowid |
|"Bob" | 23 | 5 | 2 |
How do I prevent rowid from incrementing?
This is how INSERT OR REPLACE works.
If a violation of a unique index/constraint occurs then the existing row is deleted and the new row is inserted, so a new rowid is assigned for the new row.
If you don't want this to happen you must use UPSERT:
insert into tablename(Name, Age, CustomerId) values ('Bob', 23, 5)
on conflict(CustomerId) do update
set Name = excluded.Name,
Age = excluded.Age;
See the demo.

How to select the next 50 records from a specified row in an SQLite table

Consider that my SQLite table has the following schema,
|'''''''''''''''''''''''''''''''''''''
| unique_id(TXT) | object_data(BLOB) |
|'''''''''''''''''''''''''''''''''''''
| | |
| | |
| | |
''''''''''''''''''''''''''''''''''''''
Assume that the row_id property is enabled, and so my question is, for a given unique_id, is it possible to fetch the next 50 records from the table,
using a single SQL query? (Using the row_id property)
Try getting result through this method.
Select * from table_name [WHERE conditions] Limit 50 offset (Select row_id from table_name where unique_id = x);

Fastest way to generate a change set between 2 sql tables or CSV files

I am looking for a fast way to get the "deltas" between the 2 csv files (2 GB each).
(The CSV files are loaded to SQLite table where the CSV file first line are the column names, the 0th column is a unique key)
The output is supposed to be something along the lines of:
"Inserts" =... , "deletes" , "Updates" from new.csv to old.csv
The 0th column of the CSV can be considered unique
So something along the lines of:
File1:
id | c1 | c2 | c3 | ....
1 | v1 | v2 | v3 |
File2:
id | c1 | c2 | c3 | ....
1 | v1' | v2' | v3' |
When you have text files, the easiest way would be to sort them by their ID values (sort -n) and then use diff.
In SQL, you can get inserted and deleted rows by checking which IDs do not occur in the other table:
SELECT * FROM Table2 WHERE id NOT IN (SELECT id FROM Table1);
SELECT * FROM Table1 WHERE id NOT IN (SELECT id FROM Table2);
To get changed rows (here, with the new values), remove all identical rows with EXCEPT:
SELECT * FROM Table2
EXCEPT
SELECT * FROM Table1;

Sqlite: Insert new record by copying from old entry when inserting in the ID field

I want to create a duplicated entry when I insert a new record with an existing id number.
i.e. when I enter a new row with 2 in the ID field, it will automatically create a new entry by copying all the fields from the row with ID=2
I am using the below trigger but it is not working.
1) the trigger fails to work when inserting an existing number in the ID column
2) when I changed new.ID to other field like new.col_2, the trigger works but an extra entry is also created after the trigger which I don't want it.
How can I solve these two issues or are there any workarounds for them?
Thanks.
CREATE TRIGGER "main"."NewTrigger" BEFORE INSERT ON "record" FOR EACH ROW WHEN new.ID is not NULL
BEGIN
INSERT INTO record (col_1, col_2)
SELECT col_1, col_2 FROM record
WHERE ID = new.ID;
END;
e.g.
original table named "record"
ID | col_1 | col_2
1 | Ada | 20
2 | Ben | 56
3 | Candy | 82
when I insert a new entry like this:
ID | col_1 | col_2
2
the resulting table "record" will become:
ID | col_1 | col_2
1 | Ada | 20
2 | Ben | 56
3 | Candy | 82
4 | Ben | 56
The INSERT statements inserts a new row.
This is not what you want because you are already inserting a row.
What you need to do is to change the values in the row after it has been inserted:
CREATE TRIGGER NewTrigger
AFTER INSERT ON record
FOR EACH ROW
WHEN NEW.ID IS NOT NULL AND NEW.Col1 IS NULL
BEGIN
UPDATE record
SET (Col1, Col2) = (SELECT Col1, Col2
FROM record
WHERE ID = NEW.ID
AND Col1 IS NOT NULL);
WHERE ID = NEW.ID
AND Col1 IS NULL;
END;
(Using a row value requires SQLite 3.15.0 or later.)

Resources