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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:26:57+00:00 2026-05-21T22:26:57+00:00

I don’t really know what to call this but it’s not that hard to

  • 0

I don’t really know what to call this but it’s not that hard to explain

Basically what I have is a result like this

Similarity ColumnA   ColumnB   ColumnC
1          SomeValue NULL      SomeValue
2          NULL      SomeB     NULL
3          SomeValue NULL      SomeC
4          SomeA     NULL      NULL

This result is created by matching a set of strings against another table. Each string also contains some values for these ColumnA..C which are the values I wan’t to aggregate in some way.

Something like min/max works very well but I can’t figure out how to get it to account for the highest similarity not just the min/max value. I don’t really want the min/max, I want the first non-null value with the highest similarity.

Ideally the result would look like this

ColumnA   ColumnB   ColumnC
SomeA     SomeB     SomeC

I’d like be able to efficiently join in the temporary result to compute the rest and I’ve been exploring different options. Something which I’ve been considering is creating a SQL Server CLR aggregate the yields the “first” non-null value but I’m unsure if there’s even such a thing as a first or last when running an aggregate on a result.

  • 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-05-21T22:26:58+00:00Added an answer on May 21, 2026 at 10:26 pm

    Okay, so I figured it out, I originally had trouble with the UPDATE FROM and JOIN not playing well together. I was counting on that the UPDATE would just occur multiple times and that would give me the correct results, however, there’s no such guarantee from SQL Server (it’s actually undefined behavior and alltough it appeared to work we’ll have none of that) but since you can run UPDATE against a CTE I combined that with the OUTER APPLY to select the exactly 1 row to complement a missing value if possible.

    Here’s the whole thing with test data as well.

    DECLARE @cost TABLE (
        make nvarchar(100) not null,
        model nvarchar(100),
        a numeric(18,2),
        b numeric(18,2)
    );
    
    INSERT @cost VALUES ('a%', null, 100, 2);
    INSERT @cost VALUES ('a%', 'a%', 149, null);
    INSERT @cost VALUES ('a%', 'ab', 349, null);
    INSERT @cost VALUES ('b', null, null, 2.5);
    INSERT @cost VALUES ('b', 'b%', 249, null);
    INSERT @cost VALUES ('b', 'b', null, 3);
    
    DECLARE @unit TABLE (
        id int,
        make nvarchar(100) not null,
        model nvarchar(100)
    );
    
    INSERT @unit VALUES (1, 'a', null);
    INSERT @unit VALUES (2, 'a', 'a');
    INSERT @unit VALUES (3, 'a', 'ab');
    INSERT @unit VALUES (4, 'b', null);
    INSERT @unit VALUES (5, 'b', 'b');
    
    DECLARE @tmp TABLE (
        id int,
        specificity int,
        a numeric(18,2),
        b numeric(18,2),
        primary key(id, specificity)
    );
    
    INSERT @tmp 
    OUTPUT inserted.* --FOR DEBUGGING
    SELECT 
        unit.id
        , ROW_NUMBER() OVER (
            PARTITION BY unit.id 
            ORDER BY cost.make DESC, cost.model DESC
        ) AS specificity
        , cost.a
        , cost.b
    FROM @unit unit
    INNER JOIN @cost cost ON unit.make LIKE cost.make
        AND (cost.model IS NULL OR unit.model LIKE cost.model)
    ;
    
    --fix the holes
    WITH tmp AS (
        SELECT * 
        FROM @tmp 
        WHERE specificity = 1 
            AND (a IS NULL OR b IS NULL) --where necessary
    )
    UPDATE tmp
    SET 
        tmp.a = COALESCE(tmp.a, a.a)
        , tmp.b = COALESCE(tmp.b, b.b)
    OUTPUT inserted.* --FOR DEBUGGING
    FROM tmp
    OUTER APPLY ( 
        SELECT TOP 1 a 
        FROM @tmp a 
        WHERE a.id = tmp.id 
            AND a.specificity > 1 
            AND a.a IS NOT NULL 
        ORDER BY a.specificity
        ) a
    OUTER APPLY ( 
        SELECT TOP 1 b 
        FROM @tmp b 
        WHERE b.id = tmp.id 
            AND b.specificity > 1 
            AND b.b IS NOT NULL 
        ORDER BY b.specificity
        ) b
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this question is trivial or not. But after a couple
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user
Don't really know how to formulate the title, but it should be pretty obvious
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know how to explain it better but i'm trying to get a response

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.