Sorry, the data is not that clean. Not strictly. What I have is a table with Core data and a table with detail data. I am matching detail to core so that I can move to history. However, there are a few issues that come in to play, one of them being that I may have 3 transactions in the core for the same acct, for the same amount with the same code within the same hour (and I only receive day and hour). Then, in the detail I have three transactions that match that acct, that amount and that code for that same day. (This data is coming from different sources, but all apply to the same acct, so I cannot get anything different that will help with uniqueness). So, if I match on the fields that I know that I can with an inner join, then I get nine matches because #1 in the core will match all three in the detail as will #2 and #3 in the core. So, aside from making the first match, copying the data to history and then deleting the copied child and core transactions (one of each) and then re-running the matching query again, is there a more efficient way to do this? Here is my current stored procedure code:
Code:
BEGIN
DECLARE @CurrCoreId INT, @CurrChildId INT, @transactId INT, @BundleId nvarchar(255)
DECLARE @MainTransactionId INT, @ChildId INT, @RowCount INT
Create table #CoreProcess
(
CoreRawId int
,ChildTransactionRawId int
,TxCode int
,EntityType_ImportFileTypeId int
,BundleId nvarchar(255)
,rnk int
)
INSERT INTO #CoreProcess
SELECT
core.CoreRawId
,child.ChildTransactionRawId
,core.TxCode
,child.EntityType_ImportFileTypeId
,child.BundleId
,DENSE_RANK() OVER (ORDER BY core.CoreRawId) as rnk
FROM dbo.CIB_CoreRaw core
JOIN dbo.[CIB_ChildTransactionRaw] child
ON dbo.fn_DecryptByPassPhrase(core.[BaseAccountNumber]) = dbo.fn_DecryptByPassPhrase([child].[BaseAccountNumber])
AND core.BankISN = child.BankISN
JOIN dbo.CIB_TransactionCode code
ON core.TXCode = code.TxCode
AND child.EntityType_ImportFileTypeId = code.EntityType_ImportFileTypeID
AND code.HasChildData = 1
WHERE (core.Credit = child.Amount OR core.Debit = child.Amount)
AND child.Amount > 0
AND core.BankISN = @BankISN
AND core.PacketDate = CONVERT(DATE, child.TransactionDate)
Declare @i Int,
@max int;
SELECT @i = 1,@max = MAX(rnk)
FROM #CoreProcess
WHILE @i < @max
BEGIN
SELECT @CurrCoreId = CoreRawId
,@CurrChildId = ChildTransactionRawId
,@BundleId = BundleId
FROM #CoreProcess
WHERE rnk = @i;
EXEC @transactId = dbo.CIB_CopyCoreToHistoryById @CurrCoreId,-1 --this deletes the core record after copying it over to history
IF (LEN(@BundleId) < 1)
BEGIN
EXEC dbo.CIB_CopyChildRawToHistoryById @transactId,@CurrChildId --this deletes the child record after copying it over to history
--DELETE FROM #CoreProcess WHERE CoreRawId = @CurrCoreId
END
--if bundle is not null, then need to add those child items as well..
IF (LEN(@BundleId) > 1)
BEGIN
EXEC dbo.CIB_CopyChildRawToHistoryByBundleId @transactId,@BundleId --this deletes the child record after copying it over to history
--DELETE FROM #CoreProcess WHERE BundleId = @BundleId
END
SET @i = @i + 1;
END
DROP TABLE #CoreProcess
END
and a sampling of data illustrating the issue
[pre]
CoreRawId ChildTransactionRawId TxCode EntityType_ImportFileTypeId BundleId rnk
26320 260913 195 117 NULL 17103
26320 260914 195 117 NULL 17103
26321 260916 195 117 NULL 17104
26322 260914 195 117 NULL 17105
26322 260913 195 117 NULL 17105
26323 260919 195 117 NULL 17106
26323 260918 195 117 NULL 17106
26323 260917 195 117 NULL 17106
26323 260920 195 117 NULL 17106
26324 260901 195 117 NULL 17107
26325 260896 195 117 NULL 17108
26326 260897 195 117 NULL 17109
26327 260892 195 117 NULL 17110
26328 260904 195 117 NULL 17111
26329 260900 195 117 NULL 17112
26330 260886 195 117 NULL 17113
26331 260911 195 117 NULL 17114
26332 260909 195 117 NULL 17115
26333 260908 195 117 NULL 17116
26334 260891 195 117 NULL 17117
26335 260893 195 117 NULL 17118
26336 260910 195 117 NULL 17119
26337 260906 195 117 NULL 17120
26338 260905 195 117 NULL 17121
26343 261607 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
26343 261608 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
26343 261609 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
26343 261610 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
26343 261615 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
26343 261616 408 120 3000000178/24/2017d6e6c8a19dc241f487f4ceb201b282fb 17122
[/pre]
you can see in this example that, for instance 26320 matches 260913 and 260914, but 26322 also matches both 260913 and 260914. What I want is for 26320 to take one (let's say 260913) and 26322 to take the other (which would be 2609+14 in this case). I know that I can go head and re-run the query after moving the first matched records each time, which would drop the duplicates, but if I have 35k records in the core and 350k records in the detail, that is more cumbersome than I would like to be...
Thanks for your thoughts,
Willie