How To select first row per group with condition [duplicate] - sql

This question already has answers here:
Selecting first row per group
(2 answers)
Closed 6 years ago.
I have the table named DealOffers :
I want to select only one record from each group of dealIds where
Price is minimum.
i.e : the expected output should be like this:

You can do something like this. However, you should consider performance if you end up having to do this on a massive scale.
select *
from (
select *,
SeqNum = row_number() over(
partition by DealId
order by Price)
from DealOffers) do
where do.SeqNum = 1;

Related

Oracle- How to get single row from multiple same type of rows [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)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
SQL Selecting dates with maximum sale for each department [duplicate]
(3 answers)
Closed 1 year ago.
I have below table output from a query.
and I want the final output as below image from the above output. Main goal is to figure out one one rows for each ffi_customer_id for each year based on reported_on and created_on date.
Thanks in advance.
This kind of problem can be solved with analytic functions. You can read more here https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174
For example you select the columns which you want in outer select and then in subquery rank over partition by ffi_customer_id, ref_period_value_code order by created_on and reported_on in descending order. Then select the records where rank is 1.
SELECT ffi_customer_id
,ref_period_value_code
,created_on
,reported_on
,financial_master_id
FROM ( SELECT your_table_name.*
,RANK() OVER(PARTITION BY ffi_customer_id, ref_period_value_code ORDER BY reported_on DESC, created_on DESC) AS "Rank"
FROM (SELECT * FROM your_table_name) AS table2(ffi_customer_id, ref_period_value_code, created_on, reported_on, financial_master_id)) table22) t0 WHERE "Rank" = 1;

Select max(date) does not work if the rows have different values, I only want to fetch the row with the highest date [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)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Closed 1 year ago.
This is the code:
select security,max(dte),close,ask,bid,mid from k$prices
where to_char(dte,'MON-YYYY') = 'JAN-2021'
group by security,close,ask,bid,mid,dte
order by security,dte desc
Below is the result: I only want to get 2 rows, which has the highest date for each security (436 January 5 and 448 January 29) but because the values of the fields are different, all rows are still being shown. Please help me. Thanks
You could rank all your rows first within the inline view t, then select only those that have rank 1 ( rnb = 1)
select security, dte, close, ask, bid, mid
from (
select security, dte, close, ask, bid, mid, row_number()over(partition by security order by dte desc) rnb
from your_table
)t
where rnb = 1
;

select first row that has a given value T-SQL [duplicate]

This question already has answers here:
Delete duplicate records in SQL Server?
(10 answers)
Closed 4 years ago.
I'm trying to remove all rows from a database that duplicate an int (named user_id) in order to keep just the first occurrence. Not sure why my attempts didn't work, and would like an explanation of how you solved the problem even more than a solution.
My attempt (and sample data) http://sqlfiddle.com/#!18/9f6fc/5
End goal:
user_id, PAios_AccountId
123 a
223 b
The easiest way is to use ROW_NUMBER:
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY PAios_AccountId) AS rn
FROM [User]
)
DELETE FROM cte
WHERE rn <> 1;
DBFiddle Demo

top 10 rows in oracle [duplicate]

This question already has answers here:
Oracle SQL - How to Retrieve highest 5 values of a column [duplicate]
(5 answers)
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Closed 6 years ago.
i have 2 tables .
abc(CID(pk), cname,)
order(order_id(pk), CID(fk), number_of_rentals)
i want to fetch top 10 customers based on number of rentals.
SELECT cid, sum(no_rentals) as sum
FROM orders
group by cid, no_rentals
order by no_rentals desc;
how can i use rownum function in above query to fetch the desired output
Just wrap your query in:
SELECT * FROM ( your_query ) WHERE ROWNUM <= 10;
However, your query does not look like it is going to do what you intend as the GROUP BY no_renalts will mean that each distinct no_rentals value will be in its own group and you will not sum the values for each customer so you probably don't want to include it in the GROUP BY. Also, if you want to order by the total number of rentals then you want to ORDER BY SUM( no_rentals ) (or by its alias) like this:
SELECT cid,
SUM(no_rentals) as total_no_rentals
FROM orders
GROUP BY cid
ORDER BY total_no_rentals DESC;
Then you can apply the row limit like this:
SELECT *
FROM (
SELECT cid,
SUM(no_rentals) as total_no_rentals
FROM orders
GROUP BY cid
ORDER BY total_no_rentals DESC
)
WHERE ROWNUM <= 10;

Select last record only if a certain condition is met [duplicate]

This question already has answers here:
Select last records only if a certain condition is met
(2 answers)
Closed 7 years ago.
I have a table that contains an ID, WorkerID, a flag (1 or 0) to show when they are on or off shift and a date when that flag value was logged. I would like to take from this table, for each unique workerID, the latest date a flag was logged (1 or 0) only if the flag of This latest date is equal to 1. Can anyone help?
If I understand correctly, you seem to want the rows of users where the log flag is 1.
This suggests row_number():
select t.*
from (select t.*, row_number() over (partition by workerId order by date desc) as seqnum
from table t
) t
where seqnum = 1 and flag = 1;
Note: your question would be much better with sample data and desired results.