从2个表中提取信息并将其放在同一列中

时间:2017-07-06 20:46:25

标签: sql sql-server

我有这篇文章

---------------------------------------------
|   Article   |   Location    | existence   | 
---------------------------------------------
|     200116    |   cc3111      |   1       |
---------------------------------------------
|     200116    |   ee3091      |       1   |
---------------------------------------------

这个计数表

----------------------------------------------------
|   Article   |   Location     |   Quantity      |
----------------------------------------------------
|     200116    |   cc3111       |   10            |
----------------------------------------------------
|     200116    |   EE3091       |   8             |
----------------------------------------------------
|     200116    |   EE2102       |   5             |
----------------------------------------------------
|     200116    |   DD5131       |   7             |
----------------------------------------------------

我想做的就是给我一个像下一个结果。

--------------------------------------------------------------------------
|   Article   |   Location    | Existence      |  Quantity |   DIF      |
--------------------------------------------------------------------------
|     200116  |   CC3111      |     1          |     10    |     9       |     
--------------------------------------------------------------------------
|     200116  |   EE3091      |     1          |     8     |      7      | 
--------------------------------------------------------------------------
|     200116  |   DD5131      |     0          |    7      |     7       |  
--------------------------------------------------------------------------
|     200116  |   EE2102      |     0          |    5      |     5      |   
----------------------------- -------------------------------------------

但是,如果不使用Union,那么我必须进行另一次查询,我只是不知道如何在不使用Union的情况下加入它。

- 执行此查询的一些帮助

- 我不知道如何在同一列中添加位置

3 个答案:

答案 0 :(得分:1)

这看起来像left join

select t2.*, coalesce(t1.existence, 0) as existence,
       (t2.quantity - coalesce(t1.existence, 0)) as dif
from t2 left join
     t1
     on t2.article = t1.article and t2.location = t1.location;

答案 1 :(得分:0)

您需要加入这些表,而不是执行 UNION 。前者将并排放置列,而后者将两个相似的源叠加在一起......

答案 2 :(得分:0)

这是另一种选择......

IF OBJECT_ID('tempdb..#Article', 'U') IS NOT NULL 
DROP TABLE #Article;

CREATE TABLE #Article (
    Article INT NOT NULL,
    Location CHAR(6) NOT NULL,
    Existance INT NOT NULL 
    );

IF OBJECT_ID('tempdb..#Counts', 'U') IS NOT NULL 
DROP TABLE #Counts;

CREATE TABLE #Counts (
    Article INT NOT NULL,
    Location CHAR(6) NOT NULL,
    Quantity INT NOT NULL 
    );

INSERT #Article(Article, Location, Existance) VALUES
    (200116, 'cc3111', 1), (200116, 'ee3091', 1);

INSERT #Counts(Article, Location, Quantity) VALUES
    (200116, 'cc3111', 10), (200116, 'EE3091', 8),
    (200116, 'EE2102', 5), (200116, 'DD5131', 10);

SELECT * FROM #Article a;
SELECT * FROM #Counts c;

--=====================================================

SELECT 
    Article = COALESCE(a.Article, c.Article),
    Location = COALESCE(a.Location, c.Location),
    Existance = COALESCE(a.Existance, 0),
    Quantity = COALESCE(c.Quantity, 0),
    DIF =  COALESCE(c.Quantity, 0) - COALESCE(a.Existance, 0)
FROM
    #Article a
    FULL JOIN #Counts c
        ON a.Article = c.Article
        AND a.Location = c.Location;