一个我感觉是超难的问题,100分,在线等,马上给分!

WNASP 2003-05-07 04:12:50
有两个表
1、主键:id,其他字段无关紧要比如xx和yy
id xx yy
001 er ee
2、主键:id 和“分段”(FenDuan)
id FenDuan
001 100-90
001 80-89
001 70-79
我想要实现这样一个表
id FenDuan1 FenDuan2 FenDuan3 xx yy
001 100-90 80-89 70-79 er ee

该如何实现?
...全文
66 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ldcr520 2003-05-07
  • 打赏
  • 举报
回复
from table1 as A,就是给你的表取了个别名A,有没有看到前面用的都是A.*,而并没有你的表名,where id = A.id ,也就不用说了,别忘了T-TRANSANCT语言是先执行FROM后面的部分.而不是SELECT部分.
WNASP 2003-05-07
  • 打赏
  • 举报
回复
谢谢楼上的,但是where id = A.id 和最后一句from table1 as A
我有点不明白,因为我以前就没有见过这样的
谢谢!
erigido 2003-05-07
  • 打赏
  • 举报
回复
(select top 1 FenDuan from table2 where id = A.id and FenDuan not exists
(select top 1 FenDuan from table2 where id = A.id )) as FenDuan2
就是说取除去第一个被选行之后的第一行!
psxfghost 2003-05-07
  • 打赏
  • 举报
回复
select id,(select max(FenDuan) from table2 where id = A.id) as FenDuan1,(select min(FenDuan) from table2 where id = A.id) as FenDuan2,select FenDuan from table where FenDuan not in(select max(FenDuan) from table2 where id = A.id,select min(FenDuan) from table2 where id = A.id) as FenDuan3
xx,yy from table1 A
:P
psxfghost 2003-05-07
  • 打赏
  • 举报
回复
tj_dns(愉快的登山者) 兄:
多谢你的程序,太巧了!^_^
学习!
happydreamer 2003-05-07
  • 打赏
  • 举报
回复
select b.id,
max(case FenDuan when '100-90' then FenDuan else null end) FenDuan1,
max(case FenDuan when '80-90' then FenDuan else null end) FenDuan2,
max(case FenDuan when '70-79' then FenDuan else null end) FenDuan3,
b.xx,b.yy,
from table2 a join table1 b on a.id=b.id
group by b.id,b.xx,b.yy
WNASP 2003-05-07
  • 打赏
  • 举报
回复
(select top 1 FenDuan from table2 where id = A.id and FenDuan not exists
(select top 1 FenDuan from table2 where id = A.id )) as FenDuan2
这句话实在是不明白,能解释一下吗?
keo 2003-05-07
  • 打赏
  • 举报
回复
不是很明白,你的fenduan1是指"100-90"还是指统计"100-90"的个数?如果是前者你想要建立的表就一点意义都没有了,如果是后者,用excel的数据透视图可以实现这个功能,但是要建立这样的表比较难,可以现建立这样的表,然后用存储过程统计更新各个分段的个数。
愉快的登山者 2003-05-07
  • 打赏
  • 举报
回复
select id,
(select top 1 FenDuan from table2 where id = A.id) as FenDuan1,
(select top 1 FenDuan from table2 where id = A.id and FenDuan not exists
(select top 1 FenDuan from table2 where id = A.id )) as FenDuan2,
(select top 1 FenDuan from table2 where id = A.id and FenDuan not exists
(select top 2 FenDuan from table2 where id = A.id )) as FenDuan3, xx, yy from table1 as A

34,590

社区成员

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

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