如何使用sql联合或组合某些列而不是其他列

时间:2017-11-17 00:01:55

标签: sql sql-server sql-server-2014

我有以下单独的报告,我有要求合并:

--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)

我需要合并这些报告,以便它们可以作为一个查询运行。我遇到的问题是每个报告的数量列需要是独立的,并且使用UNION将它们组合在一起。每个单独的报告可能没有任何共同的结果,因此联接不起作用。所以最终结果将是:

orderid,orderdate,shippingdate,itemcode,itemdescription,QuanitityA,QuantityB,QuantityC,QuantityD

3 个答案:

答案 0 :(得分:1)

没有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);

假设shippeddate总是大于orderdate(或NULL),那么您可以删除这些条件。我还建议使用标准日期格式:

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);

答案 1 :(得分:0)

我会使用公共列(orderid,orderdate,shipmentdate,itemcode,itemdescription)进行select distint,然后开始对每个查询进行左连接。

答案 2 :(得分:0)

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)