在 SQL 中对从 1 月到 12 月的月份进行排序

Posted

技术标签:

【中文标题】在 SQL 中对从 1 月到 12 月的月份进行排序【英文标题】:Sort Months from January to December in SQL 【发布时间】:2022-01-04 23:16:50 【问题描述】:

我想从一月到十二月对月份进行排序,但这是我的情况:

Result of my query

这是我对上图的查询

SELECT  SUM(totalAmount) AS Sales, 'January' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 1
UNION
SELECT  SUM(totalAmount) AS Sales, 'February' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 2
UNION
SELECT  SUM(totalAmount) AS Sales, 'March' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 3
UNION
SELECT  SUM(totalAmount) AS Sales, 'April' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 4
UNION
SELECT  SUM(totalAmount) AS Sales, 'May' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 5
UNION
SELECT  SUM(totalAmount) AS Sales, 'June' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 6
UNION
SELECT  SUM(totalAmount) AS Sales, 'July' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 7
UNION
SELECT  SUM(totalAmount) AS Sales, 'August' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 8
UNION
SELECT  SUM(totalAmount) AS Sales, 'September' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 9
UNION
SELECT  SUM(totalAmount) AS Sales, 'October' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 10
UNION
SELECT  SUM(totalAmount) AS Sales, 'November' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 11
UNION
SELECT  SUM(totalAmount) AS Sales, 'December' AS NameMonth
FROM  sales_tbl
WHERE MONTH(transactionDate) = 12 

我尝试将其包含在查询中以解决我的问题:

ORDER BY MONTH(NameMonth)

但是,我收到了一个错误Error Message

原来,NameMonth 不是表格中的一列。

非常感谢大家的回答。

【问题讨论】:

按 MONTH(transactionDate) @Roevic 而非 MONTH(NameMonth) 排序。 @KenWhite 道歉,我还是新手 【参考方案1】:

你可以这样做。

select DateName( month , DateAdd( month , datepart(Month, transactionDate) , -1 ) ) AS Sales, sum(totalAmount) As NameMonth from Mas_Transaction
group by datepart(Month, transactionDate)
order by datepart(Month, transactionDate)

【讨论】:

这对我有用!非常感谢!【参考方案2】:

首先,使用下面的方法,以每个月的一行显示销售量。之后,您可以使用 Case When 命令获取月份的名称并根据月份的 ID(MonthID 或 MONTH(transactionDate))进行排序。尝试关注:

select * from(
    SELECT  SUM(totalAmount) AS Sales , MONTH(transactionDate) AS MonthID
    FROM  sales_tbl
    GROUP BY MONTH(transactionDate)
) as tbl order by tbl.MonthID

【讨论】:

非常感谢您的回答!我会注意这一点。【参考方案3】:

以下查询使用 CTE 而不是使用子查询/联合,并按月和年排序以提高准确性。

说明: QUERY 是使用 CTE 对数据进行分组,并使用 DATENAME 内联函数获取 MonthName,然后执行正常的选择查询并对数据进行排序

WITH QUERY AS(
SELECT SUM(totalAmount) Sales,
MONTH(transactionDate) SalesMonth,
YEAR(transactionDate) SalesYear,
DATENAME(MONTH,transactionDate) NameMonth
FROM sales_tbl
GROUP BY MONTH(transactionDate),YEAR(transactionDate),DATENAME(MONTH,transactionDate) 
)
SELECT Sales,NameMonth,SalesYear
FROM QUERY
ORDER BY SalesYear,SalesMonth

参考资料:

DATENAME

CTE

【讨论】:

以上是关于在 SQL 中对从 1 月到 12 月的月份进行排序的主要内容,如果未能解决你的问题,请参考以下文章

sql如何按日期中的月份查询

在 Pivot SQL 表中排序月份

Oracle统计某一年中的1-12个月的数据总和

java怎么算出某一个月到某一个月的具体天数

如何根据月份增加 woocommerce 产品变化价格

SQL Server 中的条件联合,直到到达特定日期