I do a small queue to process which process starts first. For this, I use a table in the database. Here is the table structure (I'm mocking SQLite):
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "identifier" VARCHAR NOT NULL , "priority_number" INTEGER DEFAULT 15, "timestamp" DATETIME DEFAULT CURRENT_TIMESTAMP, "description" VARCHAR
I am trying to write SQL to give me the line of the next process. Here are some sample data:
id identifier priority_number timestamp description 1 test1 15 2009-01-20 17:14:49 NULL 2 test2 15 2009-01-20 17:14:56 NULL 3 test3 10 2009-01-20 17:15:03 NULL 4 test4 15 2009-01-20 17:15:08 NULL 5 test5 15 2009-01-20 17:32:23 NULL 6 test6 14 2009-01-20 17:32:30 NULL 7 test7 7 2009-01-20 17:32:38 NULL 8 test8 20 2009-01-20 17:32:57 NULL 9 test9 7 2009-01-21 13:47:30 NULL 10 test10 15 2009-01-21 13:50:52 NULL
If I use this SQL, I can get the data in the correct order:
select * from queue_manager order by priority_number, timestamp;
This will give me the element with the lowest priority number (most important) at the top and in these priority numbers, the earliest in the queue (by timestamp) at the top.
I can run this query and take only the first row, but I would prefer to do this with an SQL query that would give me one line of the process, which is at the top of the queue (in the above examples, the line with id = 7).
I tried to do self-connections and subqueries, but I have to have a mental block - I just canβt understand what is right.
Thanks in advance!
EDIT
I forgot to mention that I'm looking for a database independent query. I mock it in SQlite, but there is a good opportunity to implement this in DB2 or Oracle. I thought to use the "limit 1" operator in my query, but this is different from the different database mechanisms.
sql queue priority-queue
Brianh
source share