将数据从一个表#1复制到表#2,然后删除表#2的其余行以获取特定的销售发票ID

时间:2019-04-02 17:20:43

标签: sql-server

很抱歉,我对SQL查询不了解。我必须修改一个记录和维护客户分期付款的现有应用程序。

为此,我有一个要复制日期的要求 将TableNo1TableNo2(TableNo1)列数据之外的New_Amount放入TableNo2中,然后为每个SalesInvoiceID删除TablNo1的其余行。

实际上TableNo2修改了分期付款计划,这就是为什么需要相应修改New_ID SalesInvoiceID InsttNo DueDate New_Amount 1 30 1 2019-05-02 12000 2 30 2 2019-06-02 12000 3 30 3 2019-09-02 4000 4 30 4 2019-12-02 4000 的原因。

TableNo1

Instt_ID   SalesInvoiceID   InsttNo   DueDate   PaymentDate   Amount    Status
51              30            1     2019-05-02      NULL       0        Up-Coming
52              30            2     2019-06-02      NULL       0        Up-Coming
53              30            3     2019-07-02      NULL       0        Up-Coming
54              30            4     2019-08-02      NULL       0        Up-Coming
55              30            5     2019-09-02      NULL       0        Up-Coming
56              30            6     2019-10-02      NULL       0        Up-Coming
57              30            7     2019-11-02      NULL       0        Up-Coming
58              30            8     2019-12-02      NULL       0        Up-Coming

TableNo2

Instt_ID   SalesInvoiceID   InsttNo   DueDate   PaymentDate   Amount    Status
51            30             1       2019-05-02     NULL        0       Up-Coming
52            30             2       2019-06-02     NULL        0       Up-Coming
53            30             3       2019-09-02     NULL        0       Up-Coming
54            30             4       2019-12-02     NULL        0       Up-Coming

必需的输出(表2)

ob_start();

2 个答案:

答案 0 :(得分:0)

如果我正确地猜测了表之间的关系,这应该是您所需要的,但是在删除任何生产数据之前,应该在表的副本上对其进行测试。

UPDATE t2
  SET t2.DueDate = t1.DueDate
FROM
  dbo.TableNo2 AS t2
  JOIN
  dbo.TableNo1 AS t1
    ON t1.SalesInvoiceID = t2.SalesInvoiceID
    AND t1.InsttNo = t2.InsttNo;

DELETE t2
FROM 
  dbo.TableNo2 AS t2
WHERE 
  NOT EXISTS
    (
      SELECT 1 
      FROM dbo.TableNo1 AS t1
      WHERE t1.SalesInvoiceID = t2.SalesInvoiceID
      AND  t1.InsttNo = t2.InsttNo
    );

答案 1 :(得分:0)

CREATE TABLE #Table1 (New_ID INT IDENTITY (1,1) PRIMARY KEY,SalesInvoiceID 
INT,InsttNo INT,PaymentDate DATE,New_Amount INT )
CREATE TABLE #Table2 (New_ID INT IDENTITY (1,1) PRIMARY KEY,Instt_ID INT, 
SalesInvoiceID INT,InsttNo INT,DueDate DATE,PaymentDate DATE,Amount INT,Status 
VARCHAR(20))


INSERT INTO #Table1  (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30 
,1,'2019-05-02','12000')
INSERT INTO #Table1  (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30 
,2,'2019-06-02','12000')
INSERT INTO #Table1  (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30 
,3,'2019-09-02','4000')
INSERT INTO #Table1  (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30 
,4,'2019-12-02','4000')


INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(51,30,1,'2019-05-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(52,30,2,'2019-06-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(53,30,3,'2019-07-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(54,30,4,'2019-08-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(55,30,5,'2019-09-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(56,30,6,'2019-10-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(57,30,7,'2019-11-02',NULL,0,'Up-Coming')
INSERT INTO #Table2 
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES 
(58,30,8,'2019-12-02',NULL,0,'Up-Coming')



SELECT 
 T2.Instt_ID
,T1.SalesInvoiceID
,T1.InsttNo
,T1.PaymentDate DueDate
,T2.PaymentDate
,T2.Amount
,T2.Status 
 FROM #Table1 t1
 INNER JOIN #Table2 T2 ON t1.New_ID = t2.New_ID

 DROP TABLE #Table1,#Table2

enter image description here