473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sort by the values in an IN()?

I am trying to sort by the values in a:
Expand|Select|Wrap|Line Numbers
  1. SELECT idCol FROM myTable WHERE idCol IN (5,7,2,3,16)
So the results would be:
5
7
2
3
16

Now, the results are:
2
3
5
7
16

The values in the IN() are identity column values generated by a Zend search resultant, and are generated in order of weight. This means I must retain that order when querying the database for the rest of the columns associated with the identity column.

Thanks much!!!!
Nov 22 '07 #1
4 1474
deepuv04
227 Expert 100+
I am trying to sort by the values in a:
Expand|Select|Wrap|Line Numbers
  1. SELECT idCol FROM myTable WHERE idCol IN (5,7,2,3,16)
So the results would be:
5
7
2
3
16

Now, the results are:
2
3
5
7
16

The values in the IN() are identity column values generated by a Zend search resultant, and are generated in order of weight. This means I must retain that order when querying the database for the rest of the columns associated with the identity column.

Thanks much!!!!

use

SELECT idCol FROM myTable WHERE idCol IN (5,7,2,3,16) order by idCol
Nov 22 '07 #2
amitpatel66
2,367 Expert 2GB
I am trying to sort by the values in a:
Expand|Select|Wrap|Line Numbers
  1. SELECT idCol FROM myTable WHERE idCol IN (5,7,2,3,16)
So the results would be:
5
7
2
3
16

Now, the results are:
2
3
5
7
16

The values in the IN() are identity column values generated by a Zend search resultant, and are generated in order of weight. This means I must retain that order when querying the database for the rest of the columns associated with the identity column.

Thanks much!!!!
No that is not possible by a simple select query.
You can either SORT ASCENDING or DESCENDING but not random
By the way the IN clause data is hardcoded?
Nov 22 '07 #3
amitpatel66,

It's an ordered list of values returned by Zend Lucene. The order denotes the weight of the result.

So if you search for: "Sports Illustrated", your results might be:
"Sports Store"
"Illustrated Sports Images"
"Sports Illustrated"
"Sports Websites"
etc...

Each is assigned a weight (by Zend). And has a numeric value denoting the value of the corresponding ID column value in the database.

So:
ID | Results Text

837483 | "Sports Store"
82744 | "Illustrated Sports Images"
336764 | "Sports Illustrated"
326183 | "Sports Websites"

Zend returns it by weight. So since "Sports Illustrated" (2336764) is an exact match, it's returned first (but you will notice the ID is NOT first if we sort by ID column -- sorting by ID, in any way, does no good):

336764 | "Sports Illustrated"
837483 | "Sports Store"
326183 | "Sports Websites"
82744 | "Illustrated Sports Images"

I get from Zend: (336764, 837483, 326183, 82744). In that order. Meaning 336764 is closes match, 837483 is next closest, etc.

So I cannot use the ID column as the sort criteria. Only the actual position in the IN() part of the query.

---------

I am going to brute-force it. So if anyone need a less than optimal solution, here's what I am going to do:

1. Make a temp table:
CREATE TABLE TempZend(
IDCol INT,
Position TINYINT
);

2. Stick the Zend results into the table - in order:
INSERT INTO TempZend (336764, 1)
INSERT INTO TempZend (837483, 2)
INSERT INTO TempZend (326183, 3)
INSERT INTO TempZend (82744, 4)

3. Do a JOIN and sort by TempZend.Position.
SELECT * FROM myTable, TempZend WHERE myTable.idCol = TempZend.IDCol ORDER BY TempZend.Position

Sucks, yea. But will work.

If anyone comes up w/ a single kewl SELECT, please post here. I'll keep checking back.

Thanks!!
Nov 22 '07 #4
amitpatel66
2,367 Expert 2GB
amitpatel66,

It's an ordered list of values returned by Zend Lucene. The order denotes the weight of the result.

So if you search for: "Sports Illustrated", your results might be:
"Sports Store"
"Illustrated Sports Images"
"Sports Illustrated"
"Sports Websites"
etc...

Each is assigned a weight (by Zend). And has a numeric value denoting the value of the corresponding ID column value in the database.

So:
ID | Results Text

837483 | "Sports Store"
82744 | "Illustrated Sports Images"
336764 | "Sports Illustrated"
326183 | "Sports Websites"

Zend returns it by weight. So since "Sports Illustrated" (2336764) is an exact match, it's returned first (but you will notice the ID is NOT first if we sort by ID column -- sorting by ID, in any way, does no good):

336764 | "Sports Illustrated"
837483 | "Sports Store"
326183 | "Sports Websites"
82744 | "Illustrated Sports Images"

I get from Zend: (336764, 837483, 326183, 82744). In that order. Meaning 336764 is closes match, 837483 is next closest, etc.

So I cannot use the ID column as the sort criteria. Only the actual position in the IN() part of the query.

---------

I am going to brute-force it. So if anyone need a less than optimal solution, here's what I am going to do:

1. Make a temp table:
CREATE TABLE TempZend(
IDCol INT,
Position TINYINT
);

2. Stick the Zend results into the table - in order:
INSERT INTO TempZend (336764, 1)
INSERT INTO TempZend (837483, 2)
INSERT INTO TempZend (326183, 3)
INSERT INTO TempZend (82744, 4)

3. Do a JOIN and sort by TempZend.Position.
SELECT * FROM myTable, TempZend WHERE myTable.idCol = TempZend.IDCol ORDER BY TempZend.Position

Sucks, yea. But will work.

If anyone comes up w/ a single kewl SELECT, please post here. I'll keep checking back.

Thanks!!
This is for one scenario but what about other scenarios.
One more thing I would suggest is to use ORDER BY ROWID will work.
When you do an insert as mentioned in your previous post, in that order, a ROWID will be assigned to each row and ORDER by that ROWID will do. You need not have seperate column called POSITION.

Try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM myTable, TempZend WHERE myTable.idCol = TempZend.IDCol ORDER BY ROWID
  2.  
Nov 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christian Gruber | last post by:
dear NG, I have a problem sorting the values of a container, multimap<int, pair<int, double> > map; where the key element is the first column and part of the value is the second column. 1 ...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
6
by: Ali Chambers | last post by:
Hi, I have a two arrays that I wish to sort. One is the index array (full of floating point values). The other is a string array: ARRAY 1 ARRAY 2 ------- -------- -1.2 textA 12 textB...
9
by: phillip.s.powell | last post by:
Ok, you have three tables. You're supposed to be able to not only sort (ORDER BY) according to a_name, no problem, but you must also have the ability to sort (ORDER BY) the relationship between...
6
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below...
6
by: max sharma | last post by:
Hi all, I am using hashtable in C# with keys of type string and values of type double. I am not able to find a way to sort them by value (descending to be precise) instead of key. Can someone...
15
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output:...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.