I have a column with values Prefixed with 'C' like C72, C4, C54, C39, When I sort this in desc order, It becomes,
C72, C54, C4, C39
But I wan't it to be
C72, C54, C39, C4
How can I do this ?
Assuming you are using MSSQL SERVER, You can achieve it by using PATINDEX & CAST OR CONVERT. Retrieve only the Numeric part from each values as Substring using PATINDEX and cast the retrieved value to INT using CAST and use Order By
Select *
From
Tablename
ORDER BY
CAST(substring([colname],PATINDEX('%[0-9]%',[colname]),len([colname])) AS INT) DESC
Check this link if you are using SQL Server..
http://www.essentialsql.com/use-sql-server-to-sort-alphanumeric-values/
If you want to order by the number proceeding the letter C in descending order, then you can just substring off the text of the number and cast to an integer:
SELECT col
FROM yourTable
ORDER BY CAST(SUBSTRING(col, 2, LEN(col) - 1) AS INT) DESC
This answer assumes that every value in the column is prefixed by a letter C.
Related
Am trying to list top 3 records from atable based on some amount stored in a column FTE_TMUSD which is of varchar datatype
below is the query i tried
SELECT *FROM
(
SELECT * FROM FSE_TM_ENTRY
ORDER BY FTE_TMUSD desc
)
WHERE rownum <= 3
ORDER BY FTE_TMUSD DESC ;
o/p i got
972,9680,963 -->FTE_TMUSD values which are not displayed in desc
I am expecting an o/p which will display the top 3 records of values
That should work; inline view is ordered by FTE_TMUSD in descending order, and you're selecting values from it.
What looks suspicious are values you specified as the result. It appears that FTE_TMUSD's datatype is VARCHAR2 (ah, yes - it is, you said so). It means that values are sorted as strings, not numbers - and it seems that you expect numbers. So, apply TO_NUMBER to that column. Note that it'll fail if column contains anything but numbers (for example, if there's a value 972C).
Also, an alternative to your query might be use of analytic functions, such as row_number:
with temp as
(select f.*,
row_number() over (order by to_number(f.fte_tmusd) desc) rn
from fse_tm_entry f
)
select *
from temp
where rn <= 3;
These are the field (crane_no) values to be sorted
QC11QC10QC9
I tried the following query:
select * from table order by crane_no DESC
but query results does not give in an order because the field is mixed with staring and number (Example:QC12).
I get following results for above query:
QC9QC11QC10
I want the results to be in order (QC9, QC10, QC11). Thanks
If the data isn't huge, I'd use a regex order by clause:
select
cran_no
from your_table
order by
regexp_substr(cran_no, '^\D*') nulls first,
to_number(regexp_substr(cran_no, '\d+'))
This looks for the numbers in the string, so rows like 'QCC20', 'DCDS90' are ordered properly; it also takes care of nulls.
One approach is to extract the numeric portion of the crane_no columns using SUBSTR(), cast to an integer, and order descending by this value.
SELECT *
FROM yourTable
ORDER BY CAST(SUBSTR(crane_no, 3) AS INT) DESC
Note that I assume in my answer that every entry in crane_no is prefixed with the fixed width QC. If not, then we would have to do more work to identify the numerical component.
select ...
order by to_number( substr( crane_no,3 )) desc
In my table one of column i have a value like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
when i am order by this column its working fine if the row has value up to Y-9 other wise my result is wrong like below.
Y-1
Y-10
Y-11
Y-12
Y-13
Y-14
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
But i want the output like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
How to acheive the above result.i am using oracle database.Any help will be greatly appreciated!!!!!
Assuming the data is in a table t with a column col and the structure is an alphabetical string followed by dash followed by a number, and both the alphabetical and the number are always not NULL, then:
select col from t
order by substr(col, 1, instr(col, '-')), to_number(substr(col, instr(col, '-')+1))
You can use an order by manipulatinng the column content and cast to number eg:
order by substr(col1, 1,2), TO_NUMBER(sustr(col1, 3,10))
I think the good way is to get constant length field
select col from t
order by substr(col, 1, 2)|| lpad(substr(col, 3),5,'0')
it will correct work only with two nondigit simbol in begining of string up to 99999 number
code
select c1,c2,c3,c4,c5,c6
from table
where c5 in ('a', 'b')
From here, I want to split column c5 into two columns and then rank those based on the value they have for c6. One column should be made up of all a results, and the other should be all b results. I have been able to rank them using rank() over, but have been unable to split the columns apart. I haven't gotten the techniques other people have used to work for me.
select c1,c2,c3,c4,c5,c6, rank() over (partition by ... order by case when c5='a' then 1 case when c5='b' then 2 end) as rnk;
I do not understand completely what c5 contains exactly. Replace conditions like when c5='a' in case with yours.
This query does not return what I would expect. Why?
The table:
create table t
(
comment varchar(10),
id int
);
The data:
insert into t values ('C1', 1);
insert into t values ('C2', 2);
insert into t values ('C3', 3);
insert into t values ('C4', 4);
insert into t values ('C5', 5);
insert into t values ('C6', 6);
insert into t values ('C7', 7);
insert into t values ('C8', 8);
insert into t values ('C9', 9);
insert into t values ('C1', 10);
The query:
select distinct comment from t order by id desc limit 8;
The result:
C9
C8
C7
C6
C5
C4
C3
C2
When I leave DISTINCT out, I get the last row, but I want to suppress duplicates.
My workaround returns the correct result set:
select comment from t group by comment order by max(id) desc limit 8;
But I am curious if my query is not valid (T-SQL for example does not allow both a DISTINCT and ORDER BY in one query unless the ordered column is in the select list) ?
I guess, just guess, that C1 is out of selection cuz it has a dupe and also I guess that it SQL-selects this dupe with id 10 instead of 1. Thats why you got ids from 2 to 9 (totally 8 like you'd asked from SQL by limit 8). Please check if my theory is true. I guess its work flow like record with id 1, ok no dupes, next, id2, no dupes, ok..., id 10 - is dupe of id 1, removing record with id 1, adding record with id 10...