如何组合两个具有不同条件的SQLITE查询以返回一对行

时间:2016-05-24 17:01:44

标签: sql sqlite join where-in

我有一些送货数据,让我们说英国超市。我感兴趣的数据由两个select查询组成,如下所示:

select t2.COMNAME, t0.DELDATE, t1.DELSERN, t1.DELEACTVOL, t3.PRODNAME, t3.PRODEANC from TDELIVER t0 inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) 
inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO)
inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN) 
where t1.DELEACTVOL < 0

这会产生DELEACTVOL 负数的结果,类似这样,但有更多条目和实际数据:

Negative Volumes

select t2.COMNAME, t0.DELDATE, t1.DELSERN, t1.DELEACTVOL, t3.PRODNAME, t3.PRODEANC from TDELIVER t0 inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) 
inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO)
inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN)

如果DELEACTVOL 为负数或正数,会产生所有结果,如下所示:

All Volumes

正如您所看到的,除了过滤掉(大多数)正数量条目的事实外,这两个查询是相同的。每次投放都有一个唯一的DELSERN

我需要一个查询,每次DELSERN(每次投放),例如DELSERN XXXX.......89,在5月24日交付给Tesco,其中包含一些产品,例如{ {1}},Product A ,如果该产品在同一次投放中negative delivery volume。在示例数据中,不应返回positive delivery volume

我觉得我应该使用XXXX.......37WHERE IN,但我不知道。

我试过了:

JOIN

...希望select t2.COMNAME, t0.DELDATE, t1.DELEACTVOL, t3.PRODNAME, t3.PRODEANC from TDELIVER t0 inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO) inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN) where t1.DELEACTVOL < 0 and EXISTS(select p2.COMNAME, p0.DELDATE, p1.DELEACTVOL, p3.PRODNAME, p3.PRODEANC from TDELIVER p0 inner join TDELENTR p1 on (p1.DELSERN=p0.DELSERN) inner join TCOMPANY p2 on (p2.COMSERNO=p0.COMSERNO) inner join TPRODUCT p3 on (p1.PRODSERN=p3.PRODSERN) where p1.DELEACTVOL > 0 and t1.DELSERN = p1.DELSERN) ORDER by t1.DELEACTVOL 部分可以完成这个技巧,但这会返回与第一个查询相同的部分。我想因为产品A的​​负数量的每次交货至少会有一些正面的产品?我可能也需要考虑and t1.DELSERN = p1.DELSERN吗?

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

您可以join deleactvol以外的所有列上的查询,并选择所需的列。

select p1.delsern,p1.deleactvol,p2.deleactvol
from (
select t2.COMNAME, t0.DELDATE,t1.DELSERN, t1.DELEACTVOL, t3.PRODNAME, t3.PRODEANC 
from TDELIVER t0 
inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) 
inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO)
inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN) 
where t1.DELEACTVOL < 0) p1
join (select t2.COMNAME, t0.DELDATE,t1.DELSERN, t1.DELEACTVOL, t3.PRODNAME, t3.PRODEANC 
      from TDELIVER t0 
      inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) 
      inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO)
      inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN) 
      where t1.DELEACTVOL > 0) p2
on p1.DELSERN = p2.DELSERN and p1.COMNAME = p2.COMNAME 
and p1.DELDATE = p2.DELDATE and p1.PRODNAME = p2.PRODNAME and p1.PRODEANC = p2.PRODEANC 

如果每个delsern只能有一个正面和负面的deleactvol,你可以

select t2.COMNAME, t0.DELDATE, t1.DELSERN, t3.PRODNAME, t3.PRODEANC 
from TDELIVER t0 
inner join TDELENTR t1 on (t1.DELSERN=t0.DELSERN) 
inner join TCOMPANY t2 on (t2.COMSERNO=t0.COMSERNO)
inner join TPRODUCT t3 on (t1.PRODSERN=t3.PRODSERN) 
group by t2.COMNAME, t0.DELDATE, t1.DELSERN, t3.PRODNAME, t3.PRODEANC
having sum(case when t1.DELEACTVOL < 0 then -1 when t1.DELEACTVOL > 0 then 1 end) = 0