主要问题是在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
这会带回这个结果集,让我更接近我想要的但不完全是我想表达的。这显示了此Broker的所有历史记录我只想显示最新Start Balance为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 :(得分:0)
除非我错误地阅读您的问题,否则您应该只需添加一个WHERE
条款,该条款限制所发生的日期大于或等于起始余额显示为零的日期。请注意,在下面的查询中,我在子查询中使用TOP 1
作为预防措施,如果多个记录的起始余额为零。在这种情况下,您可能需要为我们提供额外的逻辑来处理它。
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
WHERE Date_Incurred >= (SELECT TOP 1 Date_Incurred FROM #Temp1
GROUP BY Date_Incurred, Broker_ID, Period_ID
HAVING MAX(Starting_Balance) = 0
ORDER BY Date_Incurred DESC)
GROUP BY Date_Incurred,
Broker_ID,
Period_ID
答案 1 :(得分:0)
我的建议是在临时表中添加属于行号的列
您可以选择包含Start Balance
等于零的行数,然后选择大于此行的rows
。
-- Define #temp1 tabel
IF OBJECT_ID('tempdb..#temp1') IS NOT NULL
DROP TABLE #temp1
create table #temp1
(
ID int IDENTITY(1,1) PRIMARY KEY,--colum belong to row number
DateIncurred int,
PeriodID int,
StartingBalance varchar (200),
Balance varchar (200),
EndBalance varchar (200)
)
INSERT INTO #temp1 (DateIncurred, PeriodID, StartingBalance, Balance, EndBalance)
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
ROW_NUMBER() OVER(ORDER BY ID ASC) AS [Row number]
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
where max(Starting_Balance) = 0
group by
Date_Incurred,
Broker_ID,
Period_ID
您可以选择大于该行的行。