h****t 发帖数: 22 | 1 一个递归关系表
ID InheritedFromID
1 0
2 1
3 2
一个object属性表
ID Prop1 prop2
1 a0 a1
1 a2 a3
2 b0 b1
2 b2 b3
3 c0 c1
一个object不具有的属性表
ID Prop1 Prop2
2 a0 a1
3 b0 b1
我需要列出所有object的属性,得到下面的结果
ID Prop1 Prop2
1 a0 a1
1 a2 a3
2 a2 a3
2 b0 b1
2 b2 b2
3 a2 a3
3 b2 b2
3 c0 c1
我现在是使用CTE列出了所有的属性组合,然后再去除掉不具有的属性。
请问网上高人,能否用单一的CTE就得到我要的结果?谢谢。 | h****t 发帖数: 22 | | B*****g 发帖数: 34098 | 3 share your solution please
【在 h****t 的大作中提到】 : 在CTE中使用except解决了,谢谢。
| h****t 发帖数: 22 | 4
首先声明,我是作应用程序开发的,在这个版上贴query是在班门弄斧,让大家见笑了。
CREATE TABLE table1(id int,InheritedFromID int)
CREATE TABLE table2(id int,prop1 nvarchar(50),prop2 nvarchar(50))
CREATE TABLE table3(id int,prop1 nvarchar(50),prop2 nvarchar(50))
insert into table1 (id,InheritedFromID) values(1,0)
insert into table1 (id,InheritedFromID) values(2,1)
insert into table1 (id,InheritedFromID) values(3,2)
insert into table2(id,prop1,prop2) values(1,'a0','a1')
insert into table2(id,prop1,prop2) values(1,'a2','a3')
insert into table2(id,prop1,prop2) values(2,'b0','b1')
insert into table2(id,prop1,prop2) values(2,'b2','b3')
insert into table2(id,prop1,prop2) values(3,'c0','c1')
insert into table3(id,prop1,prop2) values(2,'a0','a1')
insert into table3(id,prop1,prop2) values(3,'b0','b1')
with table4(id,prop1,prop2)
as
(
select t2.id,prop1,prop2 from table2 t2
inner join table1 t1 on t1.id = t2.id
union all
(
select t1.id,prop1,prop2 from table1 t1
inner join table4 t4 on t1.InheritedFromID = t4.id
except
select t3.id,t3.prop1,t3.prop2 from table3 t3
inner join table1 t1 on t1.id = t3.id
)
)
select * from table4 order by id,prop1
【在 B*****g 的大作中提到】 : share your solution please
|
|