当前位置:编程学习 > C#/ASP.NET >>

高分求助 正解SQLSEVER 2005 sql排序

我的数据MS SQL 字段是varchar(50) 类型。存的GB MB KG 例如 3.3GB 523.6MB 823.KB这种属性, 二千多条, 怎样对它进行按大小排序,sql语句怎么写? --------------------编程问答--------------------
//try..
create table #temp(num varchar(50))
insert into #temp
select '3.3GB' union all
select '523.6MB' union all
select '823.5KB'


select * from #temp order by num desc

select * from #temp order by 
case when charindex('GB',num)>0 then convert(decimal,substring(num,1,charindex('GB',num)-1))*1024*1024 
     when charindex('MB',num)>0 then convert(decimal,substring(num,1,charindex('MB',num)-1))*1024 end desc

drop table #temp
/*
3.3GB
523.6MB
823.5KB
*/
--------------------编程问答-------------------- 师傅 你也的不对  我在数据库里查询 查出这样的结果咯 
3232GB
22GB
7.8GB
7.12GB
5.66GB
5.88GB
6.12GB
4.73GB
4.9GB
5.14GB
5.34GB
4.53GB
5.33GB
4.17GB
4.21GB
3.59GB
4GB
4.3GB
4.28GB
4.24GB
4.19GB
4.45GB
3.97GB
4.19GB
3.67GB
4.32GB
3232MB
3.43GB
2.95GB
3.48GB
2.69GB
3.42GB
3.36GB
3.04GB
1.67GB --------------------编程问答-------------------- Select * From tb Order by case when charindex('GB',filesize)>0 then 3 when charindex('MB',filesize)>0 then 2 when charindex('KB',filesize)>0 then 1 else 0 end DESC, Cast( substring(filesize,1,len(filesize)-2) as float) DESC --------------------编程问答-------------------- 我能不能弱弱地问一下,你添加的时候,就不能先处理一下,统一单位吗?

全用KB作单位,不得得了?

这样,性能要提高很大,因为添加很少,无论是次数还是内容。

但查询很多,无论是次数还是内容 --------------------编程问答-------------------- 全部换算成一种单位来排序就可以,

欢迎光临我的博客 Silent博客 --------------------编程问答--------------------
引用 5 楼 zlblog 的回复:
全部换算成一种单位来排序就可以,

欢迎光临我的博客 Silent博客

可以先全部换算为kb,再按数字排序 --------------------编程问答--------------------
引用 2 楼 zhangjianfeila 的回复:
师傅 你也的不对 我在数据库里查询 查出这样的结果咯 
3232GB
22GB
7.8GB
7.12GB
5.66GB
5.88GB
6.12GB
4.73GB
4.9GB
5.14GB
5.34GB
4.53GB
5.33GB
4.17GB
4.21GB
3.59GB
4GB
4.3GB
4.28GB
4.24GB
4.19GB
4.45GB
3.9……
你这样试试看,因为那时3.59被转换为4了。
select * from #temp order by 
case when charindex('GB',num)>0 then cast(substring(num,1,charindex('GB',num)-1) as decimal(20,3))*1024*1024 
     when charindex('MB',num)>0 then cast(substring(num,1,charindex('MB',num)-1) as decimal(20,3))*1024 
     when charindex('KB',num)>0 then cast(substring(num,1,charindex('KB',num)-1) as decimal(20,3)) end desc
--------------------编程问答--------------------
select * from tb
 order by left(字段,len(字段)-2)*
          (case right(字段) 
           when 'GB' then 1024*1024.0
           when 'MB' then 1024.0
           else 1.0 end) 
--------------------编程问答-------------------- 专题解答:http://www.cnblogs.com/insus/archive/2011/08/21/2147910.html
好好参考喔。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,