在数据透视表中按升序对月份进行排序

发布于 2025-01-16 20:10:57 字数 2140 浏览 2 评论 0原文

下面是我创建并在其中插入值的表:

CREATE TABLE Purchases   
(PurchaseDate varchar(25),   
SoldAmount int)  
GO  
  
INSERT INTO Purchases VALUES ('2001-01-28', 325),  
              ('2001-02-28', 245),  
              ('2001-02-28', 587),  
              ('2001-03-28', 312),  
              ('2001-04-28', 325),  
              ('2001-05-28', 675),  
              ('2001-05-28', 228),  
              ('2001-06-28', 109),  
              ('2001-07-28', 905),  
              ('2001-07-28', 207),  
              ('2001-08-28', 503),  
              ('2001-08-28', 102),  
              ('2001-09-28', 504),  
              ('2001-09-28', 103),  
              ('2001-09-28', 542),  
              ('2001-10-28', 915),  
              ('2001-10-28', 755),  
              ('2001-11-28', 385),  
              ('2001-12-28', 285),  
              ('2002-01-28', 492),  
              ('2002-02-28', 286),  
              ('2002-02-28', 664),  
              ('2002-03-28', 883),  
              ('2002-04-28', 673),  
              ('2002-05-28', 200),  
              ('2002-05-28', 421),  
              ('2002-06-28', 642),  
              ('2002-07-28', 325),  
              ('2002-07-28', 789),  
              ('2002-08-28', 432),  
              ('2002-08-28', 432),  
              ('2002-09-28', 886),  
              ('2002-09-28', 310),  
              ('2002-09-28', 970),  
              ('2002-10-28', 297),  
              ('2002-10-28', 301),  
              ('2002-11-28', 570),  
              ('2002-12-28', 921)  
GO

现在的问题是: 按月显示每年的销售总额 (SoldAmount)

这就是我所做的:

SELECT [Month],[2001],[2002]
FROM
(
 SELECT DATENAME(M,PurchaseDate) AS [Month],YEAR(PurchaseDate) AS [Year],SoldAmount
 FROM Purchases
) AS DataSource
PIVOT
(
 SUM(SoldAmount)
 FOR [Year] IN ([2001],[2002])
) AS Pivoting
ORDER BY [Month]

我得到以下结果:

在此处输入图像描述

虽然我得到的结果是正确的,但是唯一的问题是这个月尽管添加了 ORDER BY [Month],但列并未按升序排序。

如何在数据透视表中按升序对月份进行排序?

Below is the table I have created and inserted values in it:

CREATE TABLE Purchases   
(PurchaseDate varchar(25),   
SoldAmount int)  
GO  
  
INSERT INTO Purchases VALUES ('2001-01-28', 325),  
              ('2001-02-28', 245),  
              ('2001-02-28', 587),  
              ('2001-03-28', 312),  
              ('2001-04-28', 325),  
              ('2001-05-28', 675),  
              ('2001-05-28', 228),  
              ('2001-06-28', 109),  
              ('2001-07-28', 905),  
              ('2001-07-28', 207),  
              ('2001-08-28', 503),  
              ('2001-08-28', 102),  
              ('2001-09-28', 504),  
              ('2001-09-28', 103),  
              ('2001-09-28', 542),  
              ('2001-10-28', 915),  
              ('2001-10-28', 755),  
              ('2001-11-28', 385),  
              ('2001-12-28', 285),  
              ('2002-01-28', 492),  
              ('2002-02-28', 286),  
              ('2002-02-28', 664),  
              ('2002-03-28', 883),  
              ('2002-04-28', 673),  
              ('2002-05-28', 200),  
              ('2002-05-28', 421),  
              ('2002-06-28', 642),  
              ('2002-07-28', 325),  
              ('2002-07-28', 789),  
              ('2002-08-28', 432),  
              ('2002-08-28', 432),  
              ('2002-09-28', 886),  
              ('2002-09-28', 310),  
              ('2002-09-28', 970),  
              ('2002-10-28', 297),  
              ('2002-10-28', 301),  
              ('2002-11-28', 570),  
              ('2002-12-28', 921)  
GO

Now the question is:
Display the total sum of sales (SoldAmount) for each year by month

This is what I have done:

SELECT [Month],[2001],[2002]
FROM
(
 SELECT DATENAME(M,PurchaseDate) AS [Month],YEAR(PurchaseDate) AS [Year],SoldAmount
 FROM Purchases
) AS DataSource
PIVOT
(
 SUM(SoldAmount)
 FOR [Year] IN ([2001],[2002])
) AS Pivoting
ORDER BY [Month]

I got the following result:

enter image description here

Although I got the result correct, but the only issue is that the Month column is not sorted in ascending order despite adding ORDER BY [Month].

How can months be sorted in ascending order in a Pivot Table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私野 2025-01-23 20:10:57

您可以将月份数字添加到查询的数据透视部分,然后使用它进行排序:

SELECT [Month], [2001], [2002]
FROM
(
    SELECT DATENAME(M, PurchaseDate) AS [Month],
           MONTH(PurchaseDate) AS mn,
           YEAR(PurchaseDate) AS [Year],
           SoldAmount
    FROM Purchases
) AS DataSource
PIVOT
(
    SUM(SoldAmount)
    FOR [Year] IN ([2001],[2002])
) AS Pivoting
ORDER BY mn;

You could add the month number to the pivot portion of the query, and then order using it:

SELECT [Month], [2001], [2002]
FROM
(
    SELECT DATENAME(M, PurchaseDate) AS [Month],
           MONTH(PurchaseDate) AS mn,
           YEAR(PurchaseDate) AS [Year],
           SoldAmount
    FROM Purchases
) AS DataSource
PIVOT
(
    SUM(SoldAmount)
    FOR [Year] IN ([2001],[2002])
) AS Pivoting
ORDER BY mn;
慢慢从新开始 2025-01-23 20:10:57
SELECT [Month],[2001],[2002] FROM ( SELECT DATENAME(M,PurchaseDate) AS [Month],YEAR(PurchaseDate) AS Year],SoldAmount FROM Purchases) AS ataSource PIVOT (SUM(SoldAmount) FOR [Year] IN ([2001],[2002])) AS Pivoting ORDER BY MONTH('01-'+Month+'-2000')

SELECT [Month],[2001],[2002] FROM ( SELECT DATENAME(M,PurchaseDate) AS [Month],YEAR(PurchaseDate) AS Year],SoldAmount FROM Purchases) AS ataSource PIVOT (SUM(SoldAmount) FOR [Year] IN ([2001],[2002])) AS Pivoting ORDER BY MONTH('01-'+Month+'-2000')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文