Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:06:22+00:00 2026-06-07T12:06:22+00:00

Possible Duplicate: SQL Query JOIN with Table If this is the data in TestingTable1

  • 0

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

  1. Firstly- In TestingTable1, in the First row ITEM_ID is not matching with PRODUCT_ID in the First row of TestingTable2 but CREATED_TIME is matching with LAST_TIME for the first row in both the tables
  2. Secondly- In TestingTable1, in the Second row CREATED_TIME is not matching with LAST_TIME in the second row of TestingTable2 but ITEM_ID is matching with PRODUCT_ID for the second row in both the tables
  3. Thirdly- In TestingTable1, in the Third row ITEM_ID is not matching with PRODUCT_ID and also CREATED_TIME is not matching with LAST_TIME, so in the third row BOTH of them does not match with TestingTable1 third 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:06:23+00:00Added an answer on June 7, 2026 at 12:06 pm
    with C as
    (
      select *
      from TestingTable1 A
        inner join TestingTable2 B
          on A.BUYER_ID = B.USER_ID and
             B.LAST_TIME = A.Created_TIME and
             B.PRODUCT_ID <> A.ITEM_ID
      union all
      select * 
      from TestingTable1 A
        inner join TestingTable2 B 
          on A.BUYER_ID = B.USER_ID and 
             B.PRODUCT_ID = A.ITEM_ID and
             B.LAST_TIME <> A.CREATED_TIME
    )
    select *
    from C
    union all
    select *
    from TestingTable1 A
      inner join TestingTable2 B
        on A.BUYER_ID = B.USER_ID and
           A.CREATED_TIME <> B.LAST_TIME and
           A.ITEM_ID <> B.PRODUCT_ID
    where not exists (select *
                      from C
                      where A.BUYER_ID = C.BUYER_ID and
                            A.ITEM_ID = C.ITEM_ID and
                            A.CREATED_TIME = C.CREATED_TIME) and
          not exists (select *
                      from C
                      where B.USER_ID = C.USER_ID and
                            B.PRODUCT_ID = C.PRODUCT_ID and
                            B.LAST_TIME = C.LAST_TIME);
    

    SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Query JOIN with Table CREATE EXTERNAL TABLE IF NOT EXISTS TestingTable1
Possible Duplicate: Writing an SQL query to SELECT item from the following table I
Possible Duplicate: SQL query to find Missing sequence numbers I have a table which
Possible Duplicate: Parent Child table record - Building SQL query Here is my table
Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: linq to sql recursive query I got stuck with having to build
Possible Duplicate: What does the colon sign “:” do in a SQL query? Simple
Possible Duplicate: SQL Comments on Create Table on SQL Server 2008 I just want
Possible Duplicate: How do I Create a Comma-Separated List using a SQL Query? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.