解决方案 »
- 怎样在互联网上访问铁通宽带的服务器
- 请问用select、set 赋值有什么区别?
- 请问如何用SQL语句把图片中的形式写出来?
- 关于text字段内容被截??????????
- 请教一SQL语句
- [MS 可视化数据库工具]问题求解!!
- 请问各位
- MS SQL存储过程奇怪的问题求解.
- 紧急求助.用C#开发得winform程序,连不上数据库,需求帮助.
- SUM(CASE KM WNEN KM THEN CJ END)[KM] FROM (SELECT DISTINCT KM FROM TEST) AS A 中如何理解,其中[]是什么意思
- 关于插入数据的问题!
- 事务(进程 ID 90)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务
s,t,dept1,dept2
1,1,1,2
2,NULL,3,3
2,2,4,5
NULL,2,6,6
3,NULL,7,9
NULL,3,12,19
declare @t1 table(
s int,
dept1 int,
dept2 int
)
insert @t1 select
1,1,2 union all select
2,3,5 union all select
3,7,9 --表【2】
declare @t2 table(
t int,
dept1 int,
dept2 int
)
insert @t2 select
1,1,2 union all select
2,4,9 union all select
3,12,19 declare @depts table (
dept int
)
insert @depts
select dept1 from @t1
union select dept2 from @t1
union select dept1 from @t2
union select dept2 from @t2select
c.s,d.t,a.dept as dept1,b.dept as dept2
from @depts a inner join @depts b
on a.dept <b.dept and not exists (select 1 from @depts where dept>a.dept and dept<b.dept )
left join @t1 c
on c.dept1 <= a.dept and c.dept2 >= b.dept
left join @t2 d
on d.dept1 <= a.dept and d.dept2 >= b.dept
where not (c.s is null and d.t is null)
--查询得到目的表格式
s t dept1 dept2
1 1 1 2
2 NULL 3 4
2 2 4 5
NULL 2 5 7
3 2 7 9
NULL 3 12 19