【发布时间】: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