如何在一个视图中联接两个表?

时间:2019-06-11 01:16:25

标签: sql sql-server sql-server-2012

我正在尝试创建一个视图,在该视图中可以看到已计划要运输且尚未运输的物品,以及已运输但尚未计划的物品。 为此,我有2个表,其中有不同的数据。

表SC(实际发货):

+---------+-----------------+----------------------+-------------+
| item_id | source_location | destination_location | shipped_qty |
+---------+-----------------+----------------------+-------------+
| 001     |    California   |    South_Carolina    |      80     |
+---------+-----------------+----------------------+-------------+
| 001     |    California   |    South_Carolina    |      0      |
+---------+-----------------+----------------------+-------------+
| 001     |    California   |         Texas        |      20     |
+---------+-----------------+----------------------+-------------+
| 003     |      Texas      |    South_Carolina    |     200     |
+---------+-----------------+----------------------+-------------+
| 004     |  South_Carolina |         Texas        |      30     |
+---------+-----------------+----------------------+-------------+
| 004     |  South_Carolina |         Texas        |      10     |
+---------+-----------------+----------------------+-------------+

表格SO(计划发货):

+---------+-----------------+----------------------+---------------+
| item_id | source_location | destination_location | planned_order |
+---------+-----------------+----------------------+---------------+
| 001     |    California   |    South_Carolina    |      100      |
+---------+-----------------+----------------------+---------------+
| 001     |    California   |    South_Carolina    |      100      |
+---------+-----------------+----------------------+---------------+
| 001     |    California   |         Texas        |       10      |
+---------+-----------------+----------------------+---------------+
| 003     |      Texas      |    South_Carolina    |      200      |
+---------+-----------------+----------------------+---------------+
| 004     |  South_Carolina |         Texas        |      300      |
+---------+-----------------+----------------------+---------------+
| 004     |  South_Carolina |         Texas        |      50       |
+---------+-----------------+----------------------+---------------+

因此,在这种情况下,例如,由于项目001具有从加利福尼亚到南卡罗来纳州的三个不同的计划订单,因此我不希望它在视图中显示所有三个订单,因此我希望它只包含一个行,但 sum 一起所有计划的订单,如下所示。

所需结果:

+---------+----------------+-----------------+-------------+-------------+
| item_id |   source_loc   | destination_loc | shipped_qty | planned_qty |
+---------+----------------+-----------------+-------------+-------------+
| 001     |   California   |  South_Carolina |      80     | 200         |
+---------+----------------+-----------------+-------------+-------------+
| 001     |   California   |      Texas      |      20     | 10          |
+---------+----------------+-----------------+-------------+-------------+
| 003     |      Texas     |  South_Carolina |     200     | 200         |
+---------+----------------+-----------------+-------------+-------------+
| 004     | South_Carolina |      Texas      |      40     | 350         |
+---------+----------------+-----------------+-------------+-------------+

到目前为止,我已经尝试过:

SELECT  o.source_location,
        o.destination_location,
        o.item_id,
        o.planned_order,
        c.shipped_qty
        FROM SO_TRANSFER o, SC_TRANSFER c   

但是这没有用,因为shiped_qty与商品不匹配,并且此代码也没有将订单加在一起。

顺便说一句,我正在使用Microsoft SQL Server 2012。 谢谢!

5 个答案:

答案 0 :(得分:3)

我想你想要

select coalesce(s.item_id, p.item_id) as item_id,
       coalesce(s.source_location, p.source_location) as source_location,
       coalesce(s.destination_location, p.destination_location) as destination_location,
       coalesce(s.shipped_qty, 0) as shipped_qty,
       coalesce(planned_qty, 0) as planned_qty
from (select item_id, source_location, destination_location, sum(shipped_qty) as shipped_qty
      from sc
      group by item_id, source_location, destination_location
     ) s full join
     (select item_id, source_location, destination_location, sum(planned_qty) as planned_qty
      from so
      group by item_id, source_location, destination_location
     ) p
     on s.item_id = p.item_id and
        s.source_location = p.source_location and
        s.destination_location = p.destination_location;

答案 1 :(得分:1)

您可以尝试以下方法:

SELECT A.item_id,
       A.source_location AS source_loc,
       A.destination_location AS destination_loc,
       A.shipped_qty,
       B.planned_order
FROM 
      (SELECT item_id,source_location,destination_location,SUM(shipped_qty) 
      AS shipped_qty
      FROM SC GROUP BY item_id,source_location,destination_location) A, 

      (SELECT item_id,source_location,destination_location,SUM(planned_order) 
      AS planned_order
      FROM SO GROUP BY item_id,source_location,destination_location) B

WHERE A.item_id = B.item_id AND 
      A.source_location= B.source_location AND
      A.destination_location= B.destination_location

编辑:我只是意识到我的答案与Gordon Linoff's answer类似,并且他的答案具有更多功能,例如仅在T-SQL和{{3}中使用COALESCE处理一个表中存在的数据}属性。由于我为这个答案工作了1个小时,所以我将其留在这里。

答案 2 :(得分:0)

您可以创建一个SELECT语句,并将其用作FROM子句中的表:

SELECT o.source_location,
       o.destination_location,
       o.item_id,
       o.planned_order,
       c.shipped_qty_sum
FROM SO_TRANSFER o
INNER JOIN (SELECT SUM(shipped_qty) AS shipped_qty_sum,
                   source_location,
                   item_id
            FROM SC_TRANSFER
            GROUP BY source_location, item_id) c
ON o.item_id = c.item_id AND o.source_location = c.source_location

答案 3 :(得分:0)

重点是您应该首先从每个表中求和(数量),然后可以轻松地将JOIN 2表与条件结合起来:  ON so.item_id = sc.item_id AND so.source_loc = sc.source_loc AND so.destination_loc = sc.destination_loc

答案 4 :(得分:0)

以下查询将满足您的需求

logging