sqlite3: "IN" works, "NOT IN" doesn't - sqlite

This query works:
select id from wagtailcore_pagerevision
where id in (select live_revision_id as id from wagtailcore_page)
This query produces no results:
select id from wagtailcore_pagerevision
where id not in (select live_revision_id as id from wagtailcore_page)
(The difference is in vs. not in.)
... but I can very plainly see by inspection of the data that the result of the second query should be many hundreds of rows.
An index does exist on the live_revision_id column, and id of course is a primary key.

Related

sqlite shift rowid in multiple records

Hello i have an sqlite db with many records like 10540 record they are ordered by creation time , i want to shift like a record in the middle and like to do it automatically
for example :
select * from table1 where id >= 8521;
UPDATE Table1 SET id = id +1 ;
does not work i get Error: Result: UNIQUE constraint failed:
so i want to shift up all records from 8521 to the last record and get place in the 8520 place for example so i can insert my record in that place of table .
even the
id = select max(id)+1
does not work how can i increment the id from last record to the needed record so i can put a place in the records db
A simple update statement would fail, as it would try to create duplicate values in the primary key.
What you can do is this:
First update the column to the negatives of the values they should have:
update table1
set id = -(id + 1)
where id > 8520;
Now there are no duplicates and you just need to update again to the positive values:
update table1
set id = -id
where id < 0;
This will do the trick, but any kind of updating the primary key is not a recommended practice

Rank on SQLite SubQuery

I have a table Person with ID(Guid), FirstName(string), LastName(string) and 3000 entries. What I desperately need is the rank of a certain entry by ID in a sorted query by FirstName and LastName.
So for example: I search for any entries with FirstName or LastName containing the String 'mil' which returns 62 entries sorted. Since I know the ID of an entry somewhere in this result, I need the row_index of this entry.
I tried this with a Temp Table before, but since I'm working with sqlite-pcl for UWP I can't use 'CREATE TEMP TABLE' statements and so on, thus I need a solution in a single query.
PRAGMA temp_store = MEMORY;
DROP TABLE IF EXISTS TempQuery;
CREATE TEMP TABLE TempQuery AS SELECT ID FROM Person WHERE (Firstname LIKE '%mil%' OR LastName LIKE '%%');
SELECT rowid FROM TempQuery WHERE ID = '48a0231a-af41-450d-a291-5912d39119c9' LIMIT 1;

Sqlite3 order by not working for union

I have this simplified example:
CREATE TABLE test1 (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
status TEXT NOT NULL
DEFAULT 'waiting');
CREATE TABLE test2 (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
status TEXT NOT NULL
DEFAULT 'waiting');
And I run this query:
SELECT status
FROM test1
UNION
SELECT status
FROM test2
ORDER BY CASE status
WHEN 'accepted' THEN 1
WHEN 'invited' THEN 2
WHEN 'waiting' THEN 3
WHEN 'cancelled' THEN 4
ELSE 5
END
With hopes of getting combined ordering of 1-5 based on textual content of the field.
However, I get this error:
Error while executing query: 1st ORDER BY term does not match any column in the result set
When I remove either part of the UNION, the query works fine, so it's not related to the "case when" construct.
As far as I can see, both union parts have the same column named 'status', so I don't understand why I can't order the union.
It works in mysql, but I want to get it working in sqlite as well, if possible...
The documentation says:
If the SELECT is a compound SELECT, then ORDER BY expressions that are not aliases to output columns must be exactly the same as an expression used as an output column.
So you have to make the ordering expression an output column:
SELECT status,
CASE ... END AS order_me
FROM test1
UNION ALL
SELECT status,
CASE ... END
FROM test2
ORDER BY order_me
To avoid the duplication, you could use a subquery:
SELECT status
FROM (SELECT status
FROM test1
UNION ALL
SELECT status
FROM test2)
ORDER BY CASE status ...
END

How to fetch first occurance of most active row row in job table with specific deptid,

Eg : Emplid 001 most effective dated row (say 01/01/2013 )is active and belongs to deptid 101.Suppose If he has two more rows prior with same deptid say one on 10/12/2012 and 01/12/2012, Then i needs to retrieve 01/12/2012 rows.So it should be the first row of continous occurances, In case if i have row with 05/12/2012 with other deptid (102), In that case my query should return 10/12/2012 rows, Please help on this
Though not with a left join, this uses a sub-select on minimum effective date to get the data you need - the trick to get the min effdt by deptid is putting the deptid in the sub-select. Note that empl_rcd_nbr is usually used as a limiter in the sub-select (and job2.empl_rcd_nbr - job.empl_rcd_nbr) but you didn't have it in your original select. If you get dup rows, check your empl_rcd_nbr value since it is a primary key:
select job.emplid, job.effdt, job.deptid
from ps_job job
where job.effdt = (select Min(job2.effdt)
from ps_job job2
where job2.emplid = job.emplid
and job2.deptid = job.deptid)
order by job.emplid
To achieve what you need, you will have to pick up the min(effdt) row with deptid same as the deptid of the current max(effdt) row and also, there should not exist a row > the min(effdt) with deptid <> deptid of the min(effdt) row.
A query satisfying the above conditions should help you with your result set.

Delete records which have more than N entries

I have a table called history,
which has three columns.
id, value, timestamp
Id is not a primary key, but the pair (id, timestamp) is unique.
What I would like to do is delete all the older records for a specific ID that exceed a certain limit.
For example if i have these values:
-1,value1,1
-1,value2,2
-1,value3,3
-2,value4,4
-2,value5,5
-2,value6,6
And the limit is 2. After executing the statement i should get something like:
-1,value2,2
-1,value3,3
-2,value4,4
-2,value5,5
-2,value6,6
I think I have it (tried and works for the testcases i had), the answer is:
DELETE FROM history WHERE id = ?1 AND timestamp NOT in (SELECT sourcetime FROM history WHERE id =?1 ORDER BY timestamp DESC LIMIT ?2);
DELETE FROM ... WHERE timestamp < ...
Doesn't work ?

Resources