0

我正在编写一个具有以下要求的 sql 脚本:- 1) 它应该能够从 Asset_Transaction 表中获取每条记录,并为每条资产计算每条记录上的“运行总计”的单位数。通过汇总 Transaction_datetime 小于或等于所显示 Asset_id 的 Transaction_datetime 的所有 Asset_Transactions 的 Units 来计算此运行总计列。我已经做到了,我认为它工作正常。

但是我的第二个要求是:-

2) 计算该日期的单位价值,即从 Unit_Price 表中获取每个资产交易日期和该特定资产的销售单位价格,并将其乘以添加的单位数量(“运行总计”)。我在第 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

========================= 非常感谢。

4

0 回答 0