2

I have a table that has four columns: id, item_number, feature, value.

The table looks like this and has about 5 million entries.

╔════╦═════════════╦═════════╦═══════╗
║ id ║ item_number ║ feature ║ value ║
╠════╬═════════════╬═════════╬═══════╣
║ 1  ║ 234         ║ 5       ║ 15    ║
║ 2  ║ 234         ║ 3       ║ 256   ║
║ 3  ║ 453         ║ 5       ║ 14    ║
║ 4  ║ 453         ║ 4       ║ 12    ║
║ 5  ║ 453         ║ 7       ║ 332   ║
║ 6  ║ 17          ║ 5       ║ 88    ║
║ 7  ║ 17          ║ 9       ║ 13.86 ║
╚════╩═════════════╩═════════╩═══════╝

How can I sort the table so that I can get the item_numbers in descending order based on the feature value?

I am also selecting other feature numbers with their values but I only want to sort by feature number 5.

3
  • 1
    What do you mean by item_numbers in descending order based on the feature value (this based part)? Commented Jul 22, 2015 at 4:53
  • @suslov I am looking for 17, 234, 453 as the result, as 17 has the highest value of 88 for the feature 5 Commented Jul 22, 2015 at 4:56
  • Update OP with the desired result. Commented Jul 22, 2015 at 5:01

3 Answers 3

3

Using order by with desc and where clauses:

select `item_numbers`
from `tbl`
where `feature` = 5
order by `value` desc
Sign up to request clarification or add additional context in comments.

Comments

2

You need to do order by first with feature and then with item_numbers

select * from `table` order by `feature`, `item_numbers` desc

Comments

0

In your query, add

order by item_number desc

If you are trying to query based on a specific feature, so only receive one set of data for a feature at at time, add

where feature = 'feature'

where "feature" is the feature value you want to search for. If you are looking to provide all features but sort them, you can add

order by feature, item_number desc

and you will be give all features in ascending order and together (grouped) then the items_number(s) in descending order

EDIT:: Sounds like from your latest comment, this may be your solution:

SELECT item_number FROM table WHERE feature = '5' ORDER BY value DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.