SQL query for latest record [duplicate] - oracle

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.

Related

get the newest order for each group [duplicate]

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.

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.

How to extract only date value from date field in Oracle? [duplicate]

This question already has answers here:
How to correctly handle dates in queries constraints
(4 answers)
Closed 8 years ago.
I am new to Oracle database.I dont have much knowledge about date-time concepts in Oracle.
The problem i am facing is to retrieve records which are entered on a particular date.But when i am executing SQL query on database it returns zero records.
Database has date field which contains records with both datetime value.
SQL Query: SELECT * FROM table WHERE table.daterecord = to_date(03-Mar-2010)
It is not returning any record but if i change my query to
SELECT * FROM table WHERE table.daterecord > to_date(04-Mar-2010)
It will return some records.
The above difference is because of time.How can i extract time value from date. Can I use trunc function for this? Thanks in advance for your valuable suggestions.
Yes you can use TRUNC function.
SELECT *
FROM table
WHERE TRUNC(table.daterecord) = TO_DATE('03-Mar-2010', 'DD-MON-RRRR')
see this SO for suggestions of "How to correctly handle dates in queries constraints"?
In addition to the answers already provided, I would suggest using a range since this is more easily indexable:
SELECT *
FROM table
WHERE table.daterecord >= TO_DATE('03-Mar-2010', 'DD-MON-RRRR')
AND table.daterecord < TO_DATE('04-Mar-2010', 'DD-MON-RRRR')
you can use TRUNC
For example :
SELECT *
FROM table
WHERE TRUNC(table.daterecord) = TO_DATE('03-Mar-2010', 'DD-MON-RRRR')

Resources