Possible Duplicate:
SQL Query JOIN with Table
If this is the data in TestingTable1
BUYER_ID | ITEM_ID | CREATED_TIME
----------+-----------------+----------------------
1345653 151851771618 2012-07-09 19:57:33
1345653 110909316904 2012-07-09 21:29:06
1345653 221065796761 2012-07-09 19:31:48
And if this is the below data in TestingTable2
USER_ID | PRODUCT_ID | LAST_TIME
---------+----------------+-----------------------
1345653 150851771618 2012-07-09 19:57:33
1345653 110909316904 2012-07-09 22:29:06
1345653 221165796761 2012-07-09 12:31:48
I need to compare TestingTable2 with TestingTable1 on BUYER_ID and USER_ID. I need to see, if BUYER_ID and USER_ID gets matched then I need to compare ITEM_ID with PRODUCT_ID and CREATED_TIME with LAST_TIME and if there is a mismatch in TestingTable2 after comparing with TestingTable1 in either one of them or both of them, then I need to show the result.
So if you look at the above example- I have three scenarios basically
- Firstly- In
TestingTable1, in the First rowITEM_IDis not matching withPRODUCT_IDin the First row ofTestingTable2butCREATED_TIMEis matching withLAST_TIMEfor the first row in both the tables - Secondly- In
TestingTable1, in the Second rowCREATED_TIMEis not matching withLAST_TIMEin the second row ofTestingTable2butITEM_IDis matching withPRODUCT_IDfor the second row in both the tables - Thirdly- In
TestingTable1, in the Third rowITEM_IDis not matching withPRODUCT_IDand alsoCREATED_TIMEis not matching withLAST_TIME, so in the third row BOTH of them does not match withTestingTable1third row.
So these are three case that I need to cover while comparing TestingTable2 with TestingTable1 always. And TestingTable1 is the MAIN table through which comparisons need to be made always, so it means data in TestingTable1 is always accurate.
So I need to show the result like this considering the above example if not matching either one of them or both of them- TestingTable1 data then next to it same TestingTable2 data, so that I can see what value was there in TestingTable1 as compared to TestingTable2
BUYER_ID | ITEM_ID | CREATED_TIME | USER_ID | PRODUCT_ID | LAST_TIME
-----------+-----------------+---------------------------+----------------+--------------------+-----------------------
1345653 151851771618 2012-07-09 19:57:33 1345653 150851771618 2012-07-09 19:57:33
1345653 110909316904 2012-07-09 21:29:06 1345653 110909316904 2012-07-09 22:29:06
1345653 221065796761 2012-07-09 19:31:48 1345653 221165796761 2012-07-09 12:31:48
So I wrote a query, I thought it will cover all my three scenarios, but Only it covered First Two not the Third One. And I am confuse whether we can achieve this third scenario or not?
SELECT *
FROM(
SELECT *
FROM TestingTable1 A
JOIN TestingTable2 B ON A.BUYER_ID = B.USER_ID AND B.LAST_TIME = A.Created_TIME
WHERE B.PRODUCTID <> A.ITEM_ID
UNION ALL
SELECT *
FROM TestingTable1 A
INNER JOIN TestingTable2 B ON A.BUYER_ID = B.USER_ID AND B.PRODUCTID = A.ITEM_ID
WHERE B.t1time <> A.Created_TIME
) X
Any suggestions will be appreciated.
Update:-
Just a quick update what I was initially thinking to do. As I was aware of few problems with my third scenario.
First of all in TestingTable1, I am sorting(ORDER BY) the table by BUYER_ID and CREATED_TIME and same with TestingTable2 I am sorting with USER_ID and LAST_TIME and I am doing comparison by making sure data belongs to BUYER_ID and USER_ID on a given day.