how to make conditional ordering in sqlite? - sqlite

I have table with fields task_priority, task_completed_time,task_completed. Completed colume have values 0 and 1 and it is a primary sort. I want to make secondary sort to be priority if completed is 0 and completed_time if completed is 1. How can i get it?

after some typo fixes i made it:
SELECT * FROM task_table
WHERE (task_name LIKE ? )
ORDER BY task_completed ASC,
CASE task_completed
WHEN 0 THEN task_priority
WHEN 1 THEN task_completed_time
END DESC

Related

How to use top 1 from subquery in MariaDB?

Let's say I want to get the list of tickets, and for each ticket I want to find out the date of the latest post. In SQL Server I can do it this way:
select
Tickets.*
(
select top 1 [Date]
from Posts
where TicketId = Tickets.Id
order by [Date] desc
) as LatestPostDate
from Tickets
I realized that we can't use top 1 in MariaDB. And as I searched, we should use limit 1. But this does not work:
select
Tickets.*
(
select `Date`
from Posts
where TicketId = Tickets.Id
order by `Date` desc
limit 1
) as LatestPostDate
from Tickets
You can use LIMIT in a sub-query. You are just missing a comma in your query:
select
Tickets.*, <-- missing comma
(
select `Date`
from Posts
where TicketId = Tickets.Id
order by `Date` desc
limit 1
) as LatestPostDate
from Tickets
In fact sub-queries should always have a LIMIT 1 clause to make sure you always get only one row back.

Teradata auto increment id

I've been working with teradata for couple of months.
However cannot understand how correctly create auto-incrementing ID.
Serfing the internet I found the most clear and well-working for me the way to create id column as auto generated:
CREATE TABLE tbl_emp (
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 1000000
NO CYCLE),
Name VARCHAR(20),
Phone VARCHAR(10));
But it seems that exists another way to do the same by adding 'primary index (id)':
CREATE TABLE tbl_emp (
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 1000000
NO CYCLE),
Name VARCHAR(20),
Phone VARCHAR(10)) primary index (id);
I am a bit confused. What is actual difference between this 2 queries? What will be the result?

Problem inserting database row using last row in sqlite

I'd like to be able to do the following initially and also at anytime.
insert into balance (closing_amount, opening_amount, created, tx_id)
select closing_amount + :value, closing_amount, :date, :tx_id from balance order by id desc limit 1
Basically I'm inserting by using previous values. But if there are no values to begin with, nothing gets inserted.
I could use a union to which works the first time but duplicates on subsequent inserts.
I want to avoid two trips. Is there a way to do this?
Also, the tx_id will always be unique.
I think you want something like this:
insert into balance (closing_amount, opening_amount, created, tx_id)
select coalesce(max(closing_amount), 0) + :value,
coalesce(max(closing_amount), 0),
:date,
:tx_id
from (
select closing_amount
from balance
order by tx_id desc
limit 1
) t;
You only need the last closing_amount, so max(closing_amount) from the subquery, which returns 1 row or none at all, will return that closing_amount or null respectively.
See a simplified demo.

Create trigger on delete that resets auto increment id if the table is empty on sqlite

I want to create a trigger that will reset the auto increment id to 0 if the table have just become empty. I've tried the following:
CREATE TRIGGER reset_autoincrement AFTER DELETE ON temp WHEN count(*) = 0
BEGIN
UPDATE sqlite_sequence SET seq = 0 WHERE name = 'temp';
END
Although the SQL seems correct, it doesn't do what I want.
Any suggestions?
The expression in the WHEN clause is not in the context of the table; count(*) without a table returns 1.
Use a subquery instead:
... WHEN (SELECT count(*) FROM temp) = 0 ..

Is there any simpler way to see if a field has a certain value?

To see if there is a Todd in my database I currently do the following:
SELECT * FROM MyTable WHERE name='Todd' LIMIT 1
I then check the Cursor to see if its size == 1. Is there a way to return a 0 or 1 from the select statement if the condition is false or true, rather than a list of fields?
You can do
SELECT COUNT(*) it_exists
FROM
(
SELECT 1
FROM MyTable
WHERE name = 'Todd'
LIMIT 1
) q;
An inner select guarantees that LIMIT is applied. Meaning if you have hypothetically thousands of matching rows database engine will stop and return results after the first one instead of going through all of them.
Output
| it_exists |
|-----------|
| 1 |
Here is SQLFiddle demo

Resources