如何使用包含参考值的第三个表将sql表拆分为两个表

时间:2018-08-20 19:58:52

标签: sql-server

我是这里的新手,也是sql的新手,所以我希望我问的问题正确无误。

表内部产品EAN

EAN/UPC 
1234567789 
2233445566

餐桌店销售

EAN/UPC    | product | sales value |
1234567789 | xyz     | 200         |
2233445566 | abc     | 100         |
9685444444 | yyy     | 150         |

结果应如下所示:

列出我的公司销售额

EAN/UPC    | product | sales value |
1234567789 | xyz     | 200         |
2233445566 | abc     | 100         |

列出我的竞争对手的销售情况

EAN/UPC    | product | sales value |
9685444444 | yyy     | 150         |

我有所有可用的EAN / UPC代码(约100.000) 我正在从商店收到销售数据,其中包括我需要与我分开的竞争对手的EAN / UPC代码。我想使用第一个表作为参考,如果将EAN / UPC匹配的值移动到我公司销售的表中,而其他没有EAN / UPC匹配的值移动到我的竞争对手销售表中。

我当时正在考虑在EAN / UPC T1不是EAN / UPC T2的情况下使用select into语句。

非常感谢您的帮助。

4 个答案:

答案 0 :(得分:0)

Select * 
into CompanySales
from ShopSales
where [EAN/UPC] in (select [EAN/UPC] from productEAN);

Select * 
into CompetitorSales
from ShopSales
where [EAN/UPC] not in (select [EAN/UPC] from productEAN);

答案 1 :(得分:0)

这应该可以解决:

使用CTE进行设置(出于测试目的)

        var claims = new List<Claim>
        {
            new Claim(JwtRegisteredClaimNames.Sub, user.Id),
            new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
            new Claim(JwtRegisteredClaimNames.Email, email),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };

两个查询以提取信息:

WITH [shop sales] ([EAN/UPC], [product], [sales value]) AS (
    SELECT * FROM (
        VALUES
            (1234567789,'xyz',200), 
            (2233445566,'abc',100), 
            (9685444444,'yyy',150)
    ) AS A (Column1, Column2, Column3)
),
[internal product EAN] ([EAN/UPC]) AS ( 
    SELECT * FROM (
        VALUES
            (1234567789), 
            (2233445566)
    ) AS A (Column1)
)

就根据数据创建表而言,为新表使用SELECT s.* FROM [internal product EAN] ip INNER JOIN [shop sales] s ON ip.[EAN/UPC] = s.[EAN/UPC] SELECT s.* FROM [shop sales] s WHERE s.[EAN/UPC] NOT IN (SELECT [EAN/UPC] FROM [internal product EAN]) INSERT INTO可能就足够了。

答案 2 :(得分:0)

尝试这样的事情:

INSERT INTO OnlyMyProductSales([EAN],[product],SalesValue)
SELECT s.*
FROM [MyProduct] p
INNER JOIN [AllSales] s 
    ON p.[EAN] = s.[EAN]

INSERT INTO MyCompetitionSales([EAN],[product],SalesValue)
SELECT s.*
FROM  [AllSales] s
    LEFT JOIN [MyProduct] p 
        ON p.[EAN] = s.[EAN]
WHERE s.[EAN] IS NULL

答案 3 :(得分:0)

这将有助于:

CREATE TABLE #internalproductEAN (EAN_UPC  VARCHAR(50))

INSERT INTO #internalproductEAN
SELECT 1234567789 UNION ALL 
SELECT 2233445566


CREATE TABLE #shopsales (EAN_UPC  VARCHAR(50),product VARCHAR(10),SalesValue BIGINT)
INSERT INTO #shopsales
SELECT '1234567789','xyz',200  UNION ALL
SELECT '2233445566','abc',100  UNION ALL
SELECT '9685444444','yyy',150 

CREATE TABLE #companysales (EAN_UPC  VARCHAR(50),product VARCHAR(10),SalesValue BIGINT)


INSERT INTO #companysales
SELECT ss.* FROM #shopsales ss
INNER JOIN #internalproductEAN ip ON ss.EAN_UPC=ip.EAN_UPC

SELECT * FROM #companysales

CREATE TABLE #competitorsales (EAN_UPC  VARCHAR(50),product VARCHAR(10),SalesValue BIGINT)

INSERT INTO #competitorsales
SELECT ss.* FROM #shopsales ss
LEFT JOIN #internalproductEAN ip ON ss.EAN_UPC=ip.EAN_UPC
WHERE ip.EAN_UPC IS NULL

SELECT * FROM #competitorsales


--SELECT * FROM #internalproductEAN
--SELECT * FROM #shopsales

DROP TABLE #internalproductEAN
DROP TABLE #shopsales
DROP TABLE #companysales
DROP TABLE #competitorsales