SQL连接表并从同一列中的两个表获取数据

时间:2019-07-16 14:27:53

标签: sql-server

编辑这是我的简化问题

我需要“销售和参考”表中的数据

销售会向我返回已转移金额的数据

参考返回我的数据以及要供应的数量,并按库存过滤

基本上,我需要获取与引用匹配的结果,但我还需要添加未出现在“引用”表上但被where子句忽略的结果

这是我一开始使用的实际更新查询

SELECT 
    A.Reference,
    B.QtyToSupply,
    A.QtyTransfered
FROM 
    SALES A
RIGHT JOIN 
    REFERENCES B ON A.Reference = B.Reference
WHERE B.InventoryId = 1

更新示例

SELECT 
    Reference,
    QtyTransfered
FROM 
    SALES 

这将返回此数据:

  Reference   |  QtyTransfered
     M1              200
     M1              200
     M2              200
     M4              500
     M5              250
     M6              300

联合查询返回此数据

SELECT 
    A.Reference,
    B.QtyToSupply,
    A.QtyTransfered
FROM 
    SALES A
RIGHT JOIN 
    REFERENCES B ON A.Reference = B.Reference
WHERE B.InventoryId = 1


  Reference   |  QtyToSupply    | QtyTransfered
     M2              200              200
     M4              500              500
     M6              300              300
     M9              800              NULL
     M10             800              NULL

使用左联接

Reference   |  QtyToSupply    | QtyTransfered
     M2              200              200
     M4              500              500
     M6              300              300

输出应为

   Reference   |  QtyToSupply    | QtyTransfered
         M2              200              200
         M4              500              500
         M6              300              300
         M9              800              NULL
         M10             800              NULL
         M1              NULL             200
         M1              NULL             200
         M5              NULL             250

最终结果

这是我困惑的原因,因为我以前从未使用过工会,所以我设法达到预期的结果。

   SELECT 
        A.Reference,
        B.QtyToSupply,
        A.QtyTransfered
    FROM 
        SALES A
    RIGHT JOIN 
        REFERENCES B ON A.Reference = B.Reference
    WHERE B.InventoryId = 1
    UNION
    SELECT 
        S.Reference,
        R.QtyToSupply,
        S.QtyTransfered
    FROM 
        SALES S
    WHERE NOT EXISTS ( SELECT 1 FROM References R WHERE R.Reference= S.Reference)

。稍后我将编辑问题,以免遇到相同问题的人感到困惑

2 个答案:

答案 0 :(得分:2)

您需要完全连接,请参见以下SQL

    create table #SALES 
    (
    Reference varchar(5),
    QtyTransfered int
    )

    create table #REFERENCES 
    (
    Reference varchar(5),
    QtyToSupply int
    )

    insert into #SALES
    select 'M1',200 UNION ALL
    select 'M1',200 UNION ALL
    select 'M2',200 UNION ALL
    select 'M4',500 UNION ALL
    select 'M5',250 UNION ALL
    select 'M6',300 

    insert into #REFERENCES 
    select 'M2' ,200 UNION ALL   
    select 'M4' ,500 UNION ALL    
    select 'M6' ,300 UNION ALL    
    select 'M9' ,800 UNION ALL    
    select 'M10',800       

    SELECT 
    COALESCE(b.Reference,a.Reference)Reference,
    B.QtyToSupply,
    A.QtyTransfered
    FROM #SALES A
    full JOIN #REFERENCES B
    ON A.Reference = B.Reference
    order by 2 DESC

    DROP TABLE #REFERENCES
    DROP TABLE #SALES

添加输出

     Reference QtyToSupply QtyTransfered
    --------- ----------- -------------
    M9        800         NULL
    M10       800         NULL
    M4        500         500
    M6        300         300
    M2        200         200
    M5        NULL        250
    M1        NULL        200
    M1        NULL        200

答案 1 :(得分:0)

如果您与UNION ALL一起执行子查询该怎么办?...

SELECT  A.Reference,
   (SELECT b. qtytosupply FROM "REFERENCES" b where A.Reference = B.Reference) QtyToSupply,
   A.QtyTransfered
FROM SALES A 
union all
SELECT 
   A.Reference,
   A.QtyToSupply,
    (SELECT b.qtytransfered FROM SALES b where A.Reference = B.Reference)  QtyTransfered
FROM "REFERENCES" A