2017-11-17 1 views
0

Ich habe folgende separate Berichte, dass ich eine Bitte kombinieren:Wie kann ich Vereinigung oder einige Spalten kombinieren und andere nicht sql

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

ich diese Berichte kombinieren müssen, so dass sie wie ein ausgeführt werden können Abfrage. Das Problem, auf das ich stoße, ist, dass die Mengenspalte für jeden der Berichte separat sein muss und dass UNION sie kombiniert. Jeder separate Bericht hat möglicherweise keine gemeinsamen Ergebnisse, sodass ein Join nicht funktioniert. So ist das Endergebnis etwas aussehen würde:

orderid, bestelldatum, ShippedDate, itemcode, ItemDescription, QuanitityA, QuantityB, QuantityC, QuantityD

+0

Trotz Ihrer Aussagen gibt es gemeinsame Zeilen. Wenn das Bestelldatum im Oktober ist und es am 1. November ausgeliefert wird, wird diese Zeile in den ersten drei Berichten angezeigt. Die Berichte 1 und 3 werden sicherlich Duplikate haben. Ist es Zufall, dass du das nicht siehst? – SMor

Antwort

1

Nicht OR tun, was Sie wollen?

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 Unter der Annahme, ist immer größer als orderdate (oder NULL), dann können Sie diese Bedingungen entfernen. Ich würde Ihnen raten, auch Standard-Datumsformate verwenden:

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

Ich würde mit den gemeinsamen Spalten eine ausgewählte distint tun (orderid, bestelldatum, ShippedDate, itemcode, ItemDescription) und starten Sie dann mit jeder Ihrer Join-Abfragen zu tun übrig.

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