MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 22nd, 2004, 10:15 AM
StarTrek StarTrek is offline
Non-existent
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Europe
Posts: 16 StarTrek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to StarTrek Send a message via AIM to StarTrek Send a message via Yahoo to StarTrek
SQL order results at selection from an array

I have the sql interogation:
Code:
 
select col1, col2 from table 
where col2 in (45,67,23,45,89,90,12,47) 
 


how to obtain in results same order as in array:

col1 col2
-----------------
45
67
23
45
89
90
12
47
---------------

Reply With Quote
  #2  
Old April 23rd, 2004, 05:39 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
if you could dump that array into a temp table w/an identity column...
Code:
CREATE TABLE #tempTable
(
  ID   int IDENTITY(1,1),
  Col1 int
)

INSERT  into #tempTable (ID, Col1)
        (select Col1 from array)

I'm not sure on the above syntax, hopefully someone can clarify. Plus, I have no idea how you actually select data from the array though. But then you could possibly have...

tempTable
----------
ID Col1
1 45
2 67
3 23
4 45
5 89
6 90
7 12
8 47

Then join on it...
Code:
select  Col1, Col2
from    table 
join    tempTable on table.Col1 = tempTable.Col1
where   Col2 in (45,67,23,45,89,90,12,47)
order   by tempTable.ID

Last edited by Username=NULL : April 23rd, 2004 at 06:21 AM.

Reply With Quote
  #3  
Old April 23rd, 2004, 06:21 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
select col1, col2 from table 
where col2 in (45,67,23,45,89,90,12,47) 
order by case
  col2 when 45 then 1 
        when 67 then 2
        when 23 then 3
--        when 45 then 4
        when 89 then 5
        when 90 then 6
        when 12 then 7
        when 47 then 8 end

Reply With Quote
  #4  
Old April 23rd, 2004, 07:14 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Hey man I didn't know you could order by CASE...that's pretty neat, thx for posting. Only thing I see w/that is...what if there were 100 elements in the array?

Also, if you don't mind, minus the 'array', is my syntax right in the CREATE and INSERT statements I posted?

Reply With Quote
  #5  
Old April 23rd, 2004, 07:17 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Well, I didn't intend that the case expression should be written by hand.

Your syntax is okay.

Reply With Quote
  #6  
Old April 23rd, 2004, 07:20 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,310 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 4 h 21 m 49 sec
Reputation Power: 888
your INSERT is off

you must have the same number of columns in parentheses after the table name as you have values

so for your hypothetical example with ID as IDENTITY,

INSERT into #tempTable (Col1)
SELECT Col1 from array


except you've bypassed the problem, because if you could guarantee the sequence that the values would be pulled from the "array" and inserted into the temp table, then you wouldn't need the temp table!

__________________
r937.com | rudy.ca

Reply With Quote
  #7  
Old April 23rd, 2004, 07:56 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Man I'm confused...c y'all later.

Good day.

Last edited by Username=NULL : April 23rd, 2004 at 08:00 AM.

Reply With Quote
  #8  
Old April 23rd, 2004, 08:00 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
edited...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SQL order results at selection from an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway