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

Posted

技术标签:

【中文标题】如何使用 sql 联合或组合某些列而不是其他列【英文标题】:How can I union or combine some columns and not others using 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 肯定会有重复。你没看到这个是偶然的吗? 【参考方案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)

【讨论】:

以上是关于如何使用 sql 联合或组合某些列而不是其他列的主要内容,如果未能解决你的问题,请参考以下文章

jqgrid更改子网格的位置或将子网格图标添加到自定义列而不是其他任何地方?

pyqt qt4 QTableView 如何禁用某些列的排序?

如何在pyspark中使用等效的熊猫轴来删除列而不是行?

包含使用列而不是文本值或变量的全文索引

如何根据选定的列而不是Oracle中表的所有列获取不同的行

如何在 SELECT 部分中包含 BIT 类型列而不在 T-SQL 中的 GROUP BY 中包含它?