Retrieve next to last date SQL Server - sql

I think this is a simple problem but I cannot find an easy answer. I need to retrieve the last 2 entries by date. I have used max() to get the latest date; but do not know how to retrieve the next most recent.
The stored procedure code for latest date is:
SELECT *
FROM Table
WHERE Date=(SELECT MAX(Date) FROM Table);
So using a separate procedure how do I get the next most recent?

You can use order by and top:
select top 2 t.*
from t
order by date desc;

Or just to get the next most recent only as you stated...thus returning only one row...
select top 1 t.*
from t
where t.date != (select max(date) from table)
order by date desc;
or...
with cte as(
select
t.*
,row_number() over (order by t.date desc) as RN
from table t)
select *
from cte
where RN = 2

Related

Get latest record from a table based on 2 columns in hive

I want to get the latest record from my source table based on num and id columns and insert in my target table.
Scenario is explained in the attached screen shot. For latest record date column can be used.
Screenshot
Thanks.
Select num,id, date
FROM
(
Select *, ROW_NUMBER() OVER(partition by num,id Order by date desc) as rnk
FROM source_table
)a
WHERE rnk = 1;
by using corelated Subquery
select * from your_table t
where t.date= (
select max(date) from your_table t1
where t1.num=t.num and t1.id=t.id
)
You can do it using max() function
select num,id,max(date) from your_table t
group by num,id
SELECT NUM,ID,DATE FROM TABLE_TEMP
QUALIFY RANK OVER(PARTITION BY NUM,ID ORDER BY DATE DESC)=1;
You can do this using single line query
SELECT NUM,ID,DATE FROM TABLE_TEMP
QUALIFY RANK OVER(PARTITION BY NUM,ID ORDER BY DATE DESC)=1;

Multiple records not repeated

I have a table called TABLE_SCREW where I want to get the latest records for each code.
For example, in the table below you should obtain the records with ids 3 and 7.
I am a newbie in sql and I hope you can help me.
You could use:
SELECT TOP 1 WITH TIES *
FROM TABLE_SCREW
ORDER BY ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY Date DESC);
Another approach(may have better performance):
SELECT * -- here * should be replaced with actual column names
FROM (SELECT *,ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY Date DESC) AS rn
FROM TABLE_SCREW) sub
WHERE sub.rn = 1;

SQL SERVER QUERY to select max value record per item

This is the sample table
What I need to achieve is to get or display only the record of tenant with the highest month value. If ever month is equal, I need to base on the latest date value. Here is the sample desired output
With this, I started by this code using max function and incorporated temp table, but unable to get the desired result.
select tenant, name, date, month
into #sample
from tenant
select *
from #sample
where months = (select max(months)from #sample)
and output to something like this. As I believe, the code is getting the max value in the whole list not considering per tenant filtering.
Any help will be greatly appreciated :)
This can be done with the row_number window function:
select tenant, name, date, months
from (select t.*,
row_number() over (partition by t.tenant, t.name order by t.months desc, t.date desc) as rn
from TableName t) x
where rn = 1
You can use a row_number function.
Query
;with cte as
(
select rn = row_number() over
(
partition by tenant
order by months desc,[date] desc
),*
from table_name
)
select tenant,name,[date],months from cte
where rn = 1;

Whats the best way to find the "next but one newest" date in SQL?

I have a table in SQL Server that holds data organised by BusinessDataDate. BusinessDataDate is a Date only (no time component) and is only populated on Business days. e.g. not Weekends or public holidays.
Because there is no specific order to this, I want to find the date before the current max date
This query works, and I can set the values into local variables - but it feels like there must be a cleaner way?
SELECT MAX(BusinessDataDate) FROM myTable
WHERE BusinessDataDate < (SELECT MAX(BusinessDataDate) FROM myTable)
Each date will have multiple rows in the table - the exact number is not predictable.
SELECT TOP (1)
BusinessDataDate
FROM
(
SELECT
BusinessDataDate,
DENSE_RANK() OVER (ORDER BY BusinessDataDate DESC) as rn
FROM
myTable
) x
WHERE
x.rn = 2
ORDER BY
x.BusinessDataDate;
Similar to your original query but how about using top 1?
SELECT Top 1 BusinessDataDate
FROM myTable
WHERE BusinessDataDate < (SELECT MAX(BusinessDataDate) FROM myTable)
ORDER BY BusinessDataDate DESC

Get Dates on multiple calls

I have a log table that records dates.
I want to find out the date of the Second and Third call.
I get the first call by using MIN(DateCreated)
I get the last date using MAX(DateCreated). This could also be the second call.
What is the best way to find out the date of the second and third call?
I would just get all 3 rows together in one query
SELECT TOP 3 *
FROM LogTable
ORDER BY LogDate ASC -- DESC if you need the last 3
If you do need them one by one, you can use:
WITH LogByDate AS
(
SELECT LogDate,
ROW_NUMBER() OVER (ORDER BY LogDate) AS 'RowNumber'
FROM LogTable
)
SELECT *
FROM LogByDate
WHERE RowNumber = 2; -- RowNumber=3 for the third line
Try using analytical functions:
;WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY CallId ORDER BY DateCreated) Corr
FROM dbo.YourTable
)
SELECT *
FROM CTE
WHERE Corr IN (2,3)