Oracle query to find the top 2 item per group [duplicate] - sql

This question already has answers here:
Get top results for each group (in Oracle)
(5 answers)
ORACLE - find a concrete result inside a LOOP (OR SIMILAR) [duplicate]
(1 answer)
Closed 2 years ago.
I want to right a sql query to find the top 2 item per group in oracle.
The table contains:
P_id Price GroupX
1 10 a
2 5 a
3 5 a
4 4 b
5 8 b
6 6 b
Output should be:
P_id Price GroupX
1 10 a
2 5 a
5 8 b
6 6 b
What I have tried is:
Select * from table
group by GroupX
order by price desc
limit 2

How about
Select rnk,P_id, Price, GroupX
from (
select P_id, Price, GroupX,
RANK () OVER (PARTITION BY GroupX
ORDER BY Price DESC
) AS rnk
from table
)
where rnk <= 2;

You can use the ROW_NUMBER() analytic function:
SELECT p_id,
price,
GroupX
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY groupX
ORDER BY Price DESC ) AS rn
FROM table_name t
)
WHERE rn <= 2;

Related

LIMIT by GROUP in SQl (postgresql)

Below is my table students (having 100K+ rows in orignal, showing just a set):
id
name
class
marks
1
Ryan
5
8
2
Max
5
7
3
Max1
5
10
4
Ryan1
6
8
5
Max2
6
10
6
Ryan2
6
7
7
Ryan3
7
8
8
Max3
7
10
9
Ryan4
7
7
I want to fetch two rows per class ( 5 & 7) having marks <= 10 , also sorted by class, marks ASC
So, the expected result will be:-
id
name
class
marks
1
Ryan
5
8
3
Max1
5
10
7
Ryan3
7
8
8
Max3
7
10
To execute below I tried:-
SELECT DISTINCT t_out.class, t_top.marks, t_top.name
FROM students t_out
JOIN LATERAL (
select *
from students t_in
where t_in.class = t_out.class
ORDER BY t_in.id ASC
) t_top ON TRUE
WHERE t_top.marks <= 10
AND (t_out.class = 5 OR t_out.class = 7)
ORDER BY t_top.marks DESC LIMIT 2;
Result on original database:- it's loading since long time
Result on sample :- Error: near line 20: near "(": syntax error
Is 10 the highest marks?
You would use row_number():
select s.*
from (select s.*,
row_number() over (partition by class order by marks desc) as seqnum
from students s
where marks < 10 and class in (5, 7)
) s
where seqnum <= 2
order by class, marks;
Note: Your question is a little confusing. You seem to want two rows with the highest marks per class ordered in descending order by marks.
EDIT:
Based on your comment:
select s.*
from (select s.*,
row_number() over (partition by class order by marks desc) as seqnum,
count(*) over (partition by class) as cnt
from students s
where marks < 10
) s
where seqnum <= 2 and cnt >= 2
order by class, marks;

Find gaps of a sequence in PostgreSQL tables

I have a table invoices with a field invoice_number. This is what happens when i execute select invoice_number from invoice
invoice_number
1
2
3
5
6
10
11
I want a SQL that gives me the following result:
gap_start
gap_end
1
3
5
6
10
11
demo:db<>fiddle
You can use row_number() window function to create a row count and use the difference to your actual values as group criterion:
SELECT
MIN(invoice) AS start,
MAX(invoice) AS end
FROM (
SELECT
*,
invoice - row_number() OVER (ORDER BY invoice) as group_id
FROM t
) s
GROUP BY group_id
ORDER BY start

How to select top 2 values for each id

I have a table with values
id sales date
1 5 "2015-01-04"
1 3 "2015-01-03"
1 1 "2015-01-01"
1 1 "2015-01-01"
2 7 "2015-01-05"
2 6 "2015-01-04"
2 4 "2015-01-03"
3 11 "2015-01-08"
3 10 "2015-01-07"
3 9 "2015-01-06"
3 8 "2015-01-05"
I want to select top two values of each id as shown in desired output.
Desired output:
id sales date
1 5 "2015-01-04"
1 3 "2015-01-03"
2 7 "2015-01-05"
2 6 "2015-01-04"
3 11 "2015-01-08"
3 10 "2015-01-07"
My attempt:
can someone help me with this. Thank you in advance!
select transactions.salesperson_id, transactions.id, transactions.date
from transactions
ORDER BY transactions.salesperson_id ASC, transactions.date DESC;
This can be done using window functions:
select id, sales, "date"
from (
select id, sales, "date",
dense_rank() over (partition by id order by "date" desc) as rnk
from transactions
) t
where rnk <= 2;
If there are multiple rows on the same date this might return more than two rows for the same ID. If you don't want that, use row_number() instead of dense_rank()
row_number() will get what you want.
select * from
(select row_number() over (partition by id order by date) as rn, sales, date from transactions) t1
where t1.rn <= 2

pick all positive least numbers from data set [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)
Oracle group using min date
(3 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Closed 3 years ago.
I have below data in a table
ID AMOUNT DAYS
1 10 1
1 20 2
1 30 3
1 1 4
2 34 1
2 234 2
2 234 3
2 34 4
3 3 1
3 3 2
3 23 3
3 20 4
I want below results as all amounts which have least days of a ID
ID AMOUNT DAYS
1 10 1
2 34 1
3 3 1
Please suggest a sql query to pick this desired output
For your example, you can simply do:
select t.*
from t
where t.days = 1;
If 1 is not fixed, then a correlated subquery is one method:
select t.*
from t
where t.days = (select min(t2.days) from t t2 where t2.id = t.id);
Another method is aggregation:
select t.id, min(t.days) as min_days,
min(t.amount) keep (dense_rank first order by t.days asc) as min_amount
from t
group by t.id;
Of course row_number()/rank() is another alternative.
With an index on (id, days) and a large table, one of the above methods may be faster in practice.
You can use rank() function
select ID, Amount, Days from
(
select rank() over (partition by ID order by days) as rn,
t.*
from tab t
)
where rn = 1;
Demo
First group by id to find the min days for each id and then join to the table
select t.*
from tablename t inner join (
select id, min(days) days
from tablename
group by id
) g on g.id = t.id and g.days = t.days

SQL show only the top 2 comments for each actionid [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting the top n rows within a group by clause
I have the following data:
id actionid commentid
1 1 1
2 1 2
3 1 3
4 2 1
I want to create a sql to only show the top 2 comments for each actionid. In that case I the sql should only return id (1,2,4)
Thanks
SELECT id,
actionid,
commentid
FROM (
SELECT id,
actionid,
commentid,
row_number() over (partition by actionid order by id) rn
FROM your_table
) t
WHERE rn <= 2