[Solved] How to get last 3 month name in sql server


Sorry @Imrul Kaesh , there is some mistake on the query. I didn’t filter the last 3 months by ItemId.

I have updated my query as below, please have a try:

WITH Last3Month AS
(
    SELECT DISTINCT TOP 3 MONTH(IssueDate) AS Mth
    FROM Issue
    WHERE ItemId = 452      --Please add this WHERE Clause
    ORDER BY Mth DESC
)
SELECT CONVERT(CHAR, DATENAME(MONTH, IssueDate)) AS MonthName,
        ItemId,
        COUNT(CONVERT(varchar, IssueDate, 6)) AS WorkingDays
FROM dbo.Issue AS Issue
INNER JOIN Last3Month ON MONTH(Issue.IssueDate) = Last3Month.Mth
GROUP BY CONVERT(CHAR, DATENAME(MONTH, IssueDate)), ItemId, Last3Month.Mth
HAVING (ItemId = 452)
ORDER BY Last3Month.Mth

1

solved How to get last 3 month name in sql server