select multiple columns with aggregations [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)
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

Related

about the array in condition [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)
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

Select ONLY second row if Date and Time are the same (MS SQL) [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
A date, time, number is produced every 1 hour and this will be Try number 1. However, there are times when there is a second Try will happen to collect the number for the same date and Time.
I would like to select Date, Time and Number for Try number 2 instead of number 1 if it exist.
Output needed
Code
SELECT Date, Time, Number, Try
FROM DB1 DB1
WHERE (Date>={ts '2021-01-26 00:00:00'})
ORDER BY Date, Time
Use row_number and filter:
select Date, Time, Number, Try
from
(
SELECT Date, Time, Number, Try,
row_number() over(partition by Date, Time order by Try desc) as rn
) s
where rn=1
It will selecft only the last try per date, time
You can use not exists, if you specifically want "2" (as the question states):
select t.*
from db1 t
where t.try = 2 or
(t.try = 1 and
not exists (select 1
from db1 t2
where t2.date = t.date and t2.time = t.time and
t2.try = 1
)
);
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RoNum
, ID --Add any fields needed here (or replace ID by *)
FROM TABLE_NAME
) AS tbl
WHERE (Date>={ts '2021-01-26 00:00:00'})
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY;
The OFFSET clause skips zero rows and the FETCH clause returns the first row.
Reference: https://www.sqltutorial.org/sql-fetch/

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
;

how to select only max(id) if you need less columns in group by [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)
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)

SQL Query - Finding rows for a highest index number [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 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