ORACLE SQL find row with max date for each grouping - sql

I am trying to write a query which will return only the rows, which time has the greatest value for each id
Table: positions
id time otherCols...
---------- ----------- ----------
1 1
1 2
3 1
1 3
2 1
3 2
Result should look like:
id time otherCols...
---------- ----------- ----------
1 3
2 1
3 2
I tried grouping by id but I don't know how to sort after that and pick only the top result.

You can use MAX(..) KEEP (DENSE_RANK ..) OVER (PARTITION BY ..) analytic function without need of any subquery :
SELECT MAX(time) KEEP (DENSE_RANK LAST ORDER BY time)
OVER (PARTITION BY id) AS time_max,
p.*
FROM positions p
ORDER BY id
Demo

You can use window functions:
select t.*
from (select t.*,
row_number() over (partition by id order by time desc) as seqnum
from t
) t
where seqnum = 1;
An alternative method is a correlated subquery:
select t.*
from t
where t.time = (select max(t2.time) from t t2 where t2.id = t.id);
This is different from the first query in two respects:
If there are duplicate times for an id, then this returns all rows for an id. You can get that behavior using rank() in the first query.
This will not return NULL id values or ids where the time is uniformly NULL. The first query does.

Related

Unable to get dedupe records with rank

I am trying to dedupe my dataset using rank, but it is not assigning a different number to the second record. What am I doing wrong here?
with get_rank as (
select id, code, rank() over (partition by id order by z.rowid) as ranking
from mytable z
)
select *
from get_rank
where ranking = 1
and id = 72755
ID CODE RANKING
---------- ---- ----------
72755 M 1
72755 M 1
Use row_number():
with get_rank as (
select id, code,
row_number() over (partition by id order by z.rowid) as ranking
from mytable z
)
select *
from get_rank
where ranking = 1 and id = 72755;
It is guaranteed to return a different value for each row.

Extract Max single upper character from table in oracle

I have test input table
-----------------------
RN | IDs
1 | Az
2 | Azz
3 | B
4 | C
5 | a
6 | e
--------------------
I need Output
----------
RN | IDs
4 | C
I need to get Id which is in UPPER case and single(length 1) also that ID should max in sequence. Like in this example C is the output.
Because Size is 1 , case is UPPER and MAX in sequence.
Can you please help me here.
The first step is identifying the ids values you want to consider. One way it to check the length and the character range:
where length(ids) = 1
and ids between 'A' and 'Z'
which makes some assumptions about your character set, or use a regular expression which also makes some assumptions:
where regexp_like(ids, '^[A-Z]$')
or preferably one which doesn't, using a class instead of a fixed range:
where regexp_like(ids, '^[[:upper:]]$')
With your data any of those give you two rows, for B and C.
You then need to find the row with the maximum ids value. You could do a self-join which is a bit wasteful; or use a subquery that calculates a ranking column and then filter on that:
select rn, ids
from (
select rn, ids, rank() over (order by ids desc) as rnk
from your_table
where regexp_like(ids, '^[[:upper:]]$')
)
where rnk = 1;
RN IDS
---------- ---
4 C
or you could use a variant of the max() function with last to do it in one step:
select max(rn) keep (dense_rank last order by IDs) as rn, max(ids)
from your_table
where regexp_like(ids, '^[[:upper:]]$');
RN MAX
---------- ---
4 C
You haven't said what should happen if there is a tie, e.g. two rn values with C. The first of those approaches will show you both - but could be modified to only show one, if you can specify which you want to see. The second will only show you one, and which is indeterminate at the moment as there is no information in the order by on how to break the tie.
Try this:
SELECT MAX(RN) FROM test
WHERE LENGTH(ID)=1 AND (UNICODE(ID) != UNICODE(LOWER(ID))
GROUP BY ID;
Use the ASCII Function
select max(RN) as MAX_ID from yourTableName t1
where length(t1.IDs)=1 and ascii(IDs) between ascii('A') and ascii('Z');
You can do it in this way, enjoy :)
select h.ID, h.DESC
from test_table h
where length(h.DESC) = 1
and upper(h.DESC) = h.DESC
and h.ID = (select MAX(y.ID)
from test_table y
where length(y.DESC) = 1
and upper(y.DESC) = y.DESC)
It's enough to filter out length(IDs)=1 firstly, and ordering with respect to decode(upper(IDs),IDs,0,1), IDs desc
select RN , IDs
from
(
select t.*,
row_number() over (order by decode(upper(IDs),IDs,0,1), IDs desc )
as rn2
from t
where length(IDs)=1
)
where rn2 = 1;
RN IDS
----- -----
4 C
Rextester Demo

Retrieve specific rows without using rownum

Since I cant use rownum in the query, how can i use rowid to get result from 2nd row until 4th row using rowid or other possible solution apart from rownum.
Here is my current query where it will retrieve 2nd and 4th row:
SELECT * FROM Record a
WHERE
2 = (SELECT COUNT (rowid)
FROM Record b
WHERE a.rowid >= b.rowid)
UNION
SELECT * FROM Record a
WHERE
4 = (SELECT COUNT (rowid)
FROM Record c
WHERE a.rowid >= c.rowid);
Maybe there are other better ways to do it? TQ
If you can't use rownum, then use row_number():
SELECT a.*
FROM (SELECT a.*, ROW_NUMBER() OVER (ORDER BY rowid) as seqnum
FROM Record a
) a
WHERE seqnum BETWEEN 2 and 4;
Note: The ?? is for an ordering column. SQL tables represent unordered sets, so there is no concept of a first row or a second row, except in reference to an ordering column. You can use rowid for this purpose.
In Oracle 12c, you would use OFFSET/FETCH:
SELECT a.*
FROM Record a
OFFSET 1 ROWS
FETCH FIRST 3 ROWS ONLY;
I should point out that you can use rownum. You just can't do:
SELECT a.*
FROM Record a
WHERE rownum BETWEEN 2 and 4;
You can use it in a subquery:
SELECT a.*
FROM (SELECT a.*, rownum as seqnum
FROM Record a
) a
WHERE seqnum BETWEEN 2 and 4;
Do note that without an ORDER BY, there is no guarantee that the results come back in any order, including rowid order.
If you want to avoid rownum and row_number, use sum:
select *
from (
select sum(1) over ( order by rowid /* or whatever you need */ ) as rn,
r.*
from record
)
where rn between 2 and 4
The trick is only in the fact that here sum(1) gives the same thing than count(1) or count(rowid) or whatever count on a not null value, and this is the same thing than counting the rows with row_number or rownum.
In this way you use the sum to compute a row_number, without explicitly writing 'row_number' or 'rownum'.
SQL> create table testTab(x) as ( select level from dual connect by level <= 6);
Table created.
SQL> select t.*,
2 count(1) over (order by rowid desc) as count,
3 sum(1) over (order by rowid desc) as sum,
4 row_number() over (order by rowid desc) as rowNumber
5 from testTab t;
X COUNT SUM ROWNUMBER
---------- ---------- ---------- ----------
6 1 1 1
5 2 2 2
4 3 3 3
3 4 4 4
2 5 5 5
1 6 6 6
The external query simply applies the filter.
With Oracle 12c, you can now easily do row limiting. In your scenario you can do something like this:
SELECT *
FROM RECORD
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY
UNION
SELECT *
FROM RECORD
OFFSET 3 ROWS FETCH NEXT 1 ROWS ONLY

Getting all fields from table filtered by MAX(Column1)

I have table with some data, for example
ID Specified TIN Value
----------------------
1 0 tin1 45
2 1 tin1 34
3 0 tin2 23
4 3 tin2 47
5 3 tin2 12
I need to get rows with all fields by MAX(Specified) column. And if I have few row with MAX column (in example ID 4 and 5) i must take last one (with ID 5)
finally the result must be
ID Specified TIN Value
-----------------------
2 1 tin1 34
5 3 tin2 12
This will give the desired result with using window function:
;with cte as(select *, row_number(partition by tin order by specified desc, id desc) as rn
from tablename)
select * from cte where rn = 1
Edit: Updated query after question edit.
Here is the fiddle
http://sqlfiddle.com/#!9/20e1b/1/0
SELECT * FROM TBL WHERE ID IN (
SELECT max(id) FROM
TBL WHERE SPECIFIED IN
(SELECT MAX(SPECIFIED) FROM TBL
GROUP BY TIN)
group by specified)
I am sure we can simplify it further, but this will work.
select * from tbl where id =(
SELECT MAX(ID) FROM
tbl where specified =(SELECT MAX(SPECIFIED) FROM tbl))
One method is to use window functions, row_number():
select t.*
from (select t.*, row_number() over (partition by tim
order by specified desc, id desc
) as seqnum
from t
) t
where seqnum = 1;
However, if you have an index on tin, specified id and on id, the most efficient method is:
select t.*
from t
where t.id = (select top 1 t2.id
from t t2
where t2.tin = t.tin
order by t2.specified desc, id desc
);
The reason this is better is that the index will be used for the subquery. Then the index will be used for the outer query as well. This is highly efficient. Although the index will be used for the window functions; the resulting execution plan probably requires scanning the entire table.

oracle sql wih rownum <=

why below query is not giving results if I remove the < sign from query.Because even without < it must match with results?
Query used to get second max id value:
select min(id)
from(
select distinct id
from student
order by id desc
)
where rownum <=2
student id
1
2
3
4
Rownum has a special meaning in Oracle. It is increased with every row, but the optimizer knows that is increasing continuously and all consecutive rows must met the rownum condition. So if you specify rownum = 2 it will never occur since the first row is already rejected.
You can see this very nice if you do an explain plan on your query. It will show something like:
Plan for rownum <=:
COUNT STOPKEY
Plan for rownum =:
FILTER
A ROWNUM value is not assigned permanently to a row (this is a common misconception). A row in a table does not have a number; you cannot ask for row 2 or 3 from a table
click Here for more Info.
This is from the link provided:
Also confusing to many people is when a ROWNUM value is actually assigned. A ROWNUM value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation. Also, a ROWNUM value is incremented only after it is assigned, which is why the following query will never return a row:
select *
from t
where ROWNUM > 1;
Because ROWNUM > 1 is not true for the first row, ROWNUM does not advance to 2. Hence, no ROWNUM value ever gets to be greater than 1. Consider a query with this structure:
select ..., ROWNUM
from t
where <where clause>
group by <columns>
having <having clause>
order by <columns>;
I think this is the query you are looking for:
select id
from (select distinct id
from student
order by id desc
) t
where rownum <= 2;
Oracle processes the rownum before the order by, so you need a subquery to get the first two rows. The min() was forcing an aggregation that returned only one result, but before the rownum was applied.
If you actually want only the second value, you need an additional layer of subqueries:
select min(id)
from (select id
from (select distinct id
from student
order by id desc
) t
where rownum <= 2
) t;
However, I would do:
select id
from (select id, dense_rank() over (order by id) as seqnum
from student
) t
where seqnum = 2;
Order asc instead of desc
select id from student where rownum <=2 order by id asc;
Why not just use
select id
from ( select distinct id
, row_number() over (order by id desc) x
from student
)
where x = 2
Or even really bad. Getting the count and index :)
select id
from ( select id
, row_number() over (order by id desc) idx
, sum(1) over (order by null) cnt
from student
group
by id
)
where idx = cnt - 1 -- get the pre-last
Or
where idx = cnt - 2 -- get the 2nd-last
Or
where idx = 3 -- get the 3rd
Try this
SELECT *
FROM (
SELECT id, row_number() over (order by id asc) row_num
FROM student
) AS T
WHERE row_num = 2 -- or 3 ... n
ROW_NUMBER