sql convert不识别as别名。小问题,大分,天气闷热,放分发泄!

秋的红果实 2017-07-11 11:19:44
我期望的效果
三环1段
三环2段
三环3段
三环4段
三环5段
三环6段
三环7段
三环8段
三环9段
三环10段
三环11段
三环12段
四环1段
四环2段
四环3段
四环4段
四环5段
四环6段
四环7段
四环8段
四环9段
四环10段
四环11段
四环12段
四环13段
四环14段
四环15段

先按照X环order by,然后再按X段order by

select ……,SUBSTRING(someName,3,1)+case when ISNUMERIC(SUBSTRING(someName,4,1))=1 then SUBSTRING(someName,4,1) else '' end as Col from table order by SUBSTRING(someName,1,2),convert(int,Col) ASC

convert死活不识别Col,有什么说道吗?(就说这,不讨论在C#里怎么处理)

这个Col有用,且为了阅读方便,好维护,不想把那一堆直接放到ASC前(那样倒是可以)

...全文
248 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
crystal_lz 2017-07-12
  • 打赏
  • 举报
回复
引用 10 楼 From_TaiWan 的回复:
[quote=引用 9 楼 crystal_lz 的回复:] 所有的分全部冲我来 放过其他人
那你说几句才行啊 深层原因在哪里?我查了下也不是执行顺序的原因,select早于orderby [/quote] 主要是数据库的一些复杂操作我很少用 用到也是临时百度过后也就忘了 一般也就链表查询一下 我的数据并不复杂 没有相关经验说错话就不好了
班门弄武 2017-07-11
  • 打赏
  • 举报
回复
select * from ( select column1 as COL from table1 ) t order by COL
秋的红果实 2017-07-11
  • 打赏
  • 举报
回复
引用 9 楼 crystal_lz 的回复:
所有的分全部冲我来 放过其他人
那你说几句才行啊 深层原因在哪里?我查了下也不是执行顺序的原因,select早于orderby
crystal_lz 2017-07-11
  • 打赏
  • 举报
回复
所有的分全部冲我来 放过其他人
班门弄武 2017-07-11
  • 打赏
  • 举报
回复
引用 5 楼 From_TaiWan 的回复:
为什么这里,convert就不识别Col了 order by SUBSTRING(someName,1,2),Col ASC,可以执行(但不是我要的效果) select A,B from …… order by SUBSTRING(someName,1,2),convert(int,A) ASC,也可以执行 唯独convert(int,Col)就不行 我就想啊,能用或不能用,总得有个依据,不能都作为注意点记住了吧,毕竟我们能用过的语句有限啊
问题点不在于你order by Convert , 如果你愿意,可以改成 order by Convert(int, ( SUBSTRING(someName,3,1)+case when ISNUMERIC(SUBSTRING(someName,4,1))=1 then SUBSTRING(someName,4,1) else '' end )) 按道理说,查询分析器已经把异常说明的很清楚了,所以看的出来你那里天气实在太热了。
IEEE_China 2017-07-11
  • 打赏
  • 举报
回复
当你有个99段以上的,col就位数不对啦
IEEE_China 2017-07-11
  • 打赏
  • 举报
回复
用你的COL,同一条查询语句中as列名,不能在orderby中直接用吧

;with cte_a(someName) as
(
select '三环1段' union all
select '三环2段' union all
select '三环5段' union all
select '三环6段' union all
select '四环11段' union all
select '四环12段' union all
select '四环13段' union all
select '三环7段' union all
select '三环8段' union all
select '三环3段' union all
select '三环4段' union all
select '三环9段' union all
select '三环10段' union all
select '四环5段' union all
select '四环6段' union all
select '四环7段' union all
select '四环8段' union all
select '三环11段' union all
select '三环12段' union all
select '四环1段' union all
select '四环2段' union all
select '四环3段' union all
select '四环4段' union all
select '四环9段' union all
select '五环30段' union all
select '五环100段' union all
select '五环9段' union all
select '四环10段' union all
select '四环14段' union all
select '四环15段'  
)
,cte_b as(
select someName, SUBSTRING(someName,3,1)+case when ISNUMERIC(SUBSTRING(someName,4,1))=1 then SUBSTRING(someName,4,1) else '' end as Col 
from cte_a)
select * from cte_b order by SUBSTRING(someName,1,2),convert(int,Col) ASC


--结果
someName  Col
--------- ----
三环1段      1
三环2段      2
三环3段      3
三环4段      4
三环5段      5
三环6段      6
三环7段      7
三环8段      8
三环9段      9
三环10段     10
三环11段     11
三环12段     12
四环1段      1
四环2段      2
四环3段      3
四环4段      4
四环5段      5
四环6段      6
四环7段      7
四环8段      8
四环9段      9
四环10段     10
四环11段     11
四环12段     12
四环13段     13
四环14段     14
四环15段     15
五环9段      9
五环100段    10
五环30段     30

(30 行受影响)

秋的红果实 2017-07-11
  • 打赏
  • 举报
回复
为什么这里,convert就不识别Col了 order by SUBSTRING(someName,1,2),Col ASC,可以执行(但不是我要的效果) select A,B from …… order by SUBSTRING(someName,1,2),convert(int,A) ASC,也可以执行 唯独convert(int,Col)就不行 我就想啊,能用或不能用,总得有个依据,不能都作为注意点记住了吧,毕竟我们能用过的语句有限啊
秋的红果实 2017-07-11
  • 打赏
  • 举报
回复
感谢几位 3#好专业的代码!
IEEE_China 2017-07-11
  • 打赏
  • 举报
回复


declare @x nvarchar(10)
declare @d nvarchar(10)
set @x='环'
set @d='段'
;with cte_a(someName) as
(
select '三环1段' union all
select '三环2段' union all
select '三环5段' union all
select '三环6段' union all
select '四环11段' union all
select '四环12段' union all
select '四环13段' union all
select '三环7段' union all
select '三环8段' union all
select '三环3段' union all
select '三环4段' union all
select '三环9段' union all
select '三环10段' union all
select '四环5段' union all
select '四环6段' union all
select '四环7段' union all
select '四环8段' union all
select '三环11段' union all
select '三环12段' union all
select '四环1段' union all
select '四环2段' union all
select '四环3段' union all
select '四环4段' union all
select '四环9段' union all
select '五环30段' union all
select '五环100段' union all
select '五环9段' union all
select '四环10段' union all
select '四环14段' union all
select '四环15段'  
)
,
cte_b(huan,d,duan) as(
select left(someName,charindex(@x,someName)) ,cast(substring(someName,charindex(@x,someName)+1,LEN(someName)-charindex(@x,someName)-1) as int),@d from cte_a
)
select huan+cast(d as nvarchar(10))+duan as someName from cte_b order by huan,d 

--结果
someName
-----------------------------
三环1段
三环2段
三环3段
三环4段
三环5段
三环6段
三环7段
三环8段
三环9段
三环10段
三环11段
三环12段
四环1段
四环2段
四环3段
四环4段
四环5段
四环6段
四环7段
四环8段
四环9段
四环10段
四环11段
四环12段
四环13段
四环14段
四环15段
五环9段
五环30段
五环100段

(30 行受影响)


wang_peng_yl 2017-07-11
  • 打赏
  • 举报
回复
首先,你convert(int 不对,会出错,因为你最后的col值按你的逻辑有可能是'',空字符怎么转int 所以 select * from ( select ……,SUBSTRING(someName,3,1)+case when ISNUMERIC(SUBSTRING(someName,4,1))=1 then SUBSTRING(someName,4,1) else '' end as Col from table order by SUBSTRING(someName,1,2) ) A order by Col ASC

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧