【问题标题】:How can I union or combine some columns and not others using sql如何使用 sql 联合或组合某些列而不是其他列
【发布时间】:2017-11-17 00:01:55
【问题描述】:

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

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

【问题讨论】:

  • 尽管有您的陈述,但仍有共同的行。如果订单日期在 10 月,并且在 11 月 1 日发货,则该行将出现在前三个报告中。报告 1 和 3 肯定会有重复。你没看到这个是偶然的吗?

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


【解决方案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);

【讨论】:

    【解决方案2】:

    我会对常用列(orderid、orderdate、shipdeddate、itemcode、itemdescription)进行选择区分,然后开始对您的每个查询进行左连接。

    【讨论】:

      【解决方案3】:
      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)
      

      【讨论】:

        猜你喜欢
        • 2015-05-21
        • 2019-10-07
        • 2012-09-27
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-22
        相关资源
        最近更新 更多