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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:35:37+00:00 2026-05-24T23:35:37+00:00

I have a table like: ID NAME VAL ———————- 1 a1*a1 90052 2 a1*a2

  • 0

I have a table like:

ID   NAME     VAL
----------------------
1   a1*a1   90052
2   a1*a2   236
3   a1*a3   56
4   a1*a4   6072
5   a1*a5   1004
6   a2*a2   4576
7   a2*a3   724
8   a2*a4   230
9   a2*a5   679
10  a3*a3   5
11  a3*a4   644
12  a3*a5   23423
13  a4*a4   42354
14  a4*a5   10199
15  a5*a5   10279

Given a number given S = 5, I want to query
the rows wth id: 1,6,10,13,15
they are a1*a1,a2*a2,a3*a3,a4*a4 and a5*a5

I would like something like:

INSERT #NEW_TABLE (ID,NAME,Value) (
SELECT ordinal, NAME, VAL FROM myTable where id = 1,6,10,13,15)

to get

  ID   NAME     VAL
    ----------------------
    1   a1*a1   90052
    2   a2*a2   4576
    3   a3*a3   5
    4   a4*a4   42354
    5   a5*a5   10279

Is there a way to do this for any given S, Maybe wth dynamic sql?

I was getting the formula and I got this:

S=5

ID     formula
1      1
6      1+S
10     1+S+ (S-1)
13     1+S+ (S-1) + (S-2)
15     1+S+ (S-1) + (S-2) + (S-3)

Is there a way to do this inside a case or a while loop?

  • 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-24T23:35:37+00:00Added an answer on May 24, 2026 at 11:35 pm

    This worked in testing.

    You can just inner join on @Tab to limit your results. You probably also want to add some traps for values below 3, which I haven’t done.

    The basic process is

    • Declare your @s value
    • Insert the first two rows since they will always be the same
    • In a loop, insert one row at a time with an incrementing difference
    • Loop exits once it has run @s-2 times

    Try:

    DECLARE @Tab Table (id INT)
    
    DECLARE @S int = 5,
            @ct int
    
    DECLARE @cur int = (1 + @S)
    
    INSERT INTO @Tab SELECT 1
    INSERT INTO @Tab SELECT (1 + @S)
    
    SET @ct = 1
    
    WHILE @ct <= @S - 2
    BEGIN
    
        SET @cur = @cur + (@S - @ct)
    
        INSERT INTO @Tab SELECT @cur
    
        SET @ct = @ct + 1
    
    END
    
    SELECT * FROM @Tab
    ORDER BY id
    

    To use this in your query, you can do either:

    SELECT ordinal, NAME, VAL 
    FROM myTable 
    WHERE id IN (SELECT id FROM @Tab)
    
    -- OR
    
    SELECT ordinal, NAME, VAL 
    FROM myTable t
    INNER JOIN @tab t2
        ON t2.id = t.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: name code group john 12 smith 15 how
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
I have table with 50 entries (users with such details like Name Surname Location
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a table like this (simplified): ID | Name | Parent --------------------------------- 1
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have a table like so <table> <tr id=trRow1 runat=server style=display: none> <td>First Name:</td>
I have a transaction table like so id , name , code , flag

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.