How to query a varchar2 field which has numerical values using order by desc in sql - sql

I have table with varchar 2 datatypes column now i want to fetch the records column and duration column in order by desc order but not working wat i expected.
Query :
select * from sampletable2
where code = 'C'
order by duration desc ;
QUERY output looks like below :
KeyNumber ID Code BRANCH records Duration
A907654234 4 C ALA 100 99
A875678235 3 C PHE 30 9
A123456789 1 C HIE 78 45
A907863544 5 C VAL 50 23
what i want is like :
KeyNumber ID Code BRANCH records Duration
A907654234 4 C ALA 100 99
A123456789 1 C HIE 78 45
A907863544 5 C VAL 50 23
A875678235 3 C PHE 30 9
can someone correct me where im going wrong and correct my query?

How about
order by to_number(duration) desc
It should work, if duration contains numbers. Also, if that's so, why do you keep numbers in a VARCHAR2 column?
If column contains something else, and not only numbers, then see whether regular expressions help. For example:
order by to_number(regexp_substr(duration, '\d+')) desc, duration

You can use to_number and on conversion error as follow:
order by to_number(duration default -1 on conversion error) desc
With this solution, you will get all the non number value records at last.

This answers the original version of the question.
I don't see how your query is producing the first result. But if you want to sort by numeric values, then you can convert to a number:
order by to_number(records) desc, to_number(duration) desc ;
That said, a much better approach is to fix the data model. You should not be storing numbers as strings.

Related

How to select last element for each ID

I would like select some elements from the last id
Here an example that I have :
id money
1 200
1 150
1 500
3 50
4 40
4 300
5 110
Here what I would like :
1 500
3 50
4 300
5 110
So like you can see, I took last id and the money who corresponds.
I tried to do a group by id order by id descending with limit 1. But limit 1 is not available in proc sql from sas and it doesn't work.
Thanks in advance
Unlike SAS datasets, SQL tables represent unordered sets. In your case, it looks like you want the maximum value in the second column, in which case you can use aggregation:
proc sql;
select id, max(money)
from t
group by id;
If you actually mean the last row per id based on the ordering in the SAS dataset, I would suggest using a data step instead.

SQL complex grouping "in column"

I have a table with 3 columns (sorted by the first two):
letter
number (sorted for each letter)
difference between current number and previous number of the same letter
I'd like to calculate (with vanlla SQL) a fourth new column RESULT to group these data when the third column (difference of number between contiguos record; i.e #2 --> 4 = 5-1) is greater than 30 marking all the records of this interval with letter-number of the first record (i.e A1 for #1,#2,#3).
Since the difference between contiguos numbers makes sense just for records with the same letter, for the first record of a new letter, the value of differnce is 31 (meaning that it's a new group; i.e. #6).
Here is what I'd like to get as result:
# Letter Number Difference RESULT (new column)
1 A 1 1 A1
2 A 5 4 A1
3 A 7 2 A1
4 A 40 33 A40 (*)
5 A 43 3 A40
6 B 1 31 B1 (*)
7 B 25 24 B1
8 B 27 2 B1
9 B 70 43 B70 (*)
10 B 75 5 B70
Now I can only find the "breaking values" (*) with this query where they get a value of 1:
select letter
,number
,cast(difference/30 as int) break
from table
where cast(difference/30 as int) = 1
Even though I'm able to find these breaking values I can't finish my task.
Can anyone help me finding a way to obtain the column RESULT?
Thanks in advance
FF
As I understand you need to construct the last result column. You can use concat to do that:
SELECT letter
,number
,concat(letter, cast(difference/30 as int)) result
FROM table
HAVING result = 'A1'
after some exercise and a little help from a friend of mine, I've found a possible solution to my sql prolblem.
The only requirment for the solution is that my first record must have a value of 31 in Difference field (since I need "breaks" when Difference > 30 than the previous record).
Here is the query to get the column RESULT I needed:
select alls.letter
,alls.number
,ints.letter||ints.number as result
from competition.lag alls
,(select letter
,number
,difference
,result
from (select letter
,number
,difference
,case when difference>30 then 1 else 2 end as result
from competition.lag
) temp
where result = 1
) ints
where ints.letter=alls.letter
and alls.number>=ints.number
and alls.number-30<=ints.number

Selecting filtered values from Oracle using ROWNUM

I have a requirement wherein i need to find the record number of the records that are returned from the resultset. I know that i can use ROWNUM to get the record number from the resultset but my issue is slightly different. below are the details
Table : ProcessSummary
Columns:
PS_PK ProcessId StepId AsscoiateId ProcessName AssetAmount
145 25 50 Process1 3,500.00
267 26 45 Process2 4,400.00
356 27 70 Process3 2,400.00
456 28 80 90 Process4 780.00
556 29 56 67 Process5 4,500.00
656 45 70 Process6 6,000.00
789 31 75 Process7 8,000.00
Now what i need to do is fetch all the records from the ProcessSummary Table when either of ProcessId OR StepId OR AssociateId is NULL. I wrote the below query
select * from ProcessSummary where ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL
As expected i got 1st, 2nd, 3rd, 6th and 7th records in the resultset that got returned.
Now what i need is to get the records numbers 1,2,3,6,7. I tried to use the ROWNUM as below but i got the values of 1,2,3,4,5 and not 1,2,3,6,7.
select ROWNUM from ProcessSummary where ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL
Is it possible to get the ROWNUM values in the sequence that i want and if yes then can you please let me know how can i do this. Also if ROWNUM cannot be used then what would be the other option that i can use to get the result in the form that i want.
Any help would be greately appericiated as i could not find much on the net or SO regarding this sort of requirement.
Thanks
Vikeng21
rownum is an internal numbering that gives you a row number based on the current query results only, so that numbering is not tied to a specific record, and it will change when you change the data or the query.
But the numbering you ask for is already in your table. It looks like you just need to SELECT PS_PK .. instead. PS_PK is the field in your table that contains the actual number you want.
You can generate a numbering using an analytical function, and then filter that query. You need some fields to order by, though. In this case I've chosen PS_PK, but it can be another field, like ProcessName or a combination of other fields as well.
select
*
from
(select
dense_rank() over (order by PS_PK) as RANKING,
p.*
from
ProcessSummary p)
where
ProcessId IS NULL OR StepId IS NULL OR AsscoiateId IS NULL
So, in this query, first a numbering is calculated for each row that is returned from the inner query. The numbering is returned as the field RANKING. And then the other query filters further, but still will return the field RANKING with the original numbering.
Instead of dense_rank there is also rank and row_number. The differences are subtle, but you can just experiment and read some docs here and here to learn about the differences and see which one fits you best.
Note that this might slow down your query, because the inner query first generates a number for each row in the table (there is no filtering on that level now).

SQL - Min difference between two integer fields

How I can get min difference between two integer fields(value_0 - value)?
value_0 >= value always
value_0 | value
-------------------
15 | 10
12 | 10
15 | 11
11 | 11
Try this:
SELECT MIN(value_0-value) as MinDiff
FROM TableName
WHERE value_0>=value
With the sample data you have given,
Output is 0. (11-11)
See demo in SQL Fiddle.
Read more about MIN() here.
Here is one way:
select min(value_0 - value)
from table t;
This is pretty basic SQL. If you want to see other values on the same row as the minimum, use order by and choose one row:
select (value_0 - value)
from table t
order by (value_0 - value)
limit 1;
The limit 1 works in some databases for getting one row. Others use top 1 in the select clause. Or fetch first 1 rows only. Or even something else.

SELECT RECORD FROM ORACLE DATABASE

I want to extract the "last" inserted record from database, using this type of query
SELECT A.IDOC_NUM, A.SEG_NUM
FROM AGR3PL_LINE A, AGR3PL_IDOC B
WHERE A.IDOC_NUM(+) = B.IDOC_NUM
AND B.IDOC_NUM = '457154' AND B.CREATION_DATE = (SELECT MAX(B.CREATION_DATE) FROM AGR3PL_IDOC B)
ORDER BY A.SEG_NUM DESC;
The result is this:
IDOC_NUM SEG_NUM
457154 9
457154 8
457154 7
457154 6
457154 5
457154 4
457154 3
457154 2
457154 10
457154 1
This is correct, but the entry 10 is last entered, but as you can see the format result is not good, it sholud be 10,9,8..., is it maybe problem with datebase or somethig else???
Well, SEG_NUM is stored as a "string" type (VARCHAR2 or anohter).
Set the column type as NUMBER, and sort will be fine.
ORDER BY TO_NUMBER(A.SEG_NUM) DESC;
Probably SEG_NUM is text type (e.g. VARCHAR), so it is not sorted like a number.
You can change type of this column (it is the best solution, because if it is number it should have numeric type) or you can convert this column type during execution (it can reduce performance):
SELECT A.IDOC_NUM, A.SEG_NUM
FROM AGR3PL_LINE A, AGR3PL_IDOC B
WHERE A.IDOC_NUM(+) = B.IDOC_NUM
AND B.IDOC_NUM = '457154' AND B.CREATION_DATE = (SELECT MAX(B.CREATION_DATE) FROM AGR3PL_IDOC B)
ORDER BY TO_NUMBER(A.SEG_NUM) DESC;
the problem seem to be SEG_NUM is varchar in ur case. So it is treating 10 lower than 2.
Do it this way -
ORDER BY to_number(A.SEG_NUM) DESC