get the newest order for each group [duplicate] - oracle

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 5 months ago.
I am trying to get the newest order for each phone number so I used in oracle
select * from (select * from orders where phonenum ='914780' order by order_date)
where rownum<=1
it works for one number only if I used it of several number it give me wrong results
as each number has several orders

Use window function for ordering phone number wise orders and then pick each phone number latest record.
Select *
from (
Select o.*,
row_number() over (partition by phonenum order by order_id desc) AS rowno
from order o
Where phonenum = '1233'
) t
Where t.rowno = 1;
N.B.: use table name and columns according to your DB objects.

Related

Trying to display top 3 amount from a table using sql query in oracle 11g..column is of varchar type

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;

Oracle how to do a limit? [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 4 years ago.
I have to limit to 2 rows.
But can't do it for a SQL-Fetch.
select *
from employee;
You can use something like this:
select *
from
( select *
from emp
order by data desc )
where ROWNUM <= 2;
You can change the query as:
select *
from
top_n_test
order by
num
fetch first 3 rows only;
The select first n rows only selects the first n rows.
Well, the simplest way is to
select *
from employee
where rownum <= 2;
but the question is what exactly do you want to do with that.

Oracle rownum = 1 to select topmost row from the set fails [duplicate]

This question already has answers here:
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
How do I do top 1 in Oracle? [duplicate]
(9 answers)
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 5 years ago.
I need to select from two tables,
RATING_TABLE
RATING_TYPE RATING_PRIORITY
TITAN 1
PLATINUM(+) 1
PLATINUM 2
DIAMOND(+) 3
DIAMOND 3
GOLD 4
SILVER 4
RATING_STORAGE
RATING AMOUNT
SILVER 200
GOLD 510
DIAMOND 850
PLATINUM(+) 980
TITAN 5000
I want to select the rating from RATING_STORAGE table based on RATING_PRIORITY from RATING_TABLE.
I want to select one row with lowest rating priority. If two rating priority are eqaul I want to choose the one with the lowest amount.
So I used the query,
select s.rating,s.amount
from RATING_TABLE r, RATING_STORAGE s
where r.rating_type= s.rating_type
and rownum=1
order by r.rating_priority asc , s.amount asc ;
I am getting correct output when sorting the result but rownum=1 fails to give the topmost row.
Thanks in Advance.
You need to select after sorting is done, in your case:
select *
from (select s.rating
,s.amount
from rating_table r
,rating_storage s
where r.rating_type = s.rating_type
and rownum = 1
order by r.rating_priority asc
,s.amount asc)
where rownum = 1;

grouping while querying oracle database [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
Below is the query that i am using. but
Query:
SELECT c.session_id session_number,
u.first_name first_name,
u.last_name last_name,
Min(c.timestamp) session_start_ts,
Max(c.timestamp) session_end_ts,
Count(
CASE
WHEN c.message_type='IN' THEN 1
ELSE NULL
END) message_in_count,
Count(c.message_type) total_messages
FROM users u
join chatlog c
ON u.user_id = c.user_id
WHERE Trunc(c.timestamp) BETWEEN To_date('2017-10-11','YYYY-MM-DD') AND To_date('2017-11-09','YYYY-MM-DD')
GROUP BY c.session_id,
order by timestamp;
The problem is that it gives an error stating "not a GROUP BY expression". But instead of just grouping by session id if i use :
group by c.session_id, u.first_name, u.last_name, c.timestamp
It works though the values of first_name and last_name are same for a particulat session_id and timestamp also i am only taking the max. so cant understand why i am unable to group by session_id only.
"cant understand why i am unable to group by session_id only"
The rule of Oracle aggregation functions is that we need to group by all the non-aggregated columns in the projection. In your case that is
group by c.session_id, u.first_name, u.last_name
" how do we overcome that. i mean there must be a way that you can overcome that. so that if you want to select multiple columns and just group by a particular column?"
This doesn't apply in your case. You say:
"the values of first_name and last_name are same for a particulat (sic) session_id"
which means grouping by session_id,first_name,last_name is the same as grouping by session_id alone. But as a general point, we can use analytical functions to aggregate values in a different window from the result set. Find out more.

SQL query for latest record [duplicate]

This question already has answers here:
Oracle select most recent date record
(4 answers)
Closed 5 years ago.
I want to select the latest record from a table as based on a date field (crtn_dt). The query below does not work. Does anyone have an idea how it should be fixed?
select * from parcels
order by crtn_dt desc
where rownum = 1
You'd need to order data in the subquery and filter them in an outer query.
select *
from (
select *
from parcels
order by crtn_dt desc
)
where rownum = 1
order by clause is among last operations to perform.
What your query does, apart from being semantically incorrect, it returns one (thanks to rownum = 1 predicate) arbitrary row, and then applies order by clause to that one row.

Resources