日期:2014-05-17  浏览次数:20638 次

多表合并查询求助
有三个表t1,t2,t3,用no字段关联



t1的no是唯一的,t2和t3的no记录数不固定,sql语句要怎么写才能得到t4的结果,要求t2,t3表的记录不能重复显示

PS:20积分是我现有全部积分了

------解决方案--------------------
select id,no,case when id2 <>1 then  null else  name end as name,
case when id2 <>1 then  null else  model end as model,
case when id2 <>1 then  null else  [desc] end as [desc],
case when id1 <>1 then null else parts end as parts,
case when id1 <>1 then null else qty end as qty,
case when id1 <>1 then  null else pamount end as pamount
from(
select row_number() over(partition  by name,model,[desc],no order by id)id2,row_number() over(partition  by parts,qty,pamount,no order by id)id1,* from
(select row_number() over ( order by t1.no  ) as id, t1.no,t1.name,t1.model,t2.[desc],t2.amount,t3.parts,t3.qty,t3.pamount from t1,t2,t3
where t1.no = t2.no  and t1.no = t3.no
)tb)a