将缺少的成员添加到其他表SQL Server的表中

时间:2019-03-22 15:58:42

标签: sql sql-server database tsql

我有2张桌子:

第一个表是[Table_Time]:

Month Number    Week Number
------------    -----------
1               01
1               02
1               03
1               04
2               05
2               06
2               07
2               08
3               09
3               10
3               11
3               12

第二张表是[销售]:

Product         Store           Month Number    Week Number
------------    -----------     ------------    -----------
P1              ST1             1               03
P1              ST1             1               04
P1              ST1             2               05
P1              ST1             3               09
P1              ST1             3               11
P1              ST1             3               12

我的问题是:

如何在表[销售]中添加缺少的几周?

我的预期结果应该是:

Product         Store           Month Number    Week Number
------------    -----------     ------------    -----------
P1              ST1             1               01
P1              ST1             1               02
P1              ST1             1               03
P1              ST1             1               04
P1              ST1             2               05
P1              ST1             2               06
P1              ST1             2               07
P1              ST1             2               08
P1              ST1             3               09
P1              ST1             3               10
P1              ST1             3               11
P1              ST1             3               12

谢谢!

3 个答案:

答案 0 :(得分:2)

您可以使用“反联接”来进行INSERT,如:

insert into sales (product, store, month_number, week_number)
select 'P1', 'ST1', t.month_number, t.week_number
from table_time t
left join sales s on t.month_number = s.month_number
                 and t.week_number = s.week_number
where s.month_number is null and s.week_number is null

答案 1 :(得分:1)

这是一种方法,您需要在产品和日期之间创建笛卡尔产品。使用EXCEPT只能带来所需的值,因此可以将它们插入到Sales表中。

WITH AllProducts AS( --This can be avoided if you already have a list of them outside of Sales
    SELECT DISTINCT Product, Store
    FROM Sales
)
SELECT p.Product,
       p.Store,
       t.MonthNumber,
       t.WeekNumber
FROM AllProducts p
CROSS JOIN Table_Time t
EXCEPT
SELECT s.Product,
       s.Store,
       s.MonthNumber,
       s.WeekNumber
FROM Sales s;

答案 2 :(得分:1)

假设您有商店和产品表,只需用作CROSS JOIN

USE Sandbox;
GO
CREATE TABLE Table_time (MonthNumber int,
                         WeekNumber int);
INSERT INTO dbo.Table_time (MonthNumber,
                            WeekNumber)
VALUES (1,01),
       (1,02),
       (1,03),
       (1,04),
       (2,05),
       (2,06),
       (2,07),
       (2,08),
       (3,09),
       (3,10),
       (3,11),
       (3,12);

CREATE TABLE Store (StoreID varchar(3));
INSERT INTO Store (StoreID)
VALUES ('ST1'),('ST2');
GO

CREATE TABLE Product (ProductID varchar(2));
INSERT INTO Product
VALUES('P1'),('P2'),('P3'),('P4');
GO

SELECT *
FROM Table_time
     CROSS JOIN Product
     CROSS JOIN Store;
GO

如果您随后要进行汇总,则可以向LEFT JOIN表中添加sales

SELECT TT.MonthNumber,
       TT.MonthNumber,
       P.ProductID,
       S.StoreID,
       COUNT(SS.ID) 
FROM Table_time TT
     CROSS JOIN Product P
     CROSS JOIN Store S
     LEFT JOIN Sales SS ON TT.MonthNumber = SS.MonthNumber
                       AND TT.WeekNumber = SS.WeekNumber
                       AND P.ProductID = SS.ProductID
                       AND S.StoreID = SS.StoreID;