--> 测试数据: T1
create table t1 (id int,scode varchar(9),sqty int)
insert into t1
select 1,'s1' ,1 union all
select 2,'s2' ,2
--> 测试数据: T2
create table t2 (id int,pcode varchar(9),pqty int,yid int)
insert into t2
select 12,'p2',3,2 union all
select 13,'p3',4,2
--> 测试数据: T3
create table t3 (rcode varchar(9),rqty int,yid int)
insert into t3
select 'r4',34,13 union all
select 'r5',35,13 union all
select 'r6',36,13select * from t1
left join t2
on t2.yid=t1.id
left join t3
on t3.yid=t2.id1 s1 1 NULL NULL NULL NULL NULL NULL NULL
2 s2 2 12 p2 3 2 NULL NULL NULL
2 s2 2 13 p3 4 2 r4 34 13
2 s2 2 13 p3 4 2 r5 35 13
2 s2 2 13 p3 4 2 r6 36 13
create table t1 (id int,scode varchar(9),sqty int)
insert into t1
select 1,'s1' ,1 union all
select 2,'s2' ,2
--> 测试数据: T2
create table t2 (id int,pcode varchar(9),pqty int,yid int)
insert into t2
select 12,'p2',3,2 union all
select 13,'p3',4,2
--> 测试数据: T3
create table t3 (rcode varchar(9),rqty int,yid int)
insert into t3
select 'r4',34,13 union all
select 'r5',35,13 union all
select 'r6',36,13select * from t1
left join t2
on t2.yid=t1.id
left join t3
on t3.yid=t2.id1 s1 1 NULL NULL NULL NULL NULL NULL NULL
2 s2 2 12 p2 3 2 NULL NULL NULL
2 s2 2 13 p3 4 2 r4 34 13
2 s2 2 13 p3 4 2 r5 35 13
2 s2 2 13 p3 4 2 r6 36 13
解决方案 »
- 如何时时监控表是否有记录变动
- 菜鸟问题
- 数据库一行内容复制问题?如何复制?
- 怎样比较两个数据库结构并更新?
- 请大家帮帮我,急!
- 请教如何同时对表中数据进行分组和排序
- 把XLS数据弄到SQL的存储过程有点问题,求解
- 请教:如何比较 sqlserver 7 中两个数据库数据是否相同?(在线等待)
- SQL Server7的问题,各位快来帮忙!!!!!!!!!!
- Oracle 7.3安装问题
- SQL SERVER 2005复制技术-查看快照代理状态提示“已以用户 db-server\repl_Snapshot 的身份执行。 客户端没有所需的特权。.”
- 如何将sql server 2000的数据库数据转换为sql server 2008?
1 s1 1 NULL NULL NULL NULL NULL NULL NULL
2 s2 2 12 p2 3 2 NULL NULL NULL
NULL NULL NULL 13 p3 4 2 r4 34 13
NULL NULL NULL NULL NULL NULL NULL r5 35 13
NULL NULL NULL NULL NULL NULL NULL r6 36 13
2 s2 2 12 p2 3 2 NULL NULL NULL
2 s2 2 13 p3 4 2 r4 34 13
2 s2 2 13 p3 4 2 r5 35 13
2 s2 2 13 p3 4 2 r6 36 13这是
select * from t1
left join t2
on t2.yid=t1.id
left join t3
on t3.yid=t2.id
的执行结果,但是我想要
下面的结果
1 s1 1 NULL NULL NULL NULL NULL NULL NULL
2 s2 2 12 p2 3 2 NULL NULL NULL
NULL NULL NULL 13 p3 4 2 r4 34 13
NULL NULL NULL NULL NULL NULL NULL r5 35 13
NULL NULL NULL NULL NULL NULL NULL r6 36 13
也就是把left join的重复数据用null替换
from t1 a
left outer join t2 b on a.id=b.yid
left outer join t3 c on b.id=c.yidLZ可以查下 inner join ,left join ,left outer join,right outer join 的具体用法。
把我写的贴到查询分析器里,看下就知道了。
我想要下面的结果
1 s1 1 NULL NULL NULL NULL NULL NULL NULL
2 s2 2 12 p2 3 2 NULL NULL NULL
NULL NULL NULL 13 p3 4 2 r4 34 13
NULL NULL NULL NULL NULL NULL NULL r5 35 13
NULL NULL NULL NULL NULL NULL NULL r6 36 13
select * from (select t1id=(select count(1) from t1 a where a.scode=aa.scode and a.id<=aa.id),* from t1 aa) a1full join (select t2id=(select count(1) from t2 b where b.yid=bb.yid and b.pid<=bb.pid), * from t2 bb) b1
on b1.yid=a1.id and b1.t2id=a1.t1id
t2表的字段名id 我改成pid了,
3表的我用这种方式也能写出来,嵌套的方式(又臭又长),排序又排不明白了。
正式数据要7,8张表的,给个思路也好。
,scode=case when (ROW_NUMBER() over(partition by a.id order by b.yid))>1 then NULL else a.scode end
,sqty=case when (ROW_NUMBER() over(partition by a.id order by b.yid))>1 then NULL else a.sqty end
,t2id=case when (ROW_NUMBER() over(partition by b.id order by b.yid))>1 then NULL else b.id end
,Pcode=case when(ROW_NUMBER() over(partition by b.id order by b.yid))>1 then NULL else b.pcode end
,Pqty=case when (ROW_NUMBER() over(partition by b.id order by b.yid))>1 then NULL else b.pqty end
,t2yid=case when (ROW_NUMBER() over(partition by b.id order by b.yid))>1 then NULL else b.yid end
,c.*
from t1 a
left join t2 b on a.id=b.yid
left join t3 c on b.id=c.yid