一百个账户各有100$,某个账户某天如有支出则添加一条新记录,记录其余额。一百天后,请输出每天所有账户的余额信息
这个问题的难点在于每个用户在某天可能有多条纪录,也可能一条纪录也没有(不包括第一天)
返回的记录集是一个100天*100个用户的纪录集
下面是我的思路:

TABLE [dbo].[Account](
	[id] [int] NULL,
	[Name] [nvarchar](50) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Trade](
	[id] [int] NULL,
	[AccountId] [int] NULL,
	[sj] [datetime] NULL,
	[ye] [money] NULL
) ON [PRIMARY]

GO

 

 

 

int
set @index=1

while(@index<101)
begin
insert Account
select @index,'Name'+cast (@index as nvarchar(3))
set @index=@index+1
end

select * from Account


--生成天的时间,利用条Account


select identity(int,1,1) as Id into ##t  from Account

--假设开始时间是一个具体的日期/01/01
declare @startTime date
set @startTime = '2001/01/01 23:59:59'
select DateAdd(day,id, @startTime) as [datetime] into ##SDate from ##t



--求时间和帐号的组合情况
select Name,[datetime]
,ye=(select isnull(min(ye),100) from Trade t where t.AccountId= a.Id and t.sj<=s.[datetime])
from Account a,##SDate s

 

 

 

 

 

、、

相关文章:

  • 2021-07-19
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
  • 2022-02-15
猜你喜欢
  • 2021-06-17
  • 2021-09-14
  • 2021-08-19
  • 2021-05-23
  • 2021-12-19
  • 2022-12-23
  • 2021-06-15
相关资源
相似解决方案