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 8564667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:15:02+00:00 2026-06-11T17:15:02+00:00

Edit : People are having a hard time understanding what i want. So here’s

  • 0

Edit: People are having a hard time understanding what i want. So here’s pretty pictures that explains it in excruciating detail.

First join Transactions to Strange:

enter image description here

The results so far

Customer  Invoice  TransactionID  Mass  Length                LeptonNumber
========  =======  =============  ====  ====================  ============
Ian       One      1              Ian   Judgement Spaulders   50
Ian       One      1              Ian   Glorious Breastplate  50
Chris     Two      2              Chris Barenavel             2

Now attempt to join remaining rows with Down:

enter image description here

The results so far

Customer  Invoice         TransactionID  Mass  Length                LeptonNumber
========  =======         =============  ====  ====================  ============
Ian       One             1              Ian   Judgement Spaulders   50
Ian       One             1              Ian   Glorious Breastplate  50
Chris     Two             2              Chris Barenavel             2
Jamie     Krol Blade      3              Jay   Krol Blade            90
Jay       Arcanite Reaper 4              Ian   Arcanite Reaper       90

Finally, join any leftover rows to Charmed:

enter image description here

The results so far

Customer  Invoice         TransactionID  Mass    Length                LeptonNumber
========  =======         =============  ====    ====================  ============
Ian       One             1              Ian     Judgement Spaulders   50
Ian       One             1              Ian     Glorious Breastplate  50
Chris     Two             2              Chris   Barenavel             2
Jamie     Krol Blade      3              Jay     Krol Blade            90
Jay       Arcanite Reaper 4              Ian     Arcanite Reaper       90
Potatoe   Dan Quayle      5              Potatoe Dan Quayle            90

And how look at the rows we have left over:

enter image description here

Giving me my desired results set

Customer  Invoice         TransactionID  Mass    Length                LeptonNumber
========  =======         =============  ====    ====================  ============
Ian       One             1              Ian     Judgement Spaulders   50
Ian       One             1              Ian     Glorious Breastplate  50
Chris     Two             2              Chris   Barenavel             2
Jamie     Krol Blade      3              Jay     Krol Blade            90
Jay       Arcanite Reaper 4              Ian     Arcanite Reaper       90
Potatoe   Dan Quayle      5              Potatoe Dan Quayle            90
Stapler   Alexstraza      6              NULL    NULL                  NULL

i have a main table:

Transactions
+----------+
|          |
|          | 
|          |
|          |
|          |
|          |
|          |
+----------+

i want each row in this table to join only one possible matching table:

Tranasctions        Strange
+----------+      +----------+
| row 1 ===|=====>| row 1    |         Down
| row 2 ===|=====>| row 2    |       +---------+
| row 3 ===|======+----------+======>| row 1   |        Charmed
| row 4 ===|========================>| row 2   |       +---------+
| row 5 ===|=========================+---------+======>| row 1   |
| row 6 ===|==========================================>| row 2   |
+----------+                                           +---------+

Which normally i’d perform as a join of Transactions to the set of Strange || Down || Charmed:

SELECT
   Transactions.*,
   Quarks.Mass,
   Quarks.Length,
   Quarks.LeptonNumber
FROM Transactions
    INNER JOIN NationalSecurityLetters
    ON Transactions.TransactionID = NationalSecurityLetters.ReferenceNumber

    LEFT JOIN (
       SELECT 'Strange' AS Type, * FROM Strange
       UNION ALL
       SELECT 'Down' AS Type, * FROM Down
       UNION ALL
       SELECT 'Charmed' AS Type, * FROM Charmed
    ) Quarks
    ON (
        (Quarks.Type = 'Strange' AND Transactions.Customer = Quarks.Mass)
        OR
        (Quarks.Type = 'Down' AND Transactions.Invoice = Quarks.Length)
        OR
        (Quarks.Type = 'Charmed' AND Transactions.Customer = Quarks.Length)    
    )       

The problem is that i want the join to happen in that preferred order:

  • Strange
  • Down
  • Charmed

It’s entirely possible that a single Transaction can have matching entries in multiple tables. But for each possible JOIN of Transactions to the other tables, i want SQL Server to prefer the Strange table. If there’s no match then go to the Down table. If there’s no match to go the Charmed table.

If you find a match in      Prefer the matching row from
==========================  ============================
Strange                     Strange
Strange and Down            Strange
Strange, Down, and Charmed  Strange
Down                        Down
Down and Charmed            Down
Charmed                     Charmed
(no match?)                 (then there's no match)

i’ve thought about using an OPTION(FORCE ORDER) clause:

SELECT *
FROM Transactions
    INNER JOIN NationalSecurityLetters
    ON Transactions.TransactionID = NationalSecurityLetters.ReferenceNumber

    LEFT JOIN (
       SELECT 'Strange' AS Type, * FROM Strange
       UNION ALL
       SELECT 'Down' AS Type, * FROM Strange
       UNION ALL
       SELECT 'Charmed' AS Type, * FROM Strange
    ) Quarks
    ON (
        (Quarks.Type = 'Strange' AND Transactions.Customer = Quarks.Mass)
        OR
        (Quarks.Type = 'Down' AND Transactions.Invoice = Quarks.Length)
        OR
        (Quarks.Type = 'Charmed' AND Transactions.Customer = Quarks.Length)    
    )       
OPTION (FORCE ORDER)

But i don’t want to force SQL Server to join

  • Transactions ==> NationalSecurityLetters, when it may be more advantageous to join
  • NationalSecurityLetters ==> Transactions

  • 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-11T17:15:03+00:00Added an answer on June 11, 2026 at 5:15 pm

    Maybe this solution will help you:

    SET ANSI_WARNINGS ON;
    GO
    BEGIN TRAN;
    
    CREATE TABLE dbo.TableA (
        TableAID INT PRIMARY KEY,
        DescriptionA VARCHAR(50) NOT NULL
    );
    INSERT dbo.TableA 
    VALUES (1,'A-1'), (2,'A-2');
    
    CREATE TABLE dbo.TableB (
        TableBID INT PRIMARY KEY,
        DescriptionB VARCHAR(50) NOT NULL
    );
    INSERT dbo.TableB
    VALUES (1,'B-1'), (2,'B-2'), (4,'B-4');
    
    CREATE TABLE dbo.TableC (
        TableCID INT PRIMARY KEY,
        DescriptionC VARCHAR(50) NOT NULL
    );
    INSERT dbo.TableC
    VALUES (1,'C-1'),(3,'C-3'), (4,'C-4');
    GO
    
    CREATE TABLE dbo.[Transaction] (
        TransactionID INT IDENTITY PRIMARY KEY,
        TranDate DATE NOT NULL,
        Col1 INT NULL
    );
    INSERT dbo.[Transaction]
    VALUES ('20120101', 1), ('20120202',2), ('20120303',3), ('20120404',4), ('20120505',5);
    GO
    
    SELECT  *
    FROM    dbo.[Transaction] t
    OUTER APPLY (
        SELECT * FROM TableA a WHERE t.Col1=a.TableAID
    ) j1 --first join
    OUTER APPLY (
        SELECT * FROM TableB b WHERE j1.TableAID IS NULL AND t.Col1=b.TableBID --First condition will force the join order (dbo.TableA.TableAID should be NOT NULL)
    ) j2 --second join
    OUTER APPLY (
        SELECT * FROM TableC c WHERE j1.TableAID IS NULL AND j2.TableBID IS NULL AND t.Col1=c.TableCID ---First two conditions will force the join order (dbo.TableA.TableAID & dbo.TableB.TableBID should be NOT NULL)
    ) j3 --third join
    WHERE   j1.TableAID IS NOT NULL
    OR      j2.TableBID IS NOT NULL
    OR      j3.TableCID IS NOT NULL
    
    ROLLBACK;
    

    In this case, the join order is:

    1) t.Col1=a.TableAID

    2) if not 1) then t.Col1=b.TableBID

    3) if not 1) and 2) then t.Col1=c.TableCID

    Results:

    TransactionID TranDate   Col1 TableAID DescriptionA TableBID DescriptionB TableCID DescriptionC
    ------------- ---------- ---- -------- ------------ -------- ------------ -------- ------------
    1             2012-01-01 1    1        A-1          NULL     NULL         NULL     NULL
    2             2012-02-02 2    2        A-2          NULL     NULL         NULL     NULL
    3             2012-03-03 3    NULL     NULL         NULL     NULL         3        C-3
    4             2012-04-04 4    NULL     NULL         4        B-4          NULL     NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this is my first time using this forum. Apparently people can edit my
I am having a hard time getting started with some way to visualize some
I've implemented OpenID on my website but I'm having a hard time finding a
I'm having a hard time setting up a set of related maven projects with
EDIT: For those having issues with mysql data that won't get rid of white
Having worked with Django , I've seen that people tend to reccommend the use
All- I am having a hard time taking the value of an EditText and
EDIT heres the github page if people want to have a look https://github.com/brandongrossutti/EventStore I
Edit Since there were many downvotes and people who didn't understand what I'm asking
I have a simple form where a user can add, edit, and delete people

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.