Query to get last available data given month and year

hi,

I am in seach of a query where in I can provide month, year and client name and fetch last available comments from the table.

Client,Month,Year and Comments are columns in that table.

For Ex: If i pass client as A, month as 7 and year as 2015, I should get comments for client A, month July and year 2015 if available.

If data not available, it must go to June month and so on until it finds comments.

Also when month is Jan, if query is going back, year also should get changed.

July 21st, 2015 2:48am

SELECT TOP 1 * FROM tbl WHERE client =<var>

ORDER BY cast(rtrim(y*10000+m*100+1) as datetime)

If it does not help ,please post sample data + desired result

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:04am

Hi Uri,

Thanks for the query. But this returns first row of data it has not the last one.

For ex: If there are comments for month May and June, I need comments for June but this is returning for May.

July 21st, 2015 3:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics