Update the missing incremental number in a data set - plsql

In one of my tables I have a column called 'Priority' which is a number starting from 1. With new data the next number will be added to the new record.
When I deleted a data from the middle there will be a gap in priority. I want to run a plsql update statement so that the mising numbers will be replaced according the priority order that I had before.
original data
Priority | user
1 | A
2 | B
3 | C
4 | D
Then I delete the record B
Priority | user
1 | A
3 | C
4 | D
After Update it should be like
Priority | user
1 | A
2 | C
3 | D

You can do this without PL/SQL:
create table demo
( priority number, username varchar2(20) );
insert all
into demo values (2, 'A')
into demo values (4, 'B')
into demo values (9, 'C')
into demo values (10, 'D')
select * from dual;
merge into demo o
using ( select row_number() over (order by priority) as new_priority
, rowid as row_id
from demo ) n
on (n.row_id = o.rowid)
when matched then update set o.priority = n.new_priority;
select * from demo;
PRIORITY USERNAME
---------- --------------------
1 A
2 B
3 C
4 D

Related

Selecting the n'th range/island of rows where columns have a common value?

I need to select all rows (for a range) which have a common value within a column.
For example (starting from the last row)
I try to select all of the rows where _user_id == 1 until _user_id != 1 ?
In this case resulting in selecting rows [4, 5, 6]
+------------------------+
| _id _user_id amount |
+------------------------+
| 1 1 777 |
| 2 2 1 |
| 3 2 11 |
| 4 1 10 |
| 5 1 100 |
| 6 1 101 |
+------------------------+
/*Create the table*/
CREATE TABLE IF NOT EXISTS t1 (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
_user_id INTEGER,
amount INTEGER);
/*Add the datas*/
INSERT INTO t1 VALUES(1, 1, 777);
INSERT INTO t1 VALUES(2, 2, 1);
INSERT INTO t1 VALUES(3, 2, 11);
INSERT INTO t1 VALUES(4, 1, 10);
INSERT INTO t1 VALUES(5, 1, 100);
INSERT INTO t1 VALUES(6, 1, 101);
/*Check the datas*/
SELECT * FROM t1;
1|1|777
2|2|1
3|2|11
4|1|10
5|1|100
6|1|101
In my attempt I use Common Table Expressions to group the results of _user_id. This gives the index of the last row containing a unique value (eg. SELECT _id FROM t1 GROUP BY _user_id LIMIT 2; will produce: [6, 3])
I then use those two values to select a range where LIMIT 1 OFFSET 1 is the lower end (3) and LIMIT 1 is the upper end (6)
WITH test AS (
SELECT _id FROM t1 GROUP BY _user_id LIMIT 2
) SELECT * FROM t1 WHERE _id BETWEEN 1+ (
SELECT * FROM test LIMIT 1 OFFSET 1
) and (
SELECT * FROM test LIMIT 1
);
Output:
4|1|10
5|1|100
6|1|101
This appears to work ok at selecting the last "island" but what I really need is a way to select the n'th island.
Is there a way to generate a query capable of producing outputs like these when provided a parameter n?:
island (n=1):
4|1|10
5|1|100
6|1|101
island (n=2):
2|2|1
3|2|11
island (n=3):
1|1|777
Thanks!
SQL tables are unordered, so the only way to search for islands is to search for consecutive _id values:
WITH RECURSIVE t1_with_islands(_id, _user_id, amount, island_number) AS (
SELECT _id,
_user_id,
amount,
1
FROM t1
WHERE _id = (SELECT max(_id)
FROM t1)
UNION ALL
SELECT t1._id,
t1._user_id,
t1.amount,
CASE WHEN t1._user_id = t1_with_islands._user_id
THEN island_number
ELSE island_number + 1
END
FROM t1
JOIN t1_with_islands ON t1._id = (SELECT max(_id)
FROM t1
WHERE _id < t1_with_islands._id)
)
SELECT *
FROM t1_with_islands
ORDER BY _id;

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

How to get ID from table A to table B in trigger?

I have two tables, A & B:
TABLE A
id | name
TABLE B
id | name | fk_idA
I want to create trigger AFTER INSERT in TABLE B which updates fk_idA appropriate with the newest id from table A.
An example:
TABLE A
id | name
1 | Andrew
2 | David
TABLE B
id | name | fk_idA
1 | Photo1 | 2
If the ID column is autoincrementing, the latest is the largest one, i.e., the one returned by MAX:
CREATE TRIGGER DefaultAIsLatest
AFTER INSERT ON TableB
FOR EACH ROW
WHEN NEW.fk_idA IS NULL
BEGIN
UPDATE TableB
SET fk_idA = (SELECT MAX(id)
FROM TableA)
WHERE id = NEW.id;
END;

selecting a row based on a number of column values in SQLite

I have a table with this structure:
id | IDs | Name | Type
1 | 10 | A | 1
2 | 11 | B | 1
3 | 12 | C | 2
4 | 13 | D | 3
except id nothing else is a FOREIGN or PRIMARY KEY. I want to select a row based on it's column values that are not PRIMARY KEY. I have tried the following syntax but it yields no results.
SELECT * FROM MyTable WHERE Name = 'A', Type = 1;
what am I doing wrong? What is exactly returned by a SELECT statement? I'm totally new to Data Base and I'm currently experimenting and trying to learn it. so far my search has not yield any results regarding this case.
Use and to add multiple conditions to your query
SELECT *
FROM MyTable
WHERE Name = 'A'
AND Type = 1;

UPDATE multiple rows using SELECT

I have A table and two rows with id=1 and id=2 and their x parameter is 1. I also have B table and two rows with same id 1 and 2. I am trying to update all of the data(column)on B table which has same id with A table whose x parameter is 1.
A table
id | x |
1 | 1 |
2 | 1 |
B table
id | Y |
1 | yes|
2 | yes|
My query is
UPDATE B SET y='No' WHERE B.id=(SELECT A.id FROM A WHERE A.x=1);
The problem is select returns mutliple data and i can only update the first data.
I tried to use JOIN but sqlite gives syntax error near INNER i couldn't find the problem.
UPDATE B SET B.y='No' INNER JOIN A ON B.id=A.id WHERE A.x=1;
Use this:
UPDATE ... WHERE B.id IN (SELECT A.id ...);

Resources