Share via

get latest ingested details

arkiboys 9,711 Reputation points
2022-06-28T10:55:51.337+00:00

in sql server, I am ingesting data into a table, tblMain
the table has columns as follows:
companyName,
book,
...,
year,
month,
day

each day, a snapshot of hundreds of rows are ingested.
so, for example, for yesterday, say, 20 companies were ingested
and today, say 30 companies were ingested.
the same company may be ingested each day...

Question:
How is it possible to have a sql query to get the latest companyName details.
for example, if company123 was ingested on year=2022, month=05, day=22 and day=28
I would like to get the details of company123 for the latest date which is for day=28
Thank you

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


Answer accepted by question author

Naomi Nosonovsky 8,906 Reputation points
2022-06-28T13:17:05.447+00:00

;with cte as (select *, ROW_NUMBER() OVER (partition by CompanyName ORDER BY [Year] , [Month] , [Day] DESC) as Rn from tblMain)

select * from cte where Rn = 1

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.