Solved

Row Insert

Posted on 2014-09-09
4
179 Views
Last Modified: 2014-09-09
I have the below temp table:

CREATE TABLE #transtmp
      (qty numeric(28,3),wmslocationid nvarchar(20),letter char(1),number int,locationavail nvarchar(20),
      boxweight numeric(28,3),boxes numeric(28))
      
INSERT into #transtmp (qty,wmslocationid,letter,number,locationavail,boxweight,boxes)
values
(1940.000,'FLOOR-A-10','A',10,'FLOOR-A-10',1940.000,1),
(5820.000,'FLOOR-B-17','B',17,'FLOOR-B-17',1940.000,3),
(1940.000,'FLOOR-C-7','C',7,'FLOOR-C-7',1940.000,1),
(3880.000,'FLOOR-B-17','B',17,'FLOOR-B-17',1940.000,2)

Current Result:
qty                  wmslocationid   letter  number    location avail      box weight    boxes
1940.000      FLOOR-A-10            A      10                 FLOOR-A-10      1940.000      1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3
1940.000      FLOOR-C-7            C      7                 FLOOR-C-7              1940.000      1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2

I need help in looping through the result and insert the number of rows from the boxes result.
Expected Result:
qty                  wmslocationid   letter  number    location avail      box weight    boxes   counter
1940.000      FLOOR-A-10            A      10                 FLOOR-A-10      1940.000      1              1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              1
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              2
5820.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      3              3
1940.000      FLOOR-C-7            C      7                 FLOOR-C-7              1940.000      1              1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2              1
3880.000      FLOOR-B-17            B      17                 FLOOR-B-17      1940.000      2              2
0
Comment
Question by:Kristie
  • 2
4 Comments
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40312129
May I ask what's the logic for that?
I mean, what for you need to replicate rows?
0
 

Author Comment

by:Kristie
ID: 40312369
Victor, I am replicating row with different counter so I can use that to block off inventory space in an SSRS report.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40312412
This is a perfect use for a tally / sequential numbers table.

Code below will generate 1M numbers (that can be increased if somehow that's needed), then use that to expand the boxes.

;WITH
cteTally10 AS (
    SELECT 0 AS tally UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
),
cteTally100 AS (
    SELECT 1 AS tally
    FROM cteTally10 c1
    CROSS JOIN cteTally10 c2
),
cteTally10K AS (
    SELECT 1 AS tally
    FROM cteTally100 c1
    CROSS JOIN cteTally100 c2
),
cteTally1Mil AS (
    SELECT ROW_NUMBER() OVER(ORDER BY c1.tally) AS tally
    FROM cteTally100 c1
    CROSS JOIN cteTally10K c2
)
SELECT tran.qty, tran.wmslocationid, tran.letter, tran.number, tran.location, tran.avail, tran.[box weight], tran.boxes, t.tally AS counter
FROM #transtmp tran
INNER JOIN cteTally1Mil t ON
    t.tally BETWEEN 1 AND tran.boxes
0
 

Author Comment

by:Kristie
ID: 40312696
Works perfect, Thanks Scott.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now