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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:15:12+00:00 2026-06-12T22:15:12+00:00

This is a continuation of my previous question What is the efficient way to

  • 0

This is a continuation of my previous question What is the efficient way to generate combination of items in SQL Server? Let me explain the real scenario as what I am looking for and why….

Suppose I have a table as under

Declare @t table(Number Int)
Insert Into @t Values(10),(20),(30),(40),(18)

Number
10
20
30
40
18

and I need to look for a number say 35 (or closest).

Declare @NumberToLookfor = 35

Now the search will happen based on the weight of combination for two pairs. Let me explain.

10+20 = 30

10+30 = 40

10+40 = 50

10+18 = 28

20 + 30 = 50

20 + 40 = 60

20 + 18 = 38

30 + 40 = 70

30 + 18 = 48

40 + 18 = 58

so we can figure out that the weight of any two numbers are teh candidates here e.g. (10,20), (10,30)…(40,18)

Once we get that, the first 3 closest pairs will be (20,18), (10,20) , (10,30) in this case. Because teh dirtance between 35 and 38 (20+18) is 3 whichle it is 5 for the other pairs (10,20) , (10,30).

I think that the explanation is clear to understand what I am looking for.(If not please let me know)

What is the most efficient way of doing so?

My attempt

Declare @t table(Number Int)
Insert Into @t Values(10),(20),(30),(40),(18)

;WITH Cte1 (Number,Ids,TotalWeight) AS 
( 
    SELECT  Number           
            , ',' + CAST(Number AS VARCHAR(MAX)) 
            ,CAST(Number AS INT) 
    FROM @t 
    UNION ALL 
    SELECT  p.Number 
            ,c.Ids + ',' +  CAST(p.Number AS VARCHAR(MAX)) 
            ,CAST(c.TotalWeight + p.Number AS INT)            
    FROM @t AS p JOIN Cte1 c ON p.Number < c.Number
),Cte2 AS( 
    SELECT          
        *
        ,DENSE_RANK() OVER(ORDER BY ABS(TotalWeight - 35)) [rank] 
    FROM Cte1 
    WHERE (LEN(Ids) - LEN(REPLACE(Ids, ',', '')))/LEN(',') = 2 
)

select *
from Cte2 where [rank] <= 2

It works.

enter image description here

But if the values grows very big say more than 50 or so, then it becomes very very in efficient. Because in the first CTE , I am finding out the full permutation and in the second Cte choosing those values where only two elements participated.

so when the value grows big, the first Cte behaves very very slowly.

Is there any other way of doing so even for big tables.

DDL provided

Declare @t table(Number Int)
Insert Into @t Values  
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20), 
(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31),(32),(33),(34),(35),(36),(37),(38),(39),(40), 
(41),(42),(43),(44),(45),(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60), 
(61),(62),(63),(64),(65),(66),(67),(68),(69),(70),(71),(72),(73),(74),(75),(76),(77),(78),(79),(80), 
(81),(82),(83),(84),(85),(86),(87),(88),(89),(90),(91),(92),(93),(94),(95),(96),(97),(98),(99),(100) 

Many thanks in advance

  • 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-12T22:15:13+00:00Added an answer on June 12, 2026 at 10:15 pm

    To generically solve for pairs, triplets etc using the brute force method for very small sets, this may work. Update the number in the two indicated locations.

    Declare @t table(Number Int)
    Insert Into @t Values(10),(20),(30),(40),(18)
    
    ;WITH Cte1 (Counter,Number,Ids,TotalWeight) AS 
    ( 
        SELECT  1,Number           
                , ',' + CAST(Number AS VARCHAR(MAX)) 
                ,CAST(Number AS INT) 
        FROM @t 
        UNION ALL 
        SELECT  c.Counter+1,p.Number 
                ,c.Ids + ',' +  CAST(p.Number AS VARCHAR(MAX)) 
                ,CAST(c.TotalWeight + p.Number AS INT)            
        FROM @t AS p JOIN Cte1 c ON p.Number < c.Number
        WHERE c.Counter < 2   --<<** we need only up to 2 numbers
    ),Cte2 AS( 
        SELECT          
            *
            ,DENSE_RANK() OVER(ORDER BY ABS(TotalWeight - 35)) [rank] 
        FROM Cte1 
        WHERE Counter = 2   --<<** use only the pairs
    )
    
    select *
    from Cte2 where [rank] <= 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a continuation of my previous question is there off-the-shelf convenient way to
This question is continuation to my previous question over stack overflow how-to-download-images-asynchronously-from-web-server . I
This is a continuation of my previous question .NET regex engine returns no matches
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This is continuation of my previous question Android: Music Player gets started itself after
This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't
This question is in continuation to my previous question, in which I asked about
This is a continuation from a previous stackoverflow question. I've renamed some variables so

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.