【发布时间】:2017-03-07 07:23:09
【问题描述】:
我有一个名为“Patient Ledger Report”的存储过程,我需要在其中显示患者的日常交易详细信息和余额。我从下面的代码中为您提供了一个样本数据,这些数据是如何插入到我的临时代码中的我的 sp 中的表。
create table #Patient_ledger (PATIENT_NAME varchar(250),PATIENT_NBR bigint,BILLNO varchar(250),BILLAMOUNT bigint,
PAID_AMOUNT bigint)
Insert into #Patient_ledger (Patient_name ,Patient_nbr ,billno ,billamount ,
paid_amount )
select 'ABC',1,'DUE_BILL_ABC_1',100,50
union all
select 'ABC',1,'DUE_BILL_ABC_2',160,90
UNION ALL
select 'ABC',1,'DEPOSIT_BILL_ABC',0,60
UNION ALL
select 'XYZ',2,'DEPOSIT_BILL_XYZ',0,70
UNION ALL
select 'XYZ',2,'DUE_BILL_XYZ_1',100,30
SELECT * FROM #Patient_ledger
Drop table #Patient_ledger
我想如何在我的报告中显示数据。
PATIENT_NUMBER BILLNO BILLAMOUNT PAID_AMOUNT BALANCE
1 DUE_BILL_ABC_1 100 50 50 --(100-50)
1 DUE_BILL_ABC_2 160 90 120 --(160-90 +50(Here 50 is prev balance amount of same patient))
1 DEPOSIT_BILL_ABC 0 40 80 ---( 120-40=80)
2 DEPOSIT_BILL_XYZ 0 70 0
2 DUE_BILL_XYZ_1 100 30 0 --Here Balance is zero because patient has deposited some
--amount before bill (70-100+30=0)
Note: Balance amount should deduct when deposits are paid by that particual patient.
【问题讨论】:
-
为什么第 4 行余额为 0 ?应该是 80-70=10
-
是否有订购这些记录的日期或身份?
-
你需要有一个像账单日期这样的列来订购,你有没有这样的列
-
@Matej 在实际的sp数据中有一些不同表的标识列和日期列按数据排序
-
@Matej 它完全符合我的需要-:)
标签: sql-server sql-server-2008 tsql sql-server-2005 sql-server-2014