在#temp 表中,我得到的值为
Month Sales
JUN 2015 600.00
JAN 2015 5000.00
MAR 2015 3500.00
MAY 2015 1000.00
FEB 2015 1500.00
APR 2015 1400.00
如何更新 #temp 表以按月排序并通过修改的更改更改表,我使用 order by DATEPART(mm,CAST( Substring( Month, 1, CharIndex( ' ', Month ) - 1)+ ' 1900' AS DATETIME)) asc
但在 #temp 上触发 select 时未反射(reflect)更改
预期输出:
Month Sales
JAN 2015 5000.00
FEB 2015 1500.00
MAR 2015 3500.00
APR 2015 1400.00
MAY 2015 1000.00
JUN 2015 600.00
所以下次如果我select * from #temp
,我应该得到预期的输出
最佳答案
试试这个:
只需执行CAST(MonthVal AS DATE)
DECLARE @Table TABLE
(MonthVal VARCHAR(10), Sales DECIMAL(8,2))
INSERT INTO @Table
VALUES
('JUN 2015', 600.00 ),
('JAN 2015', 5000.00),
('MAR 2015', 3500.00),
('MAY 2015', 1000.00),
('FEB 2015', 1500.00),
('APR 2015', 1400.00)
SELECT
*
FROM
@Table
ORDER BY
CAST(MonthVal AS DATE)
关于sql - 如何在Sql中为临时表设置order by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376739/