I have a SQL view, that as far as I understand, attempts to get the latest invoice date and the year and month combination of that invoice date. These values are then used to count the quantity in that month.
My SQL knowledge is basic and I'm not sure why the person would do this part:
MAX(CONVERT(varchar(3), InvoiceDate, 101) + CONVERT(varchar(4),
YEAR(InvoiceDate))) AS YearMonth
The rest of the code, including the TOP(100) PERCENT part etc., I am fairly happy with.
I'm not sure if the part in question was a logic mistake or if it has some SQL or business reason to it as in some instances it is completely different from the Max Invoice Date.
The full SQL expression below:
SELECT TOP (100) PERCENT
MAX(InvoiceDate) AS MaxInvoiceDate,
StockCode, Warehouse,
MAX(CONVERT(varchar(3), InvoiceDate, 101) + CONVERT(varchar(4), YEAR(InvoiceDate))) AS YearMonth
FROM dbo.ArTrnDetail
GROUP BY StockCode, Warehouse
HAVING (Warehouse = 'CS') OR
(Warehouse = 'PS') OR
(Warehouse = 'DS') OR
(Warehouse = 'JS') OR
(Warehouse = 'JN')
ORDER BY MaxInvoiceDate DESC
And a potentially problematic output:
MaxInvoiceDate | StockCode | Warehouse | YearMonth
----------------------------------------------------------------
2013-08-21 00:00:00.000 | ACH045 | PS | 12/2012