我描述一下逻辑关系,求大神写一下大概的语法。
具体是这样的,有两个主表和一个明细表,主表A,B,明细表B1。B1是B的明细表。
A中字段,ffpman,ano,
B中字段,rpno,bno,abno
B1中字段,bno,line
ffpman中会包含多个rpno,一个B只有一个rpno,B1和B通过bno关联,需要查出ffpman 包含rpno时对应的
B表中是否有abno(abno=ano),如果没有,需要显示出这个rpno,ano;如果有,再判断B1表中line是否有值,如果没有,需要显示出rpno,ano
------解决思路----------------------
--你试试,可能有待优化
if not exists(select * from B join A on b.rpno=a.rpno where B.abno=A.ano)
select B.rpno,A.ano from B join A on b.rpno=a.rpno
else
if not exists(select * from B join A on b.rpno=a.rpno join B1 on B.bno=B1.bno where B.abno=A.ano and B1.line is null)
select B.rpno,A.ano from B join A on b.rpno=a.rpno
------解决思路----------------------
select
lno 询价单号,
fxdtype 询价类型,
ldate 询价日期,
lwho 询价人,
'33' 采购人员,
fdate4 要求报价日期,
spfax 要求报价时间,
M.Fsigndate 生效时间,
datediff(hour,M.Fsigndate,Getdate()) 超期小时数
From spquesctrl s1
left join (select max(Fsigndate) Fsigndate,Fno from tcheckrecord where fmoduleid=501 group by Fno)M on M.Fno=S1.lno
where ldate>'2013-01-01' and placed='T' and
datediff(hour,M.Fsigndate,Getdate())<24
and not exists(select null from splsdtl s left join splsctrl B on B.lno=s.lno
where s.hisno=s1.lno and B.placed='T' and isnull(b.Fxdtype,'')=isnull(S1.Fxdtype,'') )
and s1.ffpman like '%Buy021%'
]
建议把表中的数据和结构比较完整的贴出来,并贴出想要得到的结果,这样会便于尽快解决问题