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)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
Closed last year.
Table1:
https://i.stack.imgur.com/XVyYf.png
Table2:
https://i.stack.imgur.com/762uM.png
expect the result:
https://i.stack.imgur.com/lNssy.png
if have many data in array[R80,R01,R02,R03,R04,R05...] and 'Table2',
so i want in array to find the 'Table2' data
the result must 'date' is new(DESC)/limit 1
example:
like select * from tabel2 where ID = 'R80' DESC Date Limit1
But i want to get all items [R80,R01,R02,R03,R04,R05...]
like select * from tabel2 where in [R80,R01,R02,R03,R04,R05...] DESC Date Limit1??
Please help~Thx
not use "loop,declare,#" is best
Please dont answer
select * from tabel2 where ID = 'R80'...;
select * from tabel2 where ID = 'R01'...;
select * from tabel2 where ID = 'R02'...;
...
Seems like you are looking for top 1 row for all IDs based on latest date. Use row number
SELECT * FROM (
--mark 1st row as 1 for every ID
SELECT *,row_number() over(partition by ID order by date desc) RN from table2
where id in ('R80','R01','R02','R03','R04','R05')
) INNER_QUERY
WHERE RN = 1
Related
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;
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
;
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)
Select latest row for each group from oracle
(3 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
Closed 2 years ago.
I have a table with some fields (time, text, type). I want to build a query to return for every type the text that was introduced with the maximum value of time. Oracle has some restrictions and it is not simple to build the query without some tricks.
I am struggling to get the right query, can anyone help me with it?
TIME TEXT TYPE
--------------------------
03.05.2020 AA 2
02.04.2020 BB 2
01.04.2020 CC 1
I want a query that returns
03.05.2020 AA 2
01.04.2020 CC 1
One option would be using DENSE_RANK, FIRST and LAST Analytic Functions as
MAX(time) KEEP (DENSE_RANK LAST ORDER BY time ) OVER( PARTITION BY type )
WITH t2 AS
(
SELECT t.*, MAX(time) KEEP (DENSE_RANK LAST ORDER BY time ) OVER( PARTITION BY type )
AS highest
FROM t
)
SELECT time, text, type
FROM t2
WHERE highest = time
Through this method all the ties(repeating values for time for each type group) are listed.
Demo
First is to get the max(TIME) per type, then join it to your tableA to get other fields (TEXT).
select * from tableA t
inner join
(select max(TIME) mt, type from tableA group by type) t1 on t1.mt=t.mt and t.type= t1.type
You can use row_number (or dense_rank):
select
t.*
from (
select
t.*,
row_number() over(partition by type_column order by time_column desc) rn
from tbl t
) t
where t.rn = 1
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)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
Closed 3 years ago.
The thing is that i could have values like
ID STREET_ID HOUSENUMBER POSTCODE
10000000 20512120 22 04114
11000000 20512120 22 04074
problem is that POSTCODE have to be in select, but i need distinct STREET_ID + HOUSENUMBER with MAX id,
by example i just want to show 11000000,20512120,22,04074 out of 2 records because of MAX(h.ID).
this is my code
SELECT DISTINCT
MAX(h.ID),
h.street_id,
h.houseNumber,
h.postindex AS postCode
FROM house h
WHERE
h.postindex IS NOT NULL AND
h.STREET_ID IS NOT NULL
GROUP BY
h.street_id,
h.houseNumber
ORDER BY
STREET_ID,
CAST(REGEXP_REPLACE(REGEXP_REPLACE(h.houseNumber, '(\-|\/)(.*)'), '\D+') AS NUMBER),
h.houseNumber
i have an error " ORA-00979: not a GROUP BY expression " and i understand it, because POSTCODE is not in GROUP BY, how to deal with that?
Your requirement is a good candidate for ROW_NUMBER:
WITH cte AS (
SELECT h.*,
ROW_NUMBER() OVER (PARTITION BY h.STREET_ID, h.HOUSENUMBER ORDER BY h.ID DESC) rn
FROM house h
)
SELECT ID, STREET_ID, HOUSENUMBER, POSTCODE
FROM cte
WHERE rn = 1;
An index on (STREET_ID, HOUSENUMBER, ID) might speed up the above query, because it would let Oracle quickly find the max ID record for each street/house number.
You can do it with subquery and exists:
SELECT *
FROM house h
WHERE NOT EXISTS (SELECT 1 FROM house h2
WHERE h2.street_id = h.street_id
AND h2.houseNumber = h.houseNumber
AND h2.id > h.id)
Don't aggregate. Instead, you can filter with a correlated subquery:
select h.*
from house h
where id = (
select max(h1.id)
from house h1
where h1.street_number = h.street_number and h1.house_number = h.house_number
)
I would expect this solution to be as efficient as it gets, especially with an index on (street_number, house_number, id).
select distinct id,street_id,house_no,postal_code from house where id in (
SELECT MAX(id) from house group by street_id,house_no)
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 SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Get top results for each group (in Oracle)
(5 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Closed 5 years ago.
I am struggling to find a correct way to query the following scenario. Consider the below table as example, where I want the row with index_no 124 for that dvpc and pid value of the columns. Please suggest.
Index_No dvpc pid rate
123 123 30 0.01
124 123 30 0.02
Use ROW_NUMBER to assign a ranking on Index_No for each value of dvpc and pid, then filter to the ones with the highest rank.
SELECT Index_No, dvpc, pid, rate
FROM (SELECT Index_No, dvpc, pid, rate,
ROW_NUMBER() OVER (PARTITION BY dvpc, pid ORDER BY Index_no DESC) rn
FROM your_table) yt
WHERE rn = 1;
You could use max() and GROUP BY.
For example:
select
dvpc,
pid,
max(index_no) as max_index
from
your_table
group by
dvpc,
pid
If you require the rate for that index, then something like this would work
select
t.*
from
your_table t
inner join
(
select
dvpc,
pid,
max(index_no) as max_index
from
your_table
group by
dvpc,
pid
) s
on t.index_no = s.max_index
Hope this helps