0

I have a select query in Oracle SQL which returns the table with the results

Location     count  
 site1       95000
 site2       556900
 site3       65600

I have then used the floor function (floor/1000) to obtain last 3 digits and append it with K+ using floor(count/1000) || 'K+' as count . Once done with that, am getting a table like this

Location    count  
site1       95K+
site2       556K+
site3       65K+

Now the issue is, when am sorting the result table using the order by count desc it is sorting like this

Location    count  
site1       95K+
site3       65K+
site2       556K+

The sorting is happening by considering the 1st digit I guess. I need the result to be generated like this while sorting in descending order.

Location    count  
site2       556K+
site1       95K+
site3       65K+

Is there a way to achieve this result? Any suggestions

2
  • 1
    Edit your question and show the query you are using. Commented Sep 11, 2018 at 12:35
  • ORDER BY the original count, have another column alias for the second "count" Commented Sep 11, 2018 at 12:35

4 Answers 4

1

I believe that you can simply do:

order by count(*) desc

That is, even though you are modifying the count() value in the select, you can still order by the actual count.

Sign up to request clarification or add additional context in comments.

Comments

1

Try below query:

select location,floor(count/1000)||'K' as count
from tablename
order by floor(count/1000) desc

1 Comment

This should work. The reason behind the odd behavior in the original query is that by concatenating 'K' after your number, you are forcing the DBMS to consider the value a string, and when ordering strings, you just compare letters 1 by 1 instead of as whole like you would with numbers.
0

The formatted value is a string and it will be sorted as string (e.g. 10K+ comes before 2K+ in dictionary order sorted ascending). Place the formatted value in SELECT clause but order by original value in ORDER BY clause:

SELECT Location, FLOOR(count/1000) || 'K+' AS k_formatted_count
FROM ...
ORDER BY count DESC

Comments

0

Although you've already been told what to do (just to repeat: order by count(*) desc), here's an example which shows how you might deal with such values (combined of numbers and letters), generally:

  • you'd extract numeric part (I used REGEXP_SUBSTR, although simple SUBSTR + INSTR would work just fine in this example),
  • apply TO_NUMBER to it (otherwise you'd still be sorting strings and get a wrong result)

SQL> with test (loc, num) as
  2    (select 'site1',  95000 from dual union all
  3     select 'site2', 556900 from dual union all
  4     select 'site3',  65600 from dual
  5    )
  6  select loc,
  7         floor(sum(num) / 1000) || 'K+' cnt    --> SUM represents your COUNT
  8  from test
  9  group by loc
 10  order by to_number(regexp_substr(cnt, '^\d+')) desc;

LOC        CNT
---------- ------------------------------------------
site2      556K+
site1      95K+
site3      65K+

SQL>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.