Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2009-12-07 : 04:04:43
|
| HiI am struggling to flag matching debits to credits. A debit could potentially match multiple credits (based on specific fields matching), but if a debit matches a credit based on amount and Acc, then that credit cannot match another debit.i.e.Debits:Debit_SRN Debit Acc1000 500 21001 500 21002 500 2Crebit_SRN Crebit Acc2000 500 22001 500 22002 500 2Now, I don't particular which exact Credit that the Debit matches to, but a Credit_ID cannot be repeated for the same Debit ID; therefore it should look something like this:Debit_SRN Debit Acc Match to Credit_SRN1000 500 2 20021001 500 2 20001002 500 2 2001Does this need to be in a loop? as I have 4 million trans to run this over...Please can you help.Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-12-07 : 04:09:55
|
[code]select *from( select Debit_SRN, Debit, Acc, row_no = row_number() over (partition by Acc, Debit order by Debit_SRN) from Debits) dinner join( select Credit_SRN, Credit, Acc, row_no = row_number() over (partition by Acc, Credit order by Credit_SRN) from Credits) c on d.Acc = c.Acc and d.Debit = c.Credit and d.row_no = c.row_no[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-07 : 10:32:35
|
| TRY this too..create table #sample (Xvalue int,YValue int, ZValue int,progran# int)insert into #sample select 300, 400, 500, 1 union allselect 300, 600, 200, 2union allselect 400, 400, 100, 3union allselect 100 ,200 ,500, 4union allselect 200 ,600, 200, 5select * from #sampleselect Xvalue,YValue,ZValue from(select row_number() over( order by Xvalue)as r_no,Xvalue from #sample group by Xvalue )x full outer join(select row_number() over( order by YValue)as r_no,YValue from #sample group by YValue)y on x.r_no =y.r_no full outer join(selectSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-08 : 01:51:30
|
quote: Originally posted by senthil_nagore TRY this too..create table #sample (Xvalue int,YValue int, ZValue int,progran# int)insert into #sample select 300, 400, 500, 1 union allselect 300, 600, 200, 2union allselect 400, 400, 100, 3union allselect 100 ,200 ,500, 4union allselect 200 ,600, 200, 5select * from #sampleselect Xvalue,YValue,ZValue from(select row_number() over( order by Xvalue)as r_no,Xvalue from #sample group by Xvalue )x full outer join(select row_number() over( order by YValue)as r_no,YValue from #sample group by YValue)y on x.r_no =y.r_no full outer join(selectSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
The code is incompleteMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|