SQL - GET VALUE USING MAX [duplicate] - sql

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 4 years ago.
I am trying to get the AMOUNT_APP WHERE DATE _APP is the greatest value.
I wrote this query, but I am having troubles
SELECT AMOUNT_APP
FROM Payments_App
WHERE DATE_APP = MAX(DATE_APP)
I expect to get 5234.34
My Table

Use order by and rownum:
select pa.amount_app
from (select pa.*
from payments_app
order by date_app desc
) pa
where rownum = 1;
Or keep can be quite efficient:
select max(pa.amount_app) keep (dense_rank first order by date_app desc)
from payments_app;

If we suppouse that DATE_APP is an attribute that you could find the most recently updated results.
select AMOUNT_APP FROM Payments_App WHERE DATE_APP = (Select
MAX(DATE_APP) FROM Payments_App) ;

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

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

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;

Select count of duplicated rows returns wrong result [duplicate]

This question already has answers here:
How to delete duplicate rows in SQL Server?
(26 answers)
Closed 6 years ago.
I have a table with 82,535 rows, where 65,087 rows are unique by ID. When I pull the entire result set of 82,535 and copy to Excel and remove duplicates, it shows that there are 17,448 duplicates. But when I'm using the query below I'm getting different results:
SELECT
BLD_ID, COUNT(BLD_ID) AS [BLD_ID COUNT]
FROM
Project.BreakageAnalysisOutcome_SentToAIM
GROUP BY
BLD_ID
HAVING
COUNT(BLD_ID) > = 2
This query returns a value of 17,364
I know for sure that the number of unique BLD_ID is 65,087
Most likely reason fro that is duplicate record may have more than 2 occurrence.
find duplicate count
Select COUNT(BLD_ID)- COUNT( DISTINCT BLD_ID)
From Project.BreakageAnalysisOutcome_SentToAIM
Use CTE with a Row_Number Function instead of count with group by clause and filer by Row_Number > 1.
;WITH cte
AS
(
SELECT ID,
ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) AS Rn
FROM [Table1]
)
DELETE cte WHERE Rn > 1