有两个表
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
该如何实现?
...全文
669打赏收藏
一个我感觉是超难的问题,100分,在线等,马上给分!
有两个表 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 该如何实现?
(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 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
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
(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 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