I am trying to insert records in the RateIncrease table for all subscriptionid.
I need to insert the subscriptionid only one time. my source table (Transactions) have many transactions for one subscriptionID. I only need to grab the first payment after 7/31/05, and insert it into the RateIncrease table. If the subscriptionid is in the RateIncrease Table, I don't want to insert another record for that subscriptionid.
The problem I have is that the second pass, it seems to be getting the same subscriptionid and trying to insert it again. How can I prevent the subscriptionid from the rateincrease table from getting selected again.
select
trn.SubscriptionID
, TranDate = min(trn.TranDate)
, CreateDate = min(trn.CreateDate)
into #FirstPayment
FROM Transactions trn, RateIncrease ri
where trn.paymentamount > 0 and
trn.trandate > '07/25/2005' and
trn.paymentRateCode in
( 'DSI'
, 'SMI'
, 'DSO'
, 'MonthDSI'
, 'SMO'
, 'MonthSMI'
, 'MonthDSO'
, 'MonthSUI'
, 'MonthSMO'
, 'MonthSUO') and
ri.Subscriptionid <> trn.SubscriptionID
group by trn.subscriptionID
select * from #FirstPayment
order by subscriptionid
insert into RateIncrease
SELECT
trn.SubscriptionID
, trn.PaymentRateCode
, trn.Paymentamount
, trn.TranDate
, pay.CreateDate
FROM Transactions trn
inner join #FirstPayment pay ON
pay.SubscriptionID = trn.SubscriptionID and
pay.CreateDate = trn.CreateDate
left outer join RateIncrease ri ON
trn.SubscriptionID = ri.SubscriptionID and
ri.CreateDate = pay.CreateDate
where
trn.paymentRateCode in
( 'DSI'
, 'SMI'
, 'DSO'
, 'MonthDSI'
, 'SMO'
, 'MonthSMI'
, 'MonthDSO'
, 'MonthSUI'
, 'MonthSMO'
, 'MonthSUO') AND
ri.subscriptionid is null
Drop table #FirstPayment
GO
I need to insert the subscriptionid only one time. my source table (Transactions) have many transactions for one subscriptionID. I only need to grab the first payment after 7/31/05, and insert it into the RateIncrease table. If the subscriptionid is in the RateIncrease Table, I don't want to insert another record for that subscriptionid.
The problem I have is that the second pass, it seems to be getting the same subscriptionid and trying to insert it again. How can I prevent the subscriptionid from the rateincrease table from getting selected again.
select
trn.SubscriptionID
, TranDate = min(trn.TranDate)
, CreateDate = min(trn.CreateDate)
into #FirstPayment
FROM Transactions trn, RateIncrease ri
where trn.paymentamount > 0 and
trn.trandate > '07/25/2005' and
trn.paymentRateCode in
( 'DSI'
, 'SMI'
, 'DSO'
, 'MonthDSI'
, 'SMO'
, 'MonthSMI'
, 'MonthDSO'
, 'MonthSUI'
, 'MonthSMO'
, 'MonthSUO') and
ri.Subscriptionid <> trn.SubscriptionID
group by trn.subscriptionID
select * from #FirstPayment
order by subscriptionid
insert into RateIncrease
SELECT
trn.SubscriptionID
, trn.PaymentRateCode
, trn.Paymentamount
, trn.TranDate
, pay.CreateDate
FROM Transactions trn
inner join #FirstPayment pay ON
pay.SubscriptionID = trn.SubscriptionID and
pay.CreateDate = trn.CreateDate
left outer join RateIncrease ri ON
trn.SubscriptionID = ri.SubscriptionID and
ri.CreateDate = pay.CreateDate
where
trn.paymentRateCode in
( 'DSI'
, 'SMI'
, 'DSO'
, 'MonthDSI'
, 'SMO'
, 'MonthSMI'
, 'MonthDSO'
, 'MonthSUI'
, 'MonthSMO'
, 'MonthSUO') AND
ri.subscriptionid is null
Drop table #FirstPayment
GO