简体   繁体   English

如何使用SQL合并或合并某些列而不是其他列

[英]How can I union or combine some columns and not others using sql

I have the following separate reports that I have a request to combine: 我有以下要求合并的单独报告:

--REPORT #1
select o.orderid, o.orderdate, o.shippeddate, od.itemcode, od.itemdescription, od.quantity from orders o
join orderdetails od on od.orderid=o.orderid
where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
and o.shippeddate  between '01-NOV-2017' and '17-NOV-2017' 
and o.warehouseid=1 
and o.ordertypeid not in (7,8)
and o.orderstatusid in (7,8,9)

--REPORT #2
select o.orderid, o.orderdate, o.shippeddate, od.itemcode, od.itemdescription, od.quantity from orders o
join orderdetails od on od.orderid=o.orderid
where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
and o.shippeddate  between '01-OCT-2017' and '01-NOV-2017' 
and o.warehouseid=1 
and o.ordertypeid not in (7,8)
and o.orderstatusid in (7,8,9)

--REPORT #3
select o.orderid, o.orderdate, o.shippeddate, od.itemcode, od.itemdescription, od.quantity from orders o
join orderdetails od on od.orderid=o.orderid
where o.orderdate between '01-OCT-2017' and '01-NOV-2017'
and o.shippeddate >='01-NOV-2017'  
and o.warehouseid=1 
and o.ordertypeid not in (7,8)
and o.orderstatusid in (7,8,9)

--REPORT #4
select o.orderid, o.orderdate, o.shippeddate, od.itemcode, od.itemdescription, od.quantity from orders o
join orderdetails od on od.orderid=o.orderid
where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
and o.shippeddate is null
and o.warehouseid=1 
and o.ordertypeid not in (7,8)
and o.orderstatusid in (7,8,9)

I need to combine these reports so they can be run as one query. 我需要合并这些报告,以便它们可以作为一个查询运行。 The issue I am running into is that the quantity column needs to be separate for each of the reports and using UNION combines them. 我遇到的问题是,对于每个报告,数量列都需要分开,并使用UNION对其进行组合。 Each separate Report may not have any results in common so a join doesn't work. 每个单独的报告可能没有共同的结果,因此联接不起作用。 So the end result would be something like: 因此,最终结果将类似于:

orderid, orderdate, shippeddate, itemcode, itemdescription, QuanitityA, QuantityB, QuantityC, QuantityD 订单编号,订单日期,发货日期,项目代码,项目说明,数量A,数量B,数量C,数量D

Doesn't OR do what you want? 没有OR你想要做什么?

select o.orderid, o.orderdate, o.shippeddate, od.itemcode,
       od.itemdescription, od.quantity
from orders o join
     orderdetails od 
     on od.orderid = o.orderid
where o.orderdate between '01-OCT-2017' and '01-NOV-2017' and
      (o.shippeddate between '01-NOV-2017' and '17-NOV-2017' or 
       o.shippeddate between '01-OCT-2017' and '01-NOV-2017' or
       o.shippeddate >= '01-NOV-2017' or
       o.shippeddate is null
      ) and
      o.warehouseid = 1 and
      o.ordertypeid not in (7, 8) and
      o.orderstatusid in (7, 8, 9);

Assuming shippeddate is always greater than orderdate (or NULL ), then you can remove those conditions. 假设shippeddate始终大于orderdate (或NULL ),则可以删除这些条件。 I would also advise using standard date formats: 我也建议使用标准日期格式:

select o.orderid, o.orderdate, o.shippeddate, od.itemcode,
       od.itemdescription, od.quantity
from orders o join
     orderdetails od 
     on od.orderid = o.orderid
where o.orderdate between '2017-10-01' and '2017-11-01' and
      ) and
      o.warehouseid = 1 and
      o.ordertypeid not in (7, 8) and
      o.orderstatusid in (7, 8, 9);

我将对公用列(orderid,orderdate,shippeddate,itemcode,itemdescription)进行选择区分,然后开始对每个查询进行左联接。

IS this what you want?
    select o.orderid
    , o.orderdate
    , o.shippeddate
    , od.itemcode
    , od.itemdescription
    , od.quantity AS QuantityA
    , NULL AS QuantityB
    , NULL AS QuantityC
    , NULL AS QuantityD
    from orders o
    join orderdetails od on od.orderid=o.orderid
    where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
    and o.shippeddate  between '01-NOV-2017' and '17-NOV-2017' 
    and o.warehouseid=1 
    and o.ordertypeid not in (7,8)
    and o.orderstatusid in (7,8,9)
    UNION
    select o.orderid
    , o.orderdate
    , o.shippeddate
    , od.itemcode
    , od.itemdescription
    , NULL AS QuantityA
    , od.quantity AS QuantityB
    , NULL AS QuantityC
    , NULL AS QuantityD 
    from orders o
    join orderdetails od on od.orderid=o.orderid
    where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
    and o.shippeddate  between '01-OCT-2017' and '01-NOV-2017' 
    and o.warehouseid=1 
    and o.ordertypeid not in (7,8)
    and o.orderstatusid in (7,8,9)
    UNION
    select o.orderid
    , o.orderdate
    , o.shippeddate
    , od.itemcode
    , od.itemdescription
    , NULL AS QuantityA
    , NULL AS QuantityB
    , od.quantity AS QuantityC
    , NULL AS QuantityD
    from orders o
    join orderdetails od on od.orderid=o.orderid
    where o.orderdate between '01-OCT-2017' and '01-NOV-2017'
    and o.shippeddate >='01-NOV-2017'  
    and o.warehouseid=1 
    and o.ordertypeid not in (7,8)
    and o.orderstatusid in (7,8,9)
    UNION
    select o.orderid
    , o.orderdate
    , o.shippeddate
    , od.itemcode
    , od.itemdescription
    , NULL AS QuantityA
    , NULL AS QuantityB
    , NULL AS QuantityC
    , od.quantity AS QuantityD
    from orders o
    join orderdetails od on od.orderid=o.orderid
    where o.orderdate between '01-OCT-2017' and '01-NOV-2017' 
    and o.shippeddate is null
    and o.warehouseid=1 
    and o.ordertypeid not in (7,8)
    and o.orderstatusid in (7,8,9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM