发布网友 发布时间:2022-04-20 15:12
共1个回答
热心网友 时间:2023-09-19 05:01
在SQL SERVER 2008中的SQL写法:
select c.p1,c.q1 as 'q1(a)(where q2=0)',d.q1 as 'q1(a)(where q2=1)'
from
(select t1.p1,a.q1 from t1
left join (select t1.p1,q1 from t1,t2 where t1.p1=t2.p1 and q2=0) as a on t1.p1=a.p1) as c
,
(select t1.p1,b.q1 from t1
left join (select t1.p1,q1 from t1,t2 where t1.p1=t2.p1 and q2=1) as b on t1.p1=b.p1) as d
where c.p1=d.p1
与楼主要求稍有不同,你要求出现"0"的地方结果是“空”(NULL),我认为这样更合理些。