How do I preserve the order of a SQL query using the IN command - sql

SELECT * FROM tblItems
WHERE itemId IN (9,1,4)
Returns in the order that SQL finds them in (which happens to be 1, 4, 9) however, I want them returned in the order that I specified in the array.
I know I could reorder them after in my native language (obj c), but is there a neat way to do this in SQL?
Somthing like this would be great:
ORDER BY itemId (9,1,4) -- <-- this dosn't work :)

Probably the best way to do this is create a table of item IDs, which also includes a rank order. Then you can join and sort by the rank order.
Create a table like this:
itemID rank
9 1
1 2
4 3
Then your query would look like this:
select tblItems.* from tblItems
inner join items_to_get on
items_to_get.itemID = tblItems.itemID
order by rank

Use a CASE expression to map the ID values to an increasing sequence:
... ORDER BY CASE itemId
WHEN 9 THEN 1
WHEN 1 THEN 2
ELSE 3
END

I had the same task once in a mysql environment.
I ended up using
ORDER BY FIND_IN_SET(itemID, '9,1,4')
this is working for me since then. I hope it also works for sqlite

You can add a case construct to your select clause.
select case when itemid = 9 then 1
when itemid = 1 then 2 else 3 end sortfield
etc
order by sortfield

You could create a procedure to order the data in SQL, but that would be much more complicated than its native language counterpart.
There's no "neat way" to resort the data like that in SQL -- the WHERE clause of a SELECT simply says "if these criteria are matched, include the row"; it's not (and it cannot be) an ordering criterion.

Related

sqlite query unsorted result

I have list of Ids 31165,31160,31321,31322,31199,31136 which is dynamic.
When I run query
select id,name from master_movievod where id in(31165,31160,31321,31322,31199,31136);
I get following result
31136|Independence Day
31160|Planet of the Apes
31165|Mrs. Doubtfire
31199|Moulin Rouge
31321|Adult Movie 2
31322|Adult Movie 3
This is sorted list in ascending order.
I want the list in the same order which I give as input like
31165|Mrs. Doubtfire
31160|Planet of the Apes
31321|Adult Movie 2
31322|Adult Movie 3
31199|Moulin Rouge
31136|Independece Day
Without an order by clause, there's no guarantee on the order a database returns the results to you. SQLite, unfortunately, doesn't have something like MySQL's field for custom sorting, but you can jimmy-rig something with a case expression:
SELECT id, name
FROM master_movievod
WHERE id IN (31165, 31160, 31321, 31322, 31199, 31136)
ORDER BY CASE ID WHEN 31165 THEN 0
WHEN 31160 THEN 1
WHEN 31321 THEN 2
WHEN 31322 THEN 3
WHEN 31199 THEN 4
WHEN 31136 THEN 5
END ASC
Unfortunately, SQLite does not have an option like MySQL's FIELD for doing a custom ordering. You are left with two options. The first is that you could create a custom table containing the ordering you want and use that to sort. This option isn't very attractive. The second (and easier) option is to use ORDER BY CASE to achieve the order you want:
SELECT id, name FROM master_movievod
WHERE id IN (31165,31160,31321,31322,31199,31136)
ORDER BY
CASE id
WHEN 31165 THEN 0
WHEN 31160 THEN 1
WHEN 31321 THEN 2
WHEN 31322 THEN 3
WHEN 31199 THEN 4
WHEN 31136 THEN 5
END ASC

How can I get sets of 2 entries where each set starts with a different letter and no letter is repeated from a sqlite database?

Like this:
apple
aardvark
banana
bet
cow
car
...
zipper
zoo
Assuming the database has more than just two different entries that start with any of the letters. I was thinking of doing something with TOP and wildcards, but I don't really know enough about SQL to pull this off. What can I do?
You can do this with the substr function and a correlated subquery:
SELECT *
FROM YourTable a
WHERE wordField IN (SELECT wordField
FROM YourTable AS b
WHERE substr(a.wordField ,1,1) = substr(b.wordField ,1,1)
ORDER BY wordField
LIMIT 2)
Demo: SQL Fiddle
You can use the ORDER BY to adjust which 2 records are returned. Like ORDER BY RANDOM() if that's supported.

Subselect in ORDER BY? Valid SQL?

Is this valid SQL? If yes, could you please tell me what it does?
Select *
from MyFirstTable
order by (select min(somefield)
from MySecondTable
where MyFirstTable.id = MySecondTable.id)
A subselect in an "order by", how is that possible?? In effect this SQL query does not sort by a field, but by some value in a row of a field (min). It does not seem logical so sort by anything else other than a field name. But min(somefield) <> somefield! But, yes, this query works and someone at work who teaches me told me this, and i'm sceptical.
Can you tell me what this means? Or just post an equivilant query?
Thanks!
This query orders MyFirstTable by the minimum value of somefield stored in MySecondTable under the same id.
Here's a quick example:
MyFirstTable
id
1
2
3
MySecondTable
id somefield
1 2
1 4
2 1
3 6
3 4
In the above case, your query would return
id
2
1
3
An equivalent query that may make more sense:
SELECT MyFirstTable.ID, MyFirstTable.A, MyFirstTable.B
FROM MyFirstTable
INNER JOIN MySecondTable ON MyFirstTable.ID = MySecondTable.ID
GROUP BY MyFirstTable.ID, MyFirstTable.A, MyFirstTable.B
ORDER BY MIN(MySecondTable.SomeField)

order by in a non-numerical order

I'm trying to order the results of a query in a non alpha-numerical order. For example, if the possible values of a column are '1', '2' and '3'. ORDER BY asc will display rows with '1' first, then rows with '2' and finally rows with '3'. ORDER BY desc will do the oppsite.
What if I want a different order, like 3, 1, 2 or 1, 3, 2 ? Is that possible ?
Thank you
You want to use a case statement in your order by clause.
So for your 3, 1, 2 example, it would look something like this:
ORDER BY
CASE <yourField>
WHEN 3 THEN 1
WHEN 1 THEN 2
WHEN 2 THEN 3
ELSE 4 END ASC
It should be possible, however, you do not specify what order you are looking for. If you are just looking for a random order you can use ORDER BY NEWID().
you can add your own column named as example my_order
and then order by my_order
You don't really have an ordering as much as a certain sequence. How would you even specify this? If we think of the collection of rows as an indexed array, then you could make a auxiliary index table:
position refers_to
1 3
2 1
3 2
. .
. .
Then you can join the tables ON(index_table.refers_to = my_table.that_column) ORDER BY index_table.position.

SQL Sort Order by The Order Specified In the Query

Say I have a query "select * from clauses where id in (0,2,5,1,3)" and I actually want the rows returned in the same order they are specified the where clause. The order of the IDs will change from query to query and there is no pattern to the order.
I know it's possible to alter the data model, create temp tables, etc. But trust me that those types of solutions will not work in my situation. I also am unable to alter the order of the result objects in my application code.
I'm also aware that different database engines sort things differently, there are no guarantees in some situations blah blah blah. I just want to no, is this possible?
I'll be using mysql or sql server if that helps :)
On MySQL, you can use FIND_IN_SET:
ORDER BY FIND_IN_SET(id, '0,2,5,1,3')
The most portable means of ordering would be to use a CASE expression:
ORDER BY CASE id
WHEN 0 THEN 1
WHEN 2 THEN 2
WHEN 5 THEN 3
WHEN 1 THEN 4
WHEN 3 THEN 5
END
Select ..
From Clauses
Where Id In(0,2,5,1,3)
Order By Case Id
When 0 Then 1
When 2 Then 2
When 5 Then 3
When 1 Then 4
When 3 Then 5
...
End