SQL - Select max date lines per column value - sql

I have a table with the following fields:
Risk|Date|Value
---------------
A|2019-04-23|3
A|2019-04-23|5
A|2019-06-12|4
A|2019-06-12|1
B|2019-05-22|7
B|2019-05-22|5
B|2019-03-13|4
C|2019-01-03|3
I would like to get all the lines that accomplish: its date value is the maximum along all the date values of that specific risk. The output would be:
Risk|Date|Value
---------------
A|2019-06-12|4
A|2019-06-12|1
B|2019-05-22|7
B|2019-05-22|5
C|2019-01-03|3
For the risk A, 2019-06-12 is the max date. Thus, all the lines with that date are sent to the output.
For the risk B, 2019-05-22 is the max date. Thus, all the lines with that date are sent to the output.
For the risk C, 2019-01-03 is the max date. Thus, all the lines with that date are sent to the output.
Any suggestion?
Thank you so much!!

use corelated subquery
select t1.* from tbale t1
where t1.date=(select max(t2.date) from table t2 where t1.risk=t2.riks)

A simple way filters in the where clause:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.risk = t.risk);

Use analytical/windowing function to achieve this. Syntax may vary from database to database, but in Hive, it looks like this:
SELECT
x.risk, x.date, x.value
FROM (
SELECT
risk, date, value,
DENSE_RANK() OVER(PARTITION BY risk ORDER BY date DESC) AS risk_rank
FROM
table_name
) x
WHERE x.risk_rank = 1;

select Risk, date, value
from
(select *, dense_rank() over(partition by risk order by date desc) as K
from Max_date
) as T
where K=1

Related

SQL Max or empty value grouped by conditions

I have a table like this
and i want my output to look like this
I need to look at the ID and then take max created date and max completed date for that ID. There is also some cases where completed date is still empty so in that case i just need to look at the max created date. Im not sure how to tackle this, doing a group by doesnt account for my multiple scenarios
Use ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY QUOTE_NUMBER
ORDER BY WORKBOOK_CREATED_DATE DESC) rn
FROM yourTable
)
SELECT *
FROM yourTable
WHERE rn = 1;

SQL - Summarize column with maximum date value and other fields

I have a table with the following fields:
Id|Date|Name
---------------
A|2019-04-24|"VALUE1"
A|2019-04-23|"VALUE2"
A|2019-06-11|"VALUE3"
A|2019-06-12|"VALUE4"
B|2019-05-21|"VALUE5"
B|2019-05-22|"VALUE6"
B|2019-03-13|"VALUE7"
C|2019-01-03|"VALUE8"
I would like to get one line per Id having the info of the maximum date line. This would be the output:
Id|Date|Name
---------------
A|2019-06-12|"VALUE4"
B|2019-05-22|"VALUE6"
C|2019-01-03|"VALUE8"
I have achieved through a group by getting the Id and the MAX Date, but not the value associated to that date.
What I am working on now is to inner join that table with the input one joining it on date and id, but I am not able to join on two fields.
Is there any way to bring to the result the value field related to the max date in the group by clause?
Otherwise, How could I join on two different fields those two tables?
Any Suggestion?
Thank you so much!!
You can use a correlated subquery :
select t.*
from table t
where t.date = (select max(t1.date) from table t1 where t1.id = t.id);
However, Most of DBMS supports analytical functions, so you can use :
select t.*
from (select t.*, row_number() over (partition by t.id order by t.date desc) as seq
from table t
) t
where seq = 1;

how to get latest date column records when result should be filtered with unique column name in sql?

I have table as below:
I want write a sql query to get output as below:
the query should select all the records from the table but, when multiple records have same Id column value then it should take only one record having latest Date.
E.g., Here Rudolf id 1211 is present three times in input---in output only one Rudolf record having date 06-12-2010 is selected. same thing with James.
I tried to write a query but it was not succssful. So, please help me to form a query string in sql.
Thanks in advance
You can partition your data over Date Desc and get the first row of each partition
SELECT A.Id, A.Name, A.Place, A.Date FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Date DESC) AS rn
FROM [Table]
) A WHERE A.rn = 1
you can use WITH TIES
select top 1 PERCENT WITH TIES * from t
order by (row_number() over(partition by id order by date desc))
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=280b7412b5c0c04c208f2914b44c7ce3
As i can see from your example, duplicate rows differ only in Date. If it's a case, then simple GROUP BY with MAX aggregate function will do the job for you.
SELECT Id, Name, Place, MAX(Date)
FROM [TABLE_NAME]
GROUP BY Id, Name, Place
Here is working example: http://sqlfiddle.com/#!18/7025e/2

Max of a Date field into another field in Postgresql

I have a postgresql table wherein I have few fields such as id and date. I need to find the max date for that id and show the same into a new field for all the ids. SQLFiddle site was not responding so I have an example in the excel. Here is the screenshot of the data and the output for the table.
You could use the windowing variant of max:
SELECT id, date, MAX(date) OVER (PARTITION BY id)
FROM mytable
Something like this might work:
WITH maxdts AS (
SELECT id, max(dt) maxdt FROM table GROUP BY id
)
SELECT id, date, maxdt FROM table t, maxdts m WHERE t.id = m.id;
Keep in mind without more information that this could be a horribly inefficient query, but it will get you what you need.

SQL: Records on same day but different time

Sample data:
2015-10-09 17:06:54
2015-01-05 11:04:12
2015-01-05 11:04:13
2015-01-09 14:52:19
Hi, I am trying to get a list of records of date + time with a condition: If same date, get the earliest record.
I know this can be easily done by substr() to only the date, but I do need the time too.
Please see the data above: I do not want the 3rd record down because it is a duplicate date but a later time.
How should I do this? Thanks so much for your help!
In MS SQL 2005+, you can try this:
SELECT *
FROM (
SELECT *,
RANK() OVER(PARTITION BY CAST(DateTime_Column AS DATE) ORDER BY DateTime_Column DESC) AS R
FROM Your_Table) AS Tb
WHERE R = 1
If you want to select 1 row for each day, you can use ROW_NUMBER() instead of RANK()
In Oracle, you can change CAST(DateTime_Column AS DATE) to TRUNC(DateTime_Column)
This will display row with later date_time for a specific date.
SELECT T1.*
FROM
table T1 INNER JOIN table T2
ON DATE(T1.date_time) = DATE(T2.date_time)
AND T1.date_time > T2.date_time GROUP BY DATE(T1.date_time)
Hope this helps..