主要问题是在Start Balance = 0
之后选择行。
这里是我的代码:在具有特定值的行之后选择数据
Select distinct
st.date as [Date Incurred],
ss.brkr_id as [Broker ID],
ss.period_id as [Period ID],
case when ss.debt_typ = 'Starting Balance' then sum(st.money_amt) else 0 end as [Starting Balance]
case when ss.debt_typ = 'Balance' then sum(st.money_amt) else 0 end as [Balance]
case when ss.debt_typ = 'End Balance' then sum(st.money_amt) else 0 end as [End Balance]
into #temp1
from debt_t st
inner join debt_info ss
on ss.period_id = st.period_id
where brkr_id = '754234'
and st.money_amt >0.00
group by
st.date as [Date Incurred],
ss.brkr_id as [Broker ID],
ss.period_id as [Period ID]
select Date_Incurred,
Broker_ID,
Period_ID
max(Starting_Balance) as [Start Balance]
max(Balance) as [Current Balance]
max(End Balance) as [End Balance]
from #Temp1
group by
Date_Incurred,
Broker_ID,
Period_ID
此带回这个结果集,它使我更接近我想要什么,但不太我想表明什么。这显示了此经纪商的所有历史记录,我只想显示最新开始余额为0.00时的信息。因此,一旦支付了例如0.00的结束债务,我不想看到这一点。我只想看到公开的债务。从0.00的最新开始余额到今天。
Date_Incurred BrokerID Period_ID Start Balance Balance Ending Debt Amount
12/31/2015 754234 1 200 300 500
1/15/2016 754234 2 500 0 500
1/31/2016 754234 3 500 -500 0
8/31/2016 754234 4 0 1200 1200
9/15/2016 754234 5 1200 120 1320
9/30/2016 754234 6 1320 0 1320
10/15/2016 754234 7 1320 0 1320
10/31/2016 754234 8 1320 0 1320
11/15/2016 754234 9 1320 -320 1000
11/30/2016 754234 10 1000 1500 2500
12/15/2016 754234 11 2500 0 500
12/31/2016 754234 12 2500 500 3000
所以下面是结果集,我期待返回最新的起始余额0.00到今天之间的所有信息。
Date_Incurred BrokerID Period_ID Start Balance Balance Ending Debt Amount
8/31/2016 754234 4 0 1200 1200
9/15/2016 754234 5 1200 120 1320
9/30/2016 754234 6 1320 0 1320
10/15/2016 754234 7 1320 0 1320
10/31/2016 754234 8 1320 0 1320
11/15/2016 754234 9 1320 -320 1000
11/30/2016 754234 10 1000 1500 2500
12/15/2016 754234 11 2500 0 500
12/31/2016 754234 12 2500 500 3000
起始余额是否可以多次为零? –
是的,它可以比0.00多一次,我只想从最新的0.00中看到。 –