| SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it. |
|
|||
How to order rows in a predefined pattern?The idea is to always have predefined rows at the beginning of a record set.
For example if you have a column: id -------- article_1 article_2 article_3 How can I construct a query to return the rows in the order article_2, article_1, article_3? |
|
|||
|
This is actually a problem I had when working on the articles module for this site.
I wanted the "README" and the "FEEDBACK" articles to always be at the top of the list and the other articles ordered in descending order. This is what I did:
SELECT * FROM articles ORDER BY id = 1 DESC, id = 2 DESC, id DESCI also tried:
SELECT * FROM articles ORDER BY id IN (1,2) DESC, id DESCbut that would place article 2 in front of article 1. |
|
|||
|
In a similar situation, I derived a newid based on the id by padding extra characters in the beginning.
e.g. README, new id was ZZZREADME and FEEDBACK, new id was ZZYFEEDBACK and all other's newid was as is.
Following solution NOT tested.
FUNCTION NewId(Id AS Variant) AS Variant
SELECT * FROM articles ORDER BY NewId(id) DESC |
![]() |
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|