这是我的查询。它会像屏幕截图中描述的那样显示结果。现在我想将其更改为以列的形式显示本月的状态。
DECLARE @temp TABLE
(
MonthName VARCHAR(10),
[Year] VARCHAR(10),
StatusTypeId INT,
StatusTypeName VARCHAR(50),
StatusCount INT
)
INSERT INTO @temp
SELECT
CONVERT(varchar(3), DATENAME(month, w.ExpectedStartDate)) as MonthName,
datepart(yyyy, w.ExpectedStartDate) as [Year],
w.StatusTypeId,
st.StatusTypeName,
COUNT(ISNULL(w.StatusTypeId, 0)) AS StatusCount
FROM
Worksheet w LEFT OUTER JOIN
StatusType st ON st.StatusTypeId = w.StatusTypeId
WHERE w.ProjectId = 20
AND CONVERT(varchar(3), DATENAME(month, w.ExpectedStartDate)) between ('feb') AND ('mar')
GROUP BY
datepart(yyyy, w.ExpectedStartDate),
CONVERT(varchar(3), DATENAME(month, w.ExpectedStartDate)),
w.StatusTypeId,
st.StatusTypeName
SELECT ISNULL(((CONVERT(VARCHAR(5), [Year])) + '-' + MonthName), 'Unknown') AS MonthName,
ISNULL(StatusTypeName, 'Unknown') AS StatusTypeName,
StatusCount
FROM @temp
我认为这张图片可以很好地描述我需要的内容。
请告诉我如何按月份名称排序。例如,Jan、Feb、Mar、Jun、Dec等。
谢谢。