2

概要:如何编写 SQL 代码以将平面文件中的一条记录插入到要插入到 SQL Server 表中的单独记录中。

细节:我有一张记录租赁付款的表格。对于每份租赁协议,到期日始终是当月的第一天。在租约期间,每个月的表中都有一个条目。为简单起见,假设有一份从 2019 年 9 月 1 日到 2020 年 8 月 31 日的租赁合同。表格如下所示:

+------------------------------------------------------+
| CONTRACT_ID | DUE_DATE_DT | INVOICE_ID | RENTAL_AMT  |
+-------------+-------------+------------+-------------+
| 12345       |    9/1/2019 |            |             |
+-------------+-------------+------------+-------------+
| 12345       |   10/1/2019 |            |             |
+-------------+-------------+------------+-------------+
...
+-------------+-------------+------------+-------------+
| 12345       |    8/1/2020 |            |             |
+-------------+-------------+------------+-------------+

发票通常是一个日历月,但有时可能是两三个月,因为供应商正在追赶。数据以平面文件的形式接收,示例如下所示:

Contract  Rental Period From Date  Rental Period To Date  Invoice Number   Amount
31125      9/1/2019                  9/30/2019             6378            400.00  (standard scenario)
12345      9/1/2019                 11/30/2019             789             150.00  (multi-month scenario)

来自平面文件的数据需要存储在表中的三个单独的记录中:

+------------------------------------------------------+
| CONTRACT_ID | DUE_DATE_DT | INVOICE_ID | RENTAL_AMT  |
+-------------+-------------+------------+-------------+
| 12345       |    9/1/2019 |   789      |   50.00     |
+-------------+-------------+------------+-------------+
| 12345       |   10/1/2019 |   789      |   50.00     |
+-------------+-------------+------------+-------------+
| 12345       |   11/1/2019 |   789      |   50.00     |
+-------------+-------------+------------+-------------+

先感谢您。

4

1 回答 1

2

您可以尝试使用递归 CTE。考虑一下:

CREATE TABLE #RawData(Contract int, Rental_Period_From Date, Rental_Period_To Date, Invoice_Number int, Amount Decimal(10,2))
INSERT INTO #RawData VALUES(31225, '9/1/2019','9/30/2019', 6378,400.00)
INSERT INTO #RawData VALUES(12345, '9/1/2019','11/30/2019', 789,150.00)


;WITH CTE
AS
(
select Contract,Rental_Period_From DueDate,Invoice_Number,Amount, 0 MonthCounter, Rental_Period_To from #RawData
UNION ALL
SELECT Contract, DATEADD(month,MonthCounter+1,DueDate),Invoice_Number,Amount, MonthCounter,Rental_Period_To   from CTE
WHERE DATEADD(month,MonthCounter+1,DueDate) <= Rental_Period_To

)
SELECT CONTRACT, DueDate,Invoice_Number, CAST(Amount / (SELECT COUNT(*) 
                FROM CTE t1 WHERE t1.Contract = t2.Contract ) as decimal(10,2)) Amount 
FROM CTE t2
于 2020-03-13T14:39:00.957 回答