按月份名称对查询输出进行排序

Sorting query output by Month name

我正在尝试使用 SQL 查询从 Excel 中提取值,但我一直在努力解决的问题是按各自的顺序对月份进行排序。现在 table 正在按 A-Z 排序,我尝试使用 DATEPART,但不是很成功,因为我遇到了 Int16 错误。

Select F1,
SUM(F2),
ROUND(SUM(REPLACE(F3, ',', '.')), 2),
ROUND(SUM(REPLACE(F4, ',', '.')), 2)
FROM [Sheet1$]
WHERE F1 IN ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December')
GROUP BY F1;

这是有效的查询,但结果是:

我也愿意接受任何可以提高我的查询速度的建议,因为文档很大,大约有 50k 行,谢谢。

对于任何想知道最终有效查询的人:

Select F1,
SUM(F2),
ROUND(SUM(REPLACE(F3, ',', '.')), 2),
ROUND(SUM(REPLACE(F4, ',', '.')), 2)
FROM [Sheet1$]
WHERE F1 IN ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December')
GROUP BY F1
ORDER BY SWITCH(
F1='January', 1,
F1='February', 2,
F1='March', 3,
F1='April', 4,
F1='May', 5,
F1='June', 6,
F1='July', 7,
F1='August', 8,
F1='September', 9,
F1='October', 10,
F1='November', 11,
F1='December', 12
);

你可以这样做:

order by datepart(mm, f1 + ' 1 2000')

要么使用 CASE 表达式:

Select
  f1,
  SUM(f2),
  ROUND(SUM(REPLACE(f3, ',', '.')), 2),
  ROUND(SUM(REPLACE(f4, ',', '.')), 2)
FROM [Sheet1$]
WHERE F1 IN ('January', 'February', 'March', 'April', 'May', 'June',
             'July', 'August', 'September', 'October', 'November', 'December')
GROUP BY F1
ORDER BY
  CASE F1
    WHEN 'January' THEN 1
    WHEN 'February' THEN 2
    WHEN 'March' THEN 3
    WHEN 'April' THEN 4
    WHEN 'May' THEN 5
    WHEN 'June' THEN 6
    WHEN 'July' THEN 7
    WHEN 'August' THEN 8
    WHEN 'September' THEN 9
    WHEN 'October' THEN 10
    WHEN 'November' THEN 111
    WHEN 'December' THEN 12
  END;

或者即时创建一个月table:

Select
  s.f1,
  SUM(s.f2),
  ROUND(SUM(REPLACE(s.f3, ',', '.')), 2),
  ROUND(SUM(REPLACE(s.f4, ',', '.')), 2)
FROM [Sheet1$] s
JOIN VALUES
(
  ('January', 1),
  ('February', 2),
  ('March', 3),
  ('April', 4),
  ('May', 5),
  ('June', 6),
  ('July', 7),
  ('August', 8),
  ('September', 9),
  ('October', 10),
  ('November', 11),
  ('December', 12)
) months(month_name, month_number) ON s.f1 = months.month_name
GROUP BY s.f1
ORDER BY months.month_number;

或者创建一个真实的月份table。