SQL查询从表的每个记录生成多个记录

时间:2016-08-26 08:17:24

标签: sql sql-server vba excel-vba excel

我有一个包含3000条记录的表,并且使用每一条记录,我必须生成大约200条记录(总共600条记录),并通过SQL Server 2012将它们插入第二个表中。

我尝试使用VBA(从第一个表中选择数据,计算,然后插入第二个表),但插入太慢,并且需要永久。有人建议我直接在SQL中进行计算/插入,这就是我想要做的,但我对这种语言并不是很熟悉。

这基本上是VBA代码:

SQLStr = "SELECT * FROM TABLE_PRETS"
Set rs = cn.Execute(SQLStr)

If Not (rs.EOF And rs.BOF) Then
  rs.MoveFirst
    Do Until rs.EOF = True
      loanID = rs!N_CONTRAT
      remainingBalance = rs!MONTANT_CREDIT
      interestRate = rs!TAUX_ACTUEL / 100
      insuranceRate = rs!TAUX_ASSURANCE
      taxRate = rs!TVA
      startDate = rs!DATE_DEBUT
      monthlyRate = (interestRate * (1 + taxRate) + insuranceRate) / 12

      For i = 1 To rs!DUREE
        startingBalance = Round(remainingBalance, 2)
        principal = Round(rs!MENSUALITE - startingBalance * monthlyRate, 2)
        interestPaymentBT = Round(startingBalance * interestRate / 12, 2)
        taxOnInterest = Round(interestPaymentBT * taxRate, 2)
        insurancePayment = Round(startingBalance * insuranceRate, 2)
        remainingBalance = Round(startingBalance - principal, 2)

        SQLStr = "INSERT INTO TABLE_AMORTISSEMENT (N_CONTRAT,MOIS,DATE_ECHEANCE,MENSUALITE,SOLDE_DEPART,CAPITAL_AMORTI," _
        & "INTERET_HT,TVA,ASSURANCE,CAPITAL_RESTANT)" _
        & "VALUES (" & loanID & ", " & i & ", '" & DateAdd("m", i, startDate) & "', " & Replace(rs!MENSUALITE, ",", ".") & ", " _
        & Replace(startingBalance, ",", ".") & ", " & Replace(principal, ",", ".") & ", " & Replace(interestPaymentBT, ",", ".") _
        & ", " & Replace(taxOnInterest, ",", ".") & ", " & Replace(insurancePayment, ",", ".") & ", " _
        & Replace(remainingBalance, ",", ".") & ");"
        Set rs2 = cn.Execute(SQLStr, , adExecuteNoRecords)
      Next i
      rs.MoveNext
    Loop
End if

这是我在SQL中到目前为止的地方,但我很困惑:

DECLARE contracts_cursor CURSOR FOR
  SELECT N_CONTRAT, DATE_DEBUT, DUREE, MONTANT_CREDIT, TAUX_ACTUEL / 100 AS TAUX_CREDIT, TAUX_ASSURANCE, TVA, (TAUX_ACTUEL / 100  * (1 + TVA) + TAUX_ASSURANCE) / 12 AS TAUX_MENSUEL
  FROM TABLE_PRETS

DECLARE @index INT, @startingBalance FLOAT, @principal FLOAT, @interestPaymentBT FLOAT, @taxOnInterest FLOAT, @insurancePayment FLOAT, @remainingBalance FLOAT
SET @index = 0;

OPEN contracts_cursor;
FETCH NEXT FROM contracts_cursor;

WHILE @@FETCH_STATUS = 0
  BEGIN
    FETCH NEXT FROM contracts_cursor;
    --My brain stops working here
  END;

CLOSE contracts_cursor;
DEALLOCATE contracts_cursor;
GO

问题是,我不知道SQL是否可以一次计算整个列(如Matlab),或者我是否必须遍历列中的值。

第一个表DATA的例子: enter image description here

第一个表数据(实际上是84行)的结果的例子:

enter image description here

2 个答案:

答案 0 :(得分:1)

不确定为什么要这样做但不需要使用CURSOR来执行此操作。试试这个基于SET的方法

首先使用下面的堆叠CTE生成200条记录。使用前10个记录交叉连接它们以生成虚拟的200条记录。执行每个CTE以了解结果(生成的记录数)。

;WITH fst(n)
     AS (SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1),
     scd(n)
     AS (SELECT a.n
         FROM   fst a
                CROSS JOIN fst b),
     data(n)
     AS (SELECT a.n
         FROM   scd a
                CROSS JOIN (SELECT 1 a
                            UNION ALL
                            SELECT 1) b)
SELECT *
FROM  data --200 records

生成200条 dummy(1)数据的记录后,交叉加入原始表格,得到CTE数据的笛卡尔乘积和原始表格(即)3000 * 200 = 600k

;WITH fst(n)
     AS (SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1 UNION ALL
         SELECT 1),
     scd(n)
     AS (SELECT a.n
         FROM   fst a
                CROSS JOIN fst b),
     data(n)
     AS (SELECT a.n
         FROM   scd a
                CROSS JOIN (SELECT 1 a
                            UNION ALL
                            SELECT 1) b)
Insert into second_table
SELECT b.*
FROM   yourtable b
       CROSS JOIN data 

答案 1 :(得分:1)

这可以使用递归CTE正确设置。

有关示例,请参阅http://www.sqlservercentral.com/articles/T-SQL/90955/

这应该让你接近 - 我已经使用了临时表,但你可以重新格式化并将CTE的结果插入TABLE_AMORTISSEMENT。

使用示例贷款数据:

CREATE TABLE #TABLE_PRETS
   (N_CONTRAT INT,
    MONTANT_CREDIT DECIMAL (18,8),
    TAUX_ACTUEL DECIMAL (18,8),
    TAUX_ASSURANCE DECIMAL (18,8),
    TVA DECIMAL (18,8),
    DATE_DEBUT DATE,
    MENSUALITE DECIMAL (18,8),
    DUREE INT);

INSERT INTO #TABLE_PRETS VALUES (400083, 300000, 5.92997, 0.0036, 0.1, '2016-01-01', 4510.66, 84);


;WITH LOANS AS
(
    SELECT 
        loanID              =   P.N_CONTRAT
    ,   remainingBalance    =   P.MONTANT_CREDIT
    ,   interestRate        =   P.TAUX_ACTUEL / 100
    ,   insuranceRate       =   P.TAUX_ASSURANCE
    ,   taxRate             =   P.TVA
    ,   startDate           =   P.DATE_DEBUT
    ,   monthlyRate         =   ((P.TAUX_ACTUEL / 100) * (1 + (P.TVA)) + P.TAUX_ASSURANCE) / 12
    ,   monthlypayment      =   P.MENSUALITE
    ,   durationmonths      =   P.DUREE
    --, origstartingbalance =   ROUND(P.MONTANT_CREDIT,2)
    FROM #TABLE_PRETS P
),
X AS (
SELECT
    loanID              =   L.loanID
,   monthnum            =   1
,   startingBalance     =   CONVERT(DECIMAL(18,8),ROUND(L.remainingBalance,2))
,   principal           =   ROUND(L.monthlypayment - ROUND(L.remainingBalance,2)  * L.monthlyRate, 2)
,   interestPaymentBT   =   CONVERT(DECIMAL(18,8),ROUND(ROUND(L.remainingBalance,2)  * L.interestRate / 12, 2))
,   taxOnInterest       =   ROUND(ROUND(ROUND(L.remainingBalance,2)  * L.interestRate / 12, 2) * L.taxRate, 2)
,   insurancePayment    =   CONVERT(DECIMAL(18,8),ROUND(ROUND(L.remainingBalance,2)  * L.insuranceRate, 2))
,   remainingBalance    =   ROUND(ROUND(L.remainingBalance,2)  - ROUND(L.monthlypayment - ROUND(L.remainingBalance,2) * L.monthlyRate, 2), 2)
FROM LOANS L
UNION ALL
SELECT 
    loanID              =   X.loanID
,   monthnum            =   X.monthnum + 1
,   startingBalance     =   CONVERT(DECIMAL(18,8),ROUND(X.remainingBalance,2) )
,   principal           =   ROUND(L.monthlypayment - ROUND(X.remainingBalance,2)  * L.monthlyRate, 2)
,   interestPaymentBT   =   CONVERT(DECIMAL(18,8),ROUND(ROUND(X.remainingBalance,2)  * L.interestRate / 12, 2))
,   taxOnInterest       =   ROUND(ROUND(ROUND(X.remainingBalance,2)  * L.interestRate / 12, 2) * L.taxRate, 2)
,   insurancePayment    =   CONVERT(DECIMAL(18,8),ROUND(ROUND(X.remainingBalance,2)  * L.insuranceRate, 2))
,   remainingBalance    =   ROUND(ROUND(X.remainingBalance,2)  - ROUND(L.monthlypayment - ROUND(X.remainingBalance,2) * L.monthlyRate, 2), 2)
FROM X
INNER JOIN LOANS L ON X.loanID = L.loanID
WHERE X.monthnum < L.durationmonths
)
SELECT * FROM X