Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select records from table one which are not match

Status
Not open for further replies.

ancheswaroop

Programmer
Apr 25, 2006
12
US
Table 1: TRAN_ADJUSTMENT_REC
Table 2: DLY_TRAN_FILE

I am matching this two tables with this Query

select count(1)
from
TEDCLZ.TRAN_ADJUSTMENT_REC
INNER JOIN
TEDCLZ.DLY_TRAN_FILE
on
cast((REPLACE(substr(cast(date(cast(substr(TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_DATE,5,3) as int))as char(10)),6,5)||''||
substr(TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_DATE,1,4),'-','')) as char(10)) = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_DATE
AND cast((REPLACE(substr(cast(date(cast(substr(TEDCLZ.DLY_TRAN_FILE.AT31_DATE_POSTED,5,3) as int))as char(10)),6,5)||''||
substr(TEDCLZ.DLY_TRAN_FILE.AT31_DATE_POSTED,1,4),'-','')) as char(10)) = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE
AND substr(TEDCLZ.DLY_TRAN_FILE.AT31_AMT_TRANSACTION,2,14) = substr(TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_AMOUNT,4,14)
AND TEDCLZ.DLY_TRAN_FILE.AT31_REFERENCE_NUMBER = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_REF_NUM
AND TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_CODE = TEDCLZ.TRAN_ADJUSTMENT_REC.TRAN_CODE
AND TEDCLZ.DLY_TRAN_FILE.HASH_SRC_CRCD_I = TEDCLZ.TRAN_ADJUSTMENT_REC.HASH_SRC_CRCD_I
WHERE TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE='01112006'

No. of records in TRAN_ADJUSTMENT_REC( Table 1) is 13179
No. of records match are 13036
143 records are not matching

Now i want that 143 records from table 1 which are not matching to table 2
When i am using this query i am not getting any records ( query is running no error message)

select
TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_DATE,
TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE,
TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE,
TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_REF_NUM,
TEDCLZ.TRAN_ADJUSTMENT_REC.TRAN_CODE,
TEDCLZ.TRAN_ADJUSTMENT_REC.TCAT_CODE,
TEDCLZ.TRAN_ADJUSTMENT_REC.HASH_SRC_CRCD_I
from
TEDCLZ.TRAN_ADJUSTMENT_REC
left outer join
TEDCLZ.DLY_TRAN_FILE
on
cast((REPLACE(substr(cast(date(cast(substr(TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_DATE,5,3) as int))as char(10)),6,5)||''||
substr(TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_DATE,1,4),'-','')) as char(10)) = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_DATE
AND cast((REPLACE(substr(cast(date(cast(substr(TEDCLZ.DLY_TRAN_FILE.AT31_DATE_POSTED,5,3) as int))as char(10)),6,5)||''||
substr(TEDCLZ.DLY_TRAN_FILE.AT31_DATE_POSTED,1,4),'-','')) as char(10)) = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE
AND substr(TEDCLZ.DLY_TRAN_FILE.AT31_AMT_TRANSACTION,2,14) = substr(TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_AMOUNT,4,14)
AND TEDCLZ.DLY_TRAN_FILE.AT31_REFERENCE_NUMBER = TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_REF_NUM
AND TEDCLZ.DLY_TRAN_FILE.AT31_TRANSACTION_CODE = TEDCLZ.TRAN_ADJUSTMENT_REC.TRAN_CODE
AND TEDCLZ.DLY_TRAN_FILE.HASH_SRC_CRCD_I = TEDCLZ.TRAN_ADJUSTMENT_REC.HASH_SRC_CRCD_I
WHERE TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_POST_DATE='01112006'
AND TEDCLZ.TRAN_ADJUSTMENT_REC.TRANSACTION_AMOUNT IS NULL

is this query is correct.(left outer join)
 
Nope, this will not work. If you check on null as you do then you will not select non-existing records in the outer-join set. You better build a construction using subselect and 'not in'...

Ties Blom

 
Not had time to look at the post in detail. Just thought it's worth mentioning there is an except operator to find results in one result set, which aren't present in another.

Additionally, there is am intersection operator, which finds the elements tha belong to two different result sets.

select gid from table1
intersect
select gid from table2

select gid from table1
intersect
select gid from table2

Cheers
Greg
 
Greg,

I looked through my DB2 documentation, but could not find any examples of the except operator. Can you give an example or link to an example of this operator?

- Dan
 
Dan,

I have a typo in the above. I should have shown in my second example the word except, rather than intersect.

Some examples from the web




Google for

db2 except intersect

you'll get lots of examples.

Cheers
Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top