I have two tables: (1) tblTransaction (2) tblForMatchingtblTransaction select tbltransaction.*from ( select 1 tID, '2007-08-02' as transdate, 'OWNER-001' as ownercode, 'C-001' as customercode, 1000 as amount, null as matchID union all select 2 tID, '2007-08-02' as transdate, 'OWNER-001' as ownercode, 'C-001' as customercode, 1000 as amount, null as matchID union all select 3 tID, '2007-08-02' as transdate, 'OWNER-002' as ownercode, 'C-002' as customercode, 2000 as amount, null as matchID union all select 4 tID, '2007-08-02' as transdate, 'OWNER-003' as ownercode, 'C-003' as customercode, 500 as amount, null as matchID union all select 5 tID, '2007-08-02' as transdate, 'OWNER-004' as ownercode, 'C-004' as customercode, 1500 as amount, null as matchID union all select 6 tID, '2007-08-02' as transdate, 'OWNER-005' as ownercode, 'C-005' as customercode, 2500 as amount, null as matchID ) as tblTransaction
tblForMatchingselect tblForMatching.* from ( select 1 as matchID, '2007-08-02 11:17:24.967' as transdate, 'C-001' as customercode, 1000 as amount union all -- match select 2 as matchID, '2007-08-02 11:18:21.967' as transdate, 'C-001' as customercode, 1000 as amount union all -- match select 3 as matchID, '2007-08-02 11:18:11.967' as transdate, 'C-001' as customercode, 1000 as amount union all -- no match select 4 as matchID, '2007-08-02 11:19:22.967' as transdate, 'C-001' as customercode, 1000 as amount union all -- no match select 5 as matchID, '2007-08-02 11:20:12.967' as transdate, 'C-002' as customercode, 2000 as amount union all -- match select 6 as matchID, '2007-08-02 11:21:14.967' as transdate, 'C-004' as customercode, 1500 as amount union all -- match select 7 as matchID, '2007-08-02 12:22:15.967' as transdate, 'C-001' as customercode, 1000 as amount union all -- no match select 8 as matchID, '2007-08-02 12:23:34.967' as transdate, 'C-005' as customercode, 2500 as amount union all -- match select 9 as matchID, '2007-08-02 12:23:38.967' as transdate, 'C-006' as customercode, 2500 as amount -- no match ) as tblForMatching
What I want is to do a matching from tblForMatching to tblTransaction by their exact customercode and amount on each transdate. Incase of multiple customercode and amount on each transdate in tblForMatching, matching takes place depending on the occurrences of customercode and amount also in the tblTransaction. Matching occurs on first come basis, field tID on tblTransaction and transdate on tblForMatching.Please refer an example on the customercode = 'C-001' and amount = 1000.Desired result should be like this...select '2007-08-02' as transdate, 1 tID, 'OWNER-001' as ownercode, 'C-001' as customercode, 1000 as amount, 1 as matchID, '2007-08-02 11:17:24.967' as transdate union allselect '2007-08-02' as transdate, 2 tID, 'OWNER-001' as ownercode, 'C-001' as customercode, 1000 as amount, 2 as matchID, '2007-08-02 11:18:21.967' as transdate union allselect '2007-08-02' as transdate, 3 tID, 'OWNER-002' as ownercode, 'C-002' as customercode, 2000 as amount, 5 as matchID, '2007-08-02 11:20:12.967' as transdate union allselect '2007-08-02' as transdate, 4 tID, 'OWNER-003' as ownercode, 'C-003' as customercode, 500 as amount, null as matchID, null as transdate union allselect '2007-08-02' as transdate, 5 tID, 'OWNER-004' as ownercode, 'C-004' as customercode, 1500 as amount, 6 as matchID, '2007-08-02 11:21:14.967' as transdate union allselect '2007-08-02' as transdate, 6 tID, 'OWNER-005' as ownercode, 'C-005' as customercode, 2500 as amount, 8 as matchID, '2007-08-02 12:23:34.967' as transdate union allselect '2007-08-02' as transdate, null as tID, null as ownercode, 'C-001' as customercode, 1000 as amount, 3 as matchID, '2007-08-02 11:18:11.967' as transdate union allselect '2007-08-02' as transdate, null as tID, null as ownercode, 'C-001' as customercode, 1000 as amount, 4 as matchID, '2007-08-02 11:19:22.967' as transdate union allselect '2007-08-02' as transdate, null as tID, null as ownercode, 'C-001' as customercode, 1000 as amount, 7 as matchID, '2007-08-02 12:22:15.967' as transdate union allselect '2007-08-02' as transdate, null as tID, null as ownercode, 'C-006' as customercode, 2500 as amount, 9 as matchID, '2007-08-02 12:23:38.967' as transdate
Any help will be greatly appreciated.Want Philippines to become 1st World COuntry? Go for World War 3...