排序问题求教,多字段排序
根据不同的字段进行排序
例 根据 f1 , f2 ,f3
如果 f1 = 1 则 order by f1,f2,f3
如果 f2 = 2 则 order by f1,f3,f2
如何解决。
我现在的做法是用临时表或者表变量
create table #table1
(
....
id int identity(1,1)
)
insert into #table1(...) values select * from t1 where f1 = 1 order by f2,f3
insert into #table1(...) values select * from t1 where f1 = 2 order by f3,f2
select * from #table1 order by f1,id
请问除此之外还有更好的方案么?上述方法速度必然是很慢的。
by the way
我反复测试用临时表反而比表变量要快一点点。有点不得其解,测试是在查询分析器中进行的。