question

Yunhao L avatar image
0 Likes"
Yunhao L asked martin.j edited

Else condition to a Query when pulling from a list

Hello,

I am trying to pull from a list using the following query

SELECT item WHERE Type = {Model.parameters.SelectedItemType} ORDER BY ageInQueue DESC

This works well if in the list there is actually an object of the specified type. But is it possible to select the oldest object in that list in case there is not an SelectedItemType item in the list? Something like, if the query returns empty, then we select the first item in the list ordered by age in queue


FlexSim 22.1.1
sql queriespullfromlistpullitem
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
martin.j avatar image
0 Likes"
martin.j answered martin.j edited

You could remove your where clause so that Type is also just a ORDER BY:

SELECT item ORDER BY Type = {Model.parameters.SelectedItemType} DESC, ageInQueue DESC

Now if an item in your list is of the correct type the first part of your ORDER BY will return 1 and 0 if the item is not of the correct type, and since we sort in descending order the the results that are 1 should come first and be sorted by your second statement ageInQueue. If no items of the correct type exists all items will return 0 on the first statement, but then they will just be ordered by the second statement - eitherway this query should return an item as long as there are items in the list.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.