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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:26:59+00:00 2026-05-31T14:26:59+00:00

Basically I want to duplicate a row a variable number of times. I have

  • 0

Basically I want to duplicate a row a variable number of times.
I have a table with the following structure:

CREATE TABLE [dbo].[Start](
[ID] [int] NOT NULL,
[Apt] [int] NOT NULL,
[Cost] [int] NOT NULL)

I want to duplicate each row in this table (Apt-1) times so in the end there will be #Apt rows. Moreover for each new row the value of Cost is decremented until it reaches 0. ID will be the same as there are no primary keys. If I have a record like this:

1    5    3

I need 4 new rows inserted in the same table and they should look like this

1    5    2
1    5    1
1    5    0
1    5    0

I have tried so far a lot of ways but I cannot make it work. Many thanks!

  • 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-31T14:27:01+00:00Added an answer on May 31, 2026 at 2:27 pm

    try this

    DECLARE  @Start TABLE ( 
    [ID] [int] NOT NULL, 
    [Apt] [int] NOT NULL, 
    [Cost] [int] NOT NULL) 
    
    INSERT @Start (ID, Apt, Cost)
    VALUES  (1, 5, 3)
    
    
    
    ; WITH CTE_DIGS AS (
        SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rn
        FROM master.sys.all_columns AS a
    )
    INSERT @Start (ID, Apt, Cost)
    SELECT ID, Apt, CASE WHEN Cost - rn < 0 THEN 0 ELSE Cost - rn END
    FROM @Start
    INNER JOIN CTE_DIGS
        ON Apt > rn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
i have a table that somehow got duplicated. i basically want to delete all
Basically I do not want to be penalized by search engine for duplicate contents.
Basically, I want to achieve the same thing as ON DUPLICATE KEY in MySQL.
So I have a couple SQL commands that I basically want to make a
I have the following row in excel as an example: 610 323 0726 /
Possible Duplicate: Execute another jar in a java program Basically I want to run
Possible Duplicate: How do I overload the square-bracket operator in C#? Basically I want
I have a data.frame df and I want that every row in this df
Possible Duplicate: Android, QR code library NOT ZXING I want to create an app

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.