gpt4 book ai didi

SQL:从开始日期到结束日期重复记录

转载 作者:行者123 更新时间:2023-12-04 05:37:14 24 4
gpt4 key购买 nike

我有以下数据结构

Customer | Order | Date       | Amount | TransactionType
AABB | AB01 | 2012-06-01 | 3000 | Invoiced
AABB | AB01 | 2012-06-05 | 3000 | Payment

我需要从提交发票之日到付款之日重复我的数据。像这样;
Customer|Order|Date      | AmountDue|AmountPaid|DatePaid  |TransactionType
AABB |AB01 |2012-06-01| 3000 |NULL |2012-06-05|Invoiced
AABB |AB01 |2012-06-02| 3000 |NULL |2012-06-05|Invoiced
AABB |AB01 |2012-06-03| 3000 |NULL |2012-06-05|Invoiced
AABB |AB01 |2012-06-04| 3000 |NULL |2012-06-05|Invoiced
AABB |AB01 |2012-06-05| 3000 |3000 |2012-06-05|Payment

如何为此场景创建 SQL 脚本

最佳答案

尝试这个:

  create table cust1
(
Customer varchar(20),
Orders varchar(10),
Date datetime,
Amount float,
TransactionType varchar(50)
)
INSERT INTO cust1
VALUES('AABB','AB01','2012-06-01',3000,'Invoiced'),
('AABB','AB01','2012-06-05',3000,'Payment')

DECLARE @stDate datetime,@eddate datetime
select @stDate =MIN(date),@eddate =MAX(date) from cust1

select c1.Customer,c1.Orders,DATEADD(DD,number,@stDate) as [date],
amount amountDue,
CASE WHEN (DATEADD(DD,number,@stDate)) = @eddate then amount else null end as amountPaid,
@eddate as datepaid,
CASE WHEN (DATEADD(DD,number,@stDate)) <> @eddate then 'Invoiced' else 'Payment' end as TransactionType

from master..spt_values p inner join cust1 c1
on right(cast(c1.date as DATE),2) <= (case when p.number = 0 then 1 else p.number end)
where type='p'and DATEADD(DD,number,@stDate) <=@eddate

关于SQL:从开始日期到结束日期重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com