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

  • SEARCH
  • Home
  • 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 8828735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:40:31+00:00 2026-06-14T07:40:31+00:00

I have two tables like this: Date Client Amount | table x 123 abc

  • 0

I have two tables like this:

Date    Client  Amount | table x
123     abc     123
456     abc     987
234     xyz     567


Date    Client  Amount | table y
123     abc     234
921     lka     981
234     xyz     123

In my query, I want to display:

123     abc     123     | (from x)
123     abc     234     | (from y)
234     xyz     567     | x
234     xyz     123     | y

Leaving out all records without a “partner.” Is this possible in TSQL?

  • 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-14T07:40:33+00:00Added an answer on June 14, 2026 at 7:40 am
        select DISTINCT
               case when c='x' then x.date else y.date end date,
               case when c='x' then x.client else y.client end client,
               case when c='x' then x.amount else y.amount end amount,
               c from_
          from tablex x
          join tabley y on x.date=y.date and x.client=y.client
    cross join (select 'x' union all select 'y') z(c)
      order by date, client, from_;
    

    An alternative to the accepted solution.

    Note that where you have multiple matches from either/both tables, all the results from ‘x’ will be listed before the matches from ‘y’. e.g.

    create table tablex (Date int, Client char(3), Amount int);
    insert tablex select
        123     ,'abc',     123 union all select
        456     ,'abc',     987 union all select
        123     ,'abc',     919 union all select
        234     ,'xyz',     567;
    create table tabley (Date int, Client char(3), Amount int);
    insert tabley select
        123     ,'abc',     234 union all select
        123     ,'abc',     867 union all select
        921     ,'lka',     981 union all select
        234     ,'xyz',     123;
    
    **Results**
    
    date        client amount      from_
    ----------- ------ ----------- -----
    123         abc    123         x
    123         abc    919         x
    123         abc    234         y
    123         abc    867         y
    234         xyz    567         x
    234         xyz    123         y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables structured like this: table a id title date 1 testing1
I have two tables which looks something like this Table Queue int ID; string
I have two tables associated by FK. Table student is mapped like this: @Entity
I have two tables Department and Employee . Department table looks like this: ID
I have two tables that look like this: Table: cases id name status case_no
I have two tables like this Table Product ProductId - int <PK> ProductExpiry -
I have two tables which look like this: T1: T1ID | Date | Time
I have two tables in my database that look like this CREATE TABLE IF
I have two tables that look like this: CREATE TABLE table1 (user_id int, the_date
I have two tables like this FLUX_ISU_EQU ID NUMBER(38,0) EQU_CODE VARCHAR2(10 BYTE) DATE_DERNIERE_MODIF DATE

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.