SQL server select max two dates from a table - sql

I would like to know how if it is possible to select the two most recent dates from a column in a table. Please see the simple example below. I know to get the max date I can use the max function. I'm also aware that I could then do another max statement with a where condition that states it must be less than the first date returned from my first max query. I was wondering though if there was a way of doing this in one query?
Name DateAdded
ABC 2014-04-20
ABC 2014-04-20
ABC 2014-03-01
ABC 2014-03-01
ABC 2014-02-25
ABC 2014-05-22
ABC 2014-04-01
The two dates that should be returned are the two most recent, i.e. 2014-05-22 & 2014-04-20.
EDIT
Sorry I should have mentioned yes I want two distnict dates. The table is large and the dates are not sorted. I think sorting the table could be quite slow.

SELECT distinct top 2 Dateadded
FROM table
ORDER BY Dateadded desc

select top(2) DateAdded
from table
order by DateAdded DESC

Try This :
select distinct top(2) format(Dateadded ,'yyyy-MM-dd') as Dateadded
from TableName
order by Dateadded DESC

Related

How to compare records in the same table using SQL to find difference?

I want to compare two dispense transactions and find out if same drug was dispensed twice within 5 day window. How can I write SQL to compare these transactions and report?
patient_id
doctor_id
rx_number
pharmacy_id
drug_id
ship_date
1234
1234
rx_1234
ph_1234
1234
2022-06-28
1234
1234
rx_1234
ph_1234
1234
2022-07-01
EXISTS clause can be used for this purpose:
This one returns the row from the table if there is another row for the same drug within 5 days of ship_date:
select *
from MyTbl T1
where exists
(select *
from MyTbl T2
where T2.drug_id=T1.drug_id
and abs(datediff(day,T1.ship_date, T2.ship_date))<=5
)
The syntax for the 'number of days between two dates' is for SQL Server and it can change from one RDBMS product to other. You didn't specify what you use.
This query returns both rows (the earlier and the later shipment); you didn't specify the desired output.
I assumed ship_date=dispensed date.

In SQL, how to select rows with matching values in one column, based on earliest date in another column

I think this should be a simple SQL exercise, but I am not sure how it is done as I am new to querying dbs with SQL. I have a table that looks like this:
select * from myschema.mytable
customer_name date
nick 2017-06-19 19:26:40
tom 2017-06-21 19:24:40
peter 2017-06-23 21:25:10
nick 2017-06-24 13:43:39
I'd like for this query to return only one row for each unique name. Specifically, I'd like the query to return the rows for each customer_name with the earliest date. In this case, the first row for nick should be returned (with date 2017-06-19), but not the other row with date 2017-06-24.
Is this a simple exercise in SQL?
Thanks!
A simple MIN will do:
SELECT
customer_name,
MIN(date) AS earliest_date
FROM myschema.mytable
GROUP BY customer_name;
For this kind of problems you can use aggregate functions. what has to be done basically is grouping the rows by name and choosing the minimum date:
select cutomer_name, MIN(date)
FROM myschema.mytable
GROUP BY customer_name

How to check if record is updated?

I'm trying to get updated records count from previous week. But I'm having trouble approaching the problem.
For Eg: I have a table 'Org'
week1 :
id name age address date
record1 : 123 Joe 35 xyz 12/01/2017
week2 :
id name age address date
record1 : 123 Joe 35 abc 12/03/2017
I'm trying to get the record which has been updated. In the above example, the address for record1 with id 123 has been updated. Currently I'm checking in an in-efficient way.
Query:
select * from Org where date='12/01/2017'
and
select * from Org where date='12/03/2017'
Query:
select distinct on (id) count(*) from Org group by Org.id
A file is being pushed to the db every day. So, the updated record will have a new timestamp and the records are getting aggregated overtime which made my job a little harder. I tried joining the table to itself but it didn't make any sense to me. I'm not sure how to approach this problem. I was trying and almost reaching the solution but I don't understand why I'm getting count two times. Example Fiddle
Try this Query:
select col1
from(
select distinct col1,col2,col3,col4
from table_not
)D
group by col1
having count(1)>1
SQL Fiddle link: SQL Fiddle

How can I select if Date column is as same as current year

This is my Student table
Id(int) | Name(varchar) | registerDate(Date)
1 John 2012-01-01
How can I write the appropriate query to check if the person's registerDate value is as same as current year (2012)?
SELECT *
FROM Student
WHERE YEAR(registerDate) = YEAR(getdate())
The most direct solution would be to use the YEAR or DATEPART function in whatever flavor of SQL you're using. This will probably meet your needs but keep in mind that this approach does not allow you to use an index if you're searching the table for matches. In this case, it would be more efficient to use the BETWEEN operator.
e.g.
SELECT id, name, registerDate
FROM Student
WHERE registerDate BETWEEN 2012-01-01 and 2012-12-31
How you would generate the first and last day of the current year will vary by SQL flavor.
Because you're using a range, and index can be utilized. If you were using a function to calculate the year for each row, it would need to be computed for each row in the table instead of seeking directly to the relevant rows.
If by chance your flavor of sql is Microsoft TSql then this works:
SELECT * FROM Student Where datepart(yy,registerDate) = datepart(yy,GetDate())
This should work for SQL Query:
SELECT * FROM myTable
WHERE registerDate=YEAR(CURDATE())

Ms-Access SQL Query

I need some guidance on how to write a query in Ms-Access which will pull all the records based on the PublishedDate but with-in the expiry date. For instance, my table carries 3 columns EventTitle, PublishDate, ExpiryDate with values Title1, 2/22/2012, 3/28/2012.
Now the 1st record will only appear on 22-Feb-2012 until 28-Mar-2012. The query which I used drops the record once the current date changes from 22-Feb to 23-Feb.
I tried using the below script
SELECT top 1 Title, ExpiryDate
FROM Table1
WHERE (((PublishDate)=Date()
Or (PublishDate)<=[ExpiryDate])
AND ((ExpiryDate)>=Date()))
ORDER BY ExpiryDate ASC
Sounds like you want this:
SELECT top 1 Title, ExpiryDate
FROM Table1
WHERE PublishDate<=Date()
AND ExpiryDate>=Date()
ORDER BY ExpiryDate ASC