If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > My data get's Ordered by default!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-06, 14:24
Jugular Bean Jugular Bean is offline
Registered User
 
Join Date: Jul 2006
Posts: 11
My data get's Ordered by default!

So my problem. I have an item which has specific attributes that the user can select from a list. Now the order of these attributes when being displayed is very important.

To illustrate:
Item: Jason
Attribute1: Eats
Attribute2: Food
Attribute3: To
Attribute4: Live

Now that's the order the user selects so when I display the item with the attributes I want to see "Jason eats food to live"

However the attributes were inserted into the database as follows
Attribute Table
ID | AttributeName
1 Lives
2 To
3 Eat
4 Food

My query is
SELECT AttributeName FROM Attributes WHERE AttributeId = 3 OR AttributeId = 4 OR AttributeId = 2 OR AttributeId = 1

However the recordset returned is automatically ordered by ID. So it becomes "Jason lives to eat food"

How can I prevent this?

PS: Nevermind my grammatical changes to the data
Reply With Quote
  #2 (permalink)  
Old 07-03-06, 14:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
SELECT AttributeName FROM Attributes WHERE AttributeId in ( 3, 4, 2, 1 )
order
by case when AttributeId = 3 then 0 else 1 end
, case when AttributeId = 4 then 0 else 1 end
, case when AttributeId = 2 then 0 else 1 end
, case when AttributeId = 1 then 0 else 1 end
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-03-06, 15:38
Jugular Bean Jugular Bean is offline
Registered User
 
Join Date: Jul 2006
Posts: 11
Thanks a bunch Rudy, that rocked.

If it's not too much to ask, what exactly does the then/else bit do?
Reply With Quote
  #4 (permalink)  
Old 07-03-06, 23:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you would like to see how it works, please run this query --
Code:
SELECT AttributeName
     , case when AttributeId = 3 then 0 else 1 end as sortkey1
     , case when AttributeId = 4 then 0 else 1 end as sortkey2
     , case when AttributeId = 2 then 0 else 1 end as sortkey3
     , case when AttributeId = 1 then 0 else 1 end as sortkey4
  FROM Attributes 
 WHERE AttributeId in ( 3, 4, 2, 1 )
order 
    by case when AttributeId = 3 then 0 else 1 end
     , case when AttributeId = 4 then 0 else 1 end
     , case when AttributeId = 2 then 0 else 1 end
     , case when AttributeId = 1 then 0 else 1 end
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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