我正在尝试使用SQL查询从Excel中提取值,但是我一直在努力的是按照月份的顺序对月份进行排序。现在,该表格正在按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;
这是有效的查询,但结果是:
我也乐意接受任何可以提高查询速度的建议,因为该文档非常大,大约有5万行,谢谢。
对于想知道最终查询是否有效的任何人:
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
);
答案 0 :(得分:1)
使用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;
或动态创建一个月表:
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;
或创建真实的月份表。
答案 1 :(得分:0)
您可以这样做:
order by datepart(mm, f1 + ' 1 2000')