2017-01-09 58 views
0

主要问题是在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

起始余额是否可以多次为零? –

+0

是的,它可以比0.00多一次,我只想从最新的0.00中看到。 –

回答

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 

您可以选择大于该行的行。

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