find the youngest student from date - sql

I have the below table:
name id DOB marks
rk 2 2006-02-03 00:00:00.000 30
mk 3 2006-07-07 00:00:00.000 30
pk 4 2006-04-09 00:00:00.000 30
sk 5 2006-05-03 00:00:00.000 30
fk 6 2006-08-09 00:00:00.000 30
nk 7 2007-08-06 00:00:00.000 30
How can I find the youngest student?

You can order your table by descending date of birth and then filter the first result only, which in SQL Server can be done with
select top 1 *
from yourTable
order by DOB desc

Looks like you just need the latest date of birth (assuming DOB is date of birth):
select max(dob) from yourtable
Then your query would be:
select name as youngestStudent, dob as dateOfBirth
from yourtable
where dob = (select max(dob) from yourtable)

It's simple. According to your given data "nk" is the youngest student, so you can use the following query :
select * from yourtable
where dob = (select max(dob) from yourtable)

Related

SQL: Group By cause with condition

I have an Access table looking like this:
ID Country Application Date
--------------------------------
12 France 12/01/2016
12 Germany 01/01/2017
13 Germany 01/02/2017
14 Spain 23/01/2017
14 Germany 01/02/2017
15
16 Greece 01/01/2017
I would like to get a single occurence of each ID with the most recent application date.
I tried this:
SELECT ID, Country, Max(Application Date)
FROM MyTable
GROUP BY ID
But Access refused this query and wanted me to add the country in the group by clause, which can't work then.
Moreover, I would like to be able to fetch the rows with no country and application date as well (like the row with ID=15).
The expected result would be:
ID Country Application Date
--------------------------------
12 Germany 01/01/2017
13 Germany 01/02/2017
14 Germany 01/02/2017
15
16 Greece 01/01/2017
I think this is what you may want
select t1.* from MyTable as t1 inner join
(
SELECT ID, Max(Application Date) as Application Date
FROM MyTable
GROUP BY ID
) as t2 on t1.Id=t2.ID and t1.Application Date=t2.Application Date
It works on your input data and returns correct output data. Try it, please )
SELECT d.id, MyTable.country, d.datemax
FROM (SELECT ID as id, Max(AppDate) as datemax
FROM MyTable
GROUP BY ID) as d
,MyTable
WHERE d.id = MyTable.id and datemax = MyTable.appdate
OR (datemax is null and country is null)

Getting a row with two group by constraints

I have a table
TIMESTAMP ID Name
5/30/2016 11:45 1 Ben
5/30/2016 11:45 2 Ben
5/30/2016 23:15 2 Ben
5/30/2016 7:30 1 Peter
5/30/2016 6:05 1 Peter
5/30/2016 14:40 2 May
5/30/2016 1:05 1 May
Now, I need to get the MIN timestamp for each distinct Name.
Then if there are more than one MIN entry, choose the one with the MAX ID.
So the result should be
TIMESTAMP ID Name
5/30/2016 11:45 2 Ben
5/30/2016 6:05 1 Peter
5/30/2016 1:05 1 May
I tried using the query below:
SELECT MIN(TIMESTAMP),NAME FROM TBLSAMPLE WHERE TIMESTAMP BETWEEN TO_DATE('5/30/2016', 'MM/DD/YYYY' ) AND TO_DATE('5/30/2016', 'MM/DD/YYYY' ) + 1
GROUP BY NAME
and I could get the minimum time. But once I add in MAX(ID) the result return an entry that does not match any of the rows.
Your help are really appreciated.
You can do this with row_number():
select t.*
from (select t.*,
row_number() over (partition by name order by timestamp asc, id desc) as seqnum
from tblsample t
) t
where seqnum = 1;
Your question doesn't specify a condition on the dates. But if you want to add a where clause, then add it to the subquery.

SQL Select the Columnid with a max column group by one column

I think this question is already answered but it didn't satisfy my question.
I'd like to select the id/s of the names group by the latest date value (MAX) in my table. Using a group by column Name and group by column Date, I must get the ID, Name, Date.
Here is my table
ID Name Date
---------------------------------------
1 Brent 2012-02-17
2 Ash 2012-08-02
3 Brent 2012-08-15
4 Harold 2012-09-30
5 Margaret 2012-10-10
6 Ash 2012-12-01
7 Harold 2013-02-14
8 Ash 2012-01-01
9 Brent 2013-05-11
Output must be:
ID Name Date
---------------------------------------
5 Margaret 2012-10-10
6 Ash 2012-12-01
7 Harold 2013-02-14
9 Brent 2013-05-11
I try this statement:
SELECT
[ID], [Name], MAX([Date]) as [Date]
FROM
[SampleTable]
GROUP BY
[Name]
But I get this error:
Column 'ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
you can use Window Function such as ROW_NUMBER()
SELECT a.ID, a.Name, a.Date
FROM
(
SELECT ID, Name, Date,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY DATE DESC) rn
FROM TableName
) a
WHERE a.rn = 1
if ID and Name is the same for every group, you can simply add Name in the GROUP BY clause.
GROUP BY ID, Name

How to find most recent date given a set a values that fulfill condition *

I've been trying to build an sql query that finds from (table) the most recent date for selected id's that fulfill the condition where 'type' is in hierarchy 'vegetables'. My goal is to be able to get the whole row once max(date) and hierarchy conditions are met for each id.
Example values
ID DATE PREFERENCE AGE
123 1/3/2013 carrot 14
123 1/3/2013 apple 12
123 1/2/2013 carrot 14
124 1/5/2013 carrot 13
124 1/3/2013 apple 13
124 1/2/2013 carrot 14
125 1/4/2013 carrot 13
125 1/3/2013 apple 14
125 1/2/2013 carrot 13
I tried the following
SELECT *
FROM table
WHERE date in
(SELECT max(date) FROM (table) WHERE id in (123,124,125))
and preference in
(SELECT preference FROM (hierarchy_table)
WHERE hierarchy = vegetables))
and id in (123,24,125)
but it doesn't give me the most recent date for each id that meets the hierarchy conditions. (ex. in this scenario I would only get id 124)
Thank you in advance!
SELECT max(date) FROM (table) WHERE id in (123,124,125)
is giving you the max date from all dates, you need to group them.
Try replacing with:
SELECT max(date) FROM (table) GROUP BY id
This way you will get the max date for each id
I figured this out. Please see the query below as an example:
SELECT * FROM (table) t
WHERE t.date in
(SELECT max(date) FROM table sub_t where t.ID = sub_t.ID and (date !> (currentdate))
and preference in
(SELECT preference FROM (hierarchy_table) WHERE hierarchy ='vegetables')
and ID in ('124')
Change:
max(date)
To:
-- if your date data is in mm/dd/yyyy
max( str_to_date( date, '%m/%d/%Y' ) )
OR
-- if your date data is in dd/mm/yyyy
max( str_to_date( date, '%d/%m/%Y' ) )

Find out the Old Date from a date column in sql

How the find the oldest values from the datetime column?
I have table with datetime column (UpdateDate), and i need to find out the oldest data based on the UpdateDate .
Id UpdateDate Desc
-----------------------------------------
1 2010-06-15 00:00:00.000 aaaaa
2 2009-03-22 00:00:00.000 bbbbb
3 2008-01-12 00:00:00.000 ccccc
4 2008-02-12 00:00:00.000 ddddd
5 2009-04-03 00:00:00.000 eeeee
6 2010-06-12 00:00:00.000 fffff
I have Find out the old year dates from the current date using
Select UpdateDate from Table1 where DATEDIFF(YEAR,UpdateDate,getdate()) > 0 Query. But I need to find out the 2008th data only (Since the 2008 is the oldest one here)
I dont know what is there in the Table I need find out the Oldest date values.. How is it Possible?
Select UpdateDate from Table1 where DATEDIFF(YEAR,PartDateCol,getdate()) IN
(Select MAX(DATEDIFF(YEAR,PartDateCol,GETDATE())) DiffYear from Table1)
This will return two record of 2008. If your records has four 2006 date than it return all 2006 data if difference is large.
One way of doing this is
Select UpdateDate from Table1 where YEAR(UpdateDate )=2008
But, you can find out the oldest dates by ordering the data as such
Select * from Table1 order by UpdateDate ASC
You can use top and order by.
select top(1) UpdateDate
from Table1
order by UpdateDate
Update:
If you want all rows for the first year present you can use this instead.
select *
from (
select *,
rank() over(order by year(UpdateDate)) as rn
from Table1
) as T
where T.rn = 1
If you want the data that within 2008 year try this:
Select UpdateDate From Table1
Where Year(UpdateDate) =
(
Select Year(UpdateDate)
from Table1 Order By UpdateDate ASC Limit 1
) ;