请教一个查询问题

Jofan 2008-03-10 04:22:54
有表 tabela
有两列数据,no1为部门,按前三位区分;no2列按前三位区分以001结尾的为上级,其它为下属,我相查出的结果多出一列,能知道非001结尾的上级是谁。
请前辈赐教。

no1 no2
100100 1001001
100100 1001002
100100 1001003
100100 1001004
100200 1002001
100200 1002002
...

希望查出结果为

no1 no2 no3
100100 1001001
100100 1001002 1001001
100100 1001003 1001001
100100 1001004 1001001
100200 1002001
100200 1002002 1002001
...
...全文
87 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2008-03-10
  • 打赏
  • 举报
回复
select *,no3=case when right(no2,3)='001' then '' 
else (select no2 from [Table] where no1=a.no1 and right(no2,3)='001') end
from [Table] a
深渊的水影 2008-03-10
  • 打赏
  • 举报
回复
select no1, no2, case right(no2,3) when '001' then '' else left(no2,len(no2)-1) + '1' end
from table

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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