Find out the Old Date from a date column in sql - 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
) ;

Related

ORDER BY date but also GROUP BY userid

I have a table of records I want to sort by earliest date first then by userid.
If the user associated to the date also has other records in that table I want to group those under the earliest date.
Desired output
Id UserId Date
1 2 1/1/2020
2 2 2/1/2020
3 2 3/1/2020
4 1 1/2/2020
5 1 2/2/2020
6 3 1/4/2020
7 4 1/5/2020
In this example UserId 2 has the earliest record in that table, so that record should be first followed by his additional records in date asc order
You seems want :
select t.*
from table t
order by min(date) over (partition by userid), date;
Some database product doesn't support window function with order by, so you can do instead :
select t.*, min(date) over (partition by userid) as mndate
from table t
order by mndate, date;
If I understand what you want...
You could do this (sample with DB2 syntax):
SELECT tab.UserId, tab.Date, tab.*
FROM DB2SIS.TABLE_NAME tab
ORDER BY tab.Date ASC, tab.UserId ASC
This way UserId and Date will appear repeatedly. Instead of 'tab.*' use each field you want to show, then UserId and Date will not repeat.

Delete duplicate rows in Access SQL-Query

i want to create an Access SQL Query for deleting duplicate rows.
My Table:
CustID EventDate EventID
12 01.01.2019 1001
10 02.01.2019 1002
11 03.01.2019 1003
10 01.01.2019 1001
11 03.01.2019 1004
The table has no primary key.
I want to delete every duplicate CustID.
The result should have every CustID once with
Prio 1. the most recent EventDate
Prio 2. the biggest EventID
The result would look like this:
CustID EventDate EventID
12 01.01.2019 1001
10 02.01.2019 1002
11 03.01.2019 1004
I don't want to use macros.
How would the sql statement look like in access?
Thank you in advance.
Assuming most recent date and biggest event ID will always be in the same record, consider:
Query1:
SELECT Table1.CustID, Max([EventDate] & [eventID]) AS ID
FROM Table1
GROUP BY Table1.CustID;
Query2:
DELETE FROM Table1 WHERE Not CustID & EventDate & EventID IN (SELECT CustID & ID
FROM Query1);
You can apply the conditions for deletion with EXISTS:
DELETE FROM tablename AS t
WHERE EXISTS (
SELECT 1 FROM tablename
WHERE
CustID = t.CustID
AND
(EventDate > t.EventDate OR (EventDate = t.EventDate AND EventID > t.EventID))
)
This will help you :
Select distinct * into ‪#‎tmpl‬ From MY_TABLE
Delete from MY_TABLE
Insert into MY_TABLE
Select * from #tmpl
Drop table #tmpl
While creating temp tables if throw errors, then create another real table and perform the same.

find the youngest student from date

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)

SQL: Take maximum value, but if a field is missing for a particular ID, ignore all values

This is somewhat difficult to explain...(this is using SQL Assistant for Teradata, which I'm not overly familiar with).
ID creation_date completion_date Difference
123 5/9/2016 5/16/2016 7
123 5/14/2016 5/16/2016 2
456 4/26/2016 4/30/2016 4
456 (null) 4/30/2016 (null)
789 3/25/2016 3/31/2016 6
789 3/1/2016 3/31/2016 30
An ID may have more than one creation_date, but it will always have the same completion_date. If the creation_date is populated for all records for an ID, I want to return the record with the most recent creation_date. However, if ANY creation_date for a given ID is missing, I want to ignore all records associated with this ID.
Given the data above, I would want to return:
ID creation_date completion_date Difference
123 5/14/2016 5/16/2016 2
789 3/25/2016 3/31/2016 6
No records are returned for 456 because the second record has a missing creation_date. The record with the most recent creation_date is returned for 123 and 789.
Any help would be greatly appreciated. Thanks!
Depending on your database, here's one option using row_number to get the max date per group. You can then filter those results with not exists to check against null values:
select *
from (
select *,
row_number() over (partition by id order by creation_date desc) rn
from yourtable
) t
where rn = 1 and not exists (
select 1
from yourtable t2
where t2.creationdate is null and t.id = t2.id
)
row_number is a window function that is supported in many databases. mysql doesn't but you can achieve the same result using user-defined variables.
Here is a more generic version using conditional aggregation:
select t.*
from yourtable t
join (select id, max(creation_date) max_creation_date
from yourtable
group by id
having count(case when creation_date is null then 1 end) = 0
) t2 on t.id = t2.id and t.creation_date = t2.max_creation_date
SQL Fiddle Demo

How to get multiple rows based on max date

I have a table SalePrices in SQL server and data same as below:
SPID ProductID Price Date
001 Pro01 10 2016-03-10
002 Pro01 20 2016-03-11
003 Pro02 10 2016-03-13
004 Pro02 20 2016-03-15
What I want is create a view that show only one ProductID and Price that I have modified at the last time. So what I want is same as the result below:
ProductID Price Date
Pro01 20 2016-03-11
Pro02 20 2016-03-15
There're few different approaches for this, for example, using row_number():
;with cte as (
select
ProductID, Price, Date,
row_number() over(partition by ProductID order by Date desc) as rn
from <Table>
)
select
ProductID, Price, Date
from cte
where
rn = 1
sql fiddle demo
Another version with windowing functions, this one with FIRST_VALUE();
SELECT ProductID, price, date
FROM products
WHERE spid IN (
SELECT FIRST_VALUE(spid) OVER (PARTITION BY ProductID ORDER BY date DESC) spid
FROM products
)
An SQLfiddle to test with.
Note that Roman's version with ROW_NUMBER should work from SQL Server 2005 and newer, while this will only work for SQL Server 2012 and newer.
TRY THIS:
SELECT
ProductID
, Price
, Date FROM tablename AS A
JOIN (SELECT ProductID,MAX(Date) AS DATE FROM tablename
GROUP BY ProductID
) AS B ON A.Date=B.DATE AND A.ProductID=B.ProductID
one more approach...
select productid,price,date
from
table t1
where date=(select max(date) from table t2 where t1.productid=t2.productid)
Your last record will have the highest SPID:
select
ProductId, Price, Date
from
SalePrices sap
where
sap.spid =(
select
max(sap2.spid)
from
SalePrices sap2
where
sap2.productId = sap.productId)
This query will give u desired result:
ProductID Price Date
Pro01 20 2016-03-11
Pro02 20 2016-03-15