我正在编写一个具有以下要求的sql脚本: - 1)它应该能够从Asset_Transaction表中获取每条记录,并且每个资产在每条记录上计算“运行总计”的单位数。通过将Transaction_datetime小于或等于所显示的Asset_id的Transaction_datetime的所有Asset_Transactions的单位相加来计算此运行总计列。我已经实现了这一点,我认为它运作正常。
然而我的第二个要求是: -
2)计算该日期的单位价值,即从Unit_Price表中获取sell_unitPrice,了解每个asset_transaction和该特定资产的日期,并将其与已添加的单位数(“运行总计”)相乘。 我在第2步上苦苦挣扎
:---------------------------------------
set nocount on
go
declare mla_exceptions scroll cursor for
select distinct mla.asset_id
from asset_transaction mla
go
Print 'asset_id, Amount, Transaction Name, Total Units, Transaction Datetime'
declare @ml_asset double precision
open mla_exceptions
fetch first mla_exceptions
into @ml_asset
while (@@sqlstatus = 0)
begin
select mla.asset_id , ',',
-- mla.transaction_datetime, ',',
mla.amount, ',',
tt.name, ',',
(select sum (units) from asset_transaction where transaction_datetime <= mla.transaction_datetime and asset_id = @ml_asset and status = 'A' ) 'Running Total Units', ',',
transaction_datetime
from asset_transaction mla noholdlock
Left outer join transaction_type tt on tt.transaction_type_id = mla.transaction_type_id
where mla.asset_id = @ml_asset
order by mla.asset_id
fetch next mla_exceptions
into @ml_asset
end
close mla_exceptions
deallocate cursor mla_exceptions
go
========================= 非常感谢。