Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql adding additional rows to each row

Is it possible to add additional rows to a selected set based on a field value?

I have this query:

WITH CTEDivisions AS
(
    SELECT ....
)

SELECT 
    cnt,
    Division
FROM CTEDivisions

Which returns:

  cnt         Division
  1           Sales
  2           Marketing
  1           Business
  2           IT
  etc...

What I need is a statement that returns 3 additional rows when cnt = 2, like:

 Division     NewDivision
 Sales        Sales
 Marketing    Marketing - X
 Marketing    Marketing - Y
 Marketing    Marketing - Z
 Business     Business
 IT           IT - X
 IT           IT - Y
 IT           IT - Z
 etc...

I've searched for ways of doing this, and found some possible solutions using a cursor and WHILE loop, but those don't seem to work in conjunction with the CTE statement.

like image 293
russds Avatar asked Jun 24 '15 04:06

russds


People also ask

Can you add multiple rows at once in SQL?

Answer. Yes, instead of inserting each row in a separate INSERT statement, you can actually insert multiple rows in a single statement. To do this, you can list the values for each row separated by commas, following the VALUES clause of the statement.

How do I insert the same data in multiple rows?

MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause.

How can I insert more than 1000 rows in SQL?

A table can store upto 1000 rows in one insert statement. If a user want to insert multiple rows at a time, the following syntax has to written. If a user wants to insert more than 1000 rows, multiple insert statements, bulk insert or derived table must be used.


2 Answers

Make a table for your extra rows (either as a CTE, or as a permanent table) and left join to it using your condition (cnt=2):

DECLARE @T TABLE(cnt int, Division varchar(100));
INSERT INTO @T(cnt, Division) VALUES
(1, 'Sales'),
(2, 'Marketing'),
(1, 'Business'),
(2, 'IT');

WITH
CTEDivisions
AS
(
    SELECT
        cnt
        ,Division
    FROM @T
)
,CTE_Extra
AS
(
    SELECT ' - X' AS Extra
    UNION ALL
    SELECT ' - Y' AS Extra
    UNION ALL
    SELECT ' - Z' AS Extra
)
SELECT
    cnt
    ,Division
    ,Division + ISNULL(Extra, '') AS NewDivision
FROM
    CTEDivisions
    LEFT JOIN CTE_Extra ON CTEDivisions.cnt = 2
;

result set

cnt    Division    NewDivision
1      Sales       Sales
2      Marketing   Marketing - X
2      Marketing   Marketing - Y
2      Marketing   Marketing - Z
1      Business    Business
2      IT          IT - X
2      IT          IT - Y
2      IT          IT - Z
like image 191
Vladimir Baranov Avatar answered Oct 08 '22 02:10

Vladimir Baranov


WITH CTEDivisions AS
(
    SELECT ....
)

SELECT 
    c.cnt,
    c.Division,
    c.Division + ISNULL(' - ' + o.v, '') AS NewDivision
FROM CTEDivisions c
OUTER APPLY(SELECT v FROM(VALUES('X'),('Y'),('Z'))t(v) WHERE c.cnt = 2)o
like image 26
Giorgi Nakeuri Avatar answered Oct 08 '22 01:10

Giorgi Nakeuri