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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:15:46+00:00 2026-05-23T12:15:46+00:00

I have to match each record in TABLE1 with at most 1 record in

  • 0

I have to match each record in TABLE1 with at most 1 record in TABLE2.
There is a better way to match (CODE equality) and a poor one (in case of no CODE equality, let’s sort by CODE and match by index).

Let’s suppose as a first approximation that the code doing this might look like this :

SELECT
TABLE1.CODE AS CODE1,
TABLE2.CODE AS CODE2
FROM 
(SELECT ROW_NUMBER() OVER(ORDER BY CODE) INDEX, CODE FROM TABLE1) T1
LEFT JOIN
(SELECT ROW_NUMBER() OVER(ORDER BY CODE) INDEX, CODE FROM TABLE2) T2
ON
(T1.CODE=T2.CODE) --CODE equality
OR
(T1.INDEX=T2.INDEX) --CODE equality

Let’s consider these tables :

 TABLE1   TABLE2
+------+ +------+
| CODE | | CODE |
+------+ +------+
| AAA  | | BBB  |
| BBB  | | CCC  |
| CCC  | | DDD  |
+------+ +------+

The result will be :

CODE1 CODE2
----- -----
AAA   BBB    -> matched because of INDEX equality
BBB   BBB    -> matched because of CODE equality
BBB   CCC    -> matched because of INDEX equality
CCC   CCC    -> matched because of CODE equality
CCC   DDD    -> matched because of INDEX equality

Here comes the difficulty : I’d like to express the notion that despite there are 2 conditions of matching which are not mutually exclusive, the first one must be preferred to the second if possible, and the second must be evaluated if and only if the first one fails.

The wanted result being :

CODE1 CODE2
----- -----
AAA   DDD    -> matched because of INDEX equality between the cast-off records not able to match better
                (corrected from the previous version where AAA was said to match expectedly with BBB)
BBB   BBB    -> matched thanks to CODE equality, no need to match on INDEX
CCC   CCC    -> matched thanks to CODE equality, no need to match on INDEX

And of course I would preferably obtain this behavior all-in-one-query to avoid a several-steps script, given that :

  • you can feel free to propose a totally different query : the one above was just there to illustrate the general idea, but it’s clear it doesn’t fit the needs. So, no need to try to preserve its structure.

  • I don’t really care about performance compared to the wish to perform an all-in-one-query matching. If subqueries are needed, let’s go ! 😉

Eager to read your suggestions ! 🙂

EDIT :

I made a strong error in my OP which is now corrected and deeply changes what can be considered as an accurate answer. The expected results were not right. My most humble apologies. 🙁

The idea is : match as much as you can on CODE equality, then consider only those which are left behind by this first matching algorithm to match them by INDEX. That’s why AAA which was incorrectly expected to INDEX-match with BBB (which already CODE-matches with another BBB), must in fact INDEX-match with another non CODE-matching item, in that case DDD.

  • 1 1 Answer
  • 3 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-05-23T12:15:46+00:00Added an answer on May 23, 2026 at 12:15 pm

    Ok, I got it.
    I post the answer in case it could be of any interest for somebody out there.
    It can be adjusted depending on the final needs but the idea is there (I slightly changed the names of the fields/constants : it’s more close to the real life names and helped me to find the solution, it would have been too theorical otherwise).

    declare @uc table(cod_uc varchar(3))
    declare @cnt table(cod_cnt varchar(3))
    
    insert into @uc values('a')
    insert into @uc values('b')
    insert into @uc values('c')
    insert into @cnt values('b')
    insert into @cnt values('c')
    insert into @cnt values('d')
    insert into @cnt values('e')
    
    ;with codematchings(cod_uc,cod_cnt)
    as
    (a
        select uc.cod_uc, cnt.cod_cnt
        from 
        @uc uc full outer join @cnt cnt on uc.cod_uc=cnt.cod_cnt
    ),
    orders(cod_uc,order_uc,cod_cnt,order_cnt)
    as
    (
        select cod_uc,row_number() over(order by isnull(cod_uc,'zzzz')) order_uc, cod_cnt,row_number() over(order by isnull(cod_cnt,'zzzz')) order_cnt from codematchings where cod_uc is null or cod_cnt is null
    )
    select codematchings.*,
    case
    when codematchings.cod_uc is null then orders2.cod_uc
    when codematchings.cod_cnt is null then orders2.cod_cnt
    else codematchings.cod_uc
    end matched_with
    from codematchings
    left outer join orders orders1
    on codematchings.cod_uc is null and orders1.cod_cnt=codematchings.cod_cnt or codematchings.cod_cnt is null and orders1.cod_uc=codematchings.cod_uc
    left outer join orders orders2
    on codematchings.cod_uc is null and orders2.order_uc=orders1.order_cnt or codematchings.cod_cnt is null and orders2.order_cnt=orders1.order_uc
    

    Result :

    cod_uc cod_cnt matched_with
    ------ ------- ------------
    A      NULL    D
    B      B       B
    C      C       C
    NULL   D       A
    NULL   E       NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
I have two datatables and I need to match zipcodes from each of the
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
So I have my table users and each record has a username field and
i have a query as Record Source for a Form, for each row in
Let's say I have a MySQL table, people . Each record comprises a variety
I have to match two hashtable, but its keys does not match. So i
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
Is this Regex correct if I have to match a string which is atleast
guys i have arrays in which i have to match this kind of text

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.