SELECT
LEFT(DateName( Month, TRIPDATE),3 ) + ' ' + RIGHT(DateName( Year, TRIPDATE),2 ) AS [MonthYear],
SUM(isnull(cast(TOTALFREIGHT as float),0)) AS FreightAmount
FROM OFFLINETRANS
Group By DateName( Month, TRIPDATE ), DateName( Year, TRIPDATE )
ORDER BY max(TRIPDATE)
尝试转换您的日期/日期时间列,如下所示:
SELECT
LEFT( DATENAME( MONTH, CONVERT(datetime, TRIPDATE, 103))
,3 ) + ' ' + RIGHT(DATENAME(YEAR, convert(datetime, TRIPDATE, 103)),2 ) AS [MonthYear],
SUM(ISNULL(cast(TOTALFREIGHT as float),0)) AS FreightAmount
FROM OFFLINETRANS
Group By LEFT( DATENAME( MONTH, CONVERT(datetime, TRIPDATE, 103)) ,3 ) + ' ' + RIGHT(DATENAME(YEAR, convert(datetime, TRIPDATE, 103)),2 )
ORDER BY max(TRIPDATE)
编辑:不要编辑我的答案,如果你仍有问题,编辑自己的问题。如果新问题与原始帖子无关,请改为创建新问题。
SELECT [MonthYear],SUM(CAST(FreightAmount AS DECIMAL(18,2))) As FreightAmount
FROM (
SELECT LEFT(DateName(Month, TRIPDATE), 3) + ' ' + RIGHT(DateName(Year, TRIPDATE), 2) AS [MonthYear] ,SUM(TRIPCOST) AS FreightAmount ,max(TRIPDATE) AS UsedForOrder
FROM TRANSVIEW GROUP BY DateName(Month, TRIPDATE) ,DateName(Year, TRIPDATE)
UNION ALL
SELECT LEFT(DATENAME(MONTH, CONVERT(DATETIME, TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, convert(DATETIME, TRIPDATE, 103)), 2) AS [MonthYear] ,SUM(ISNULL(cast(TOTALFREIGHT AS FLOAT), 0)) AS FreightAmount ,max(CONVERT(DATETIME, TRIPDATE, 103)) AS UsedForOrder FROM OFFLINETRANS GROUP BY LEFT(DATENAME(MONTH, CONVERT(DATETIME, TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, convert(DATETIME, TRIPDATE, 103)), 2)
) AS A GROUP BY [MonthYear] ORDER BY max(UsedForOrder)