SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 05-13-2006, 01:04 PM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default ANY dialect question:

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 05-13-2006, 01:09 PM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default MySQL answer. Re: How to order rows in a predefined pattern?

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 DESC
I also tried:
SELECT * FROM articles ORDER BY id IN (1,2) DESC, id DESC
but that would place article 2 in front of article 1.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-20-2006, 01:12 AM
Umesha N Shetty
 
Posts: n/a
Default Access answer. Re: How to order rows in a predefined pattern?

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
    NewId = id
    IF id = README then
       NewId = "ZZZ" &  id
    end IF
    IF id = FEEDBACK then
       NewId = "ZZY" &  id
    end IF
End FUNCTION

SELECT * FROM articles ORDER BY NewId(id) DESC 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:00 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7