I've done the query but it looks ugly. And the main concern, i worry the performance would be slow.
This is my table:
ID ProductCode Qty PYear PMonth
1 A1 4 2015 2
2 A2 5 2015 2
3 A3 2 2014 12
4 A3 16 2015 4
I want to get the latest sell date categories by each product, below is the result that i want:
ProductCode Qty Year Month
A1 4 2015 2
A2 5 2015 2
A3 16 2015 4
looks simple, but my query is complicated, i bet there must be an easy solution for this, below is my query i'm working with, i joined the year and month into 1 column:
SELECT A.ProductCode, B.Qty, PDate
FROM (
SELECT MAX(ID) AS purchaseID,
ProductCode,
MAX(CAST(PYear AS VARCHAR(4)) + '-' + CAST(PMonthAS VARCHAR(2)) + '-1') AS PDate
FROM Table1
GROUP BY ProductCode
) AS B
LEFT JOIN Table1 AS B ON A.ID= B.ID