Rows to Columns

  • Can somebody please help? Is there a fairly simple way to do this?

    This is my data now.

    LABORLC

    152685A B C D E

    152969A B

    This is what I would like it to look like.

    LABORLC

    152685A

    152685B

    152685C

    152685D

    152685E

    152969A

    152969B

  • erouse (9/16/2016)


    Can somebody please help? Is there a fairly simple way to do this?

    This is my data now.

    LABORLC

    152685A B C D E

    152969A B

    This is what I would like it to look like.

    LABORLC

    152685A

    152685B

    152685C

    152685D

    152685E

    152969A

    152969B

    It is not very clear what you have going on here. Are the values delimited with a space? If so then you need to look in my signature about splitting strings. If it is something else then you need to look at the first link in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • erouse (9/16/2016)


    Can somebody please help? Is there a fairly simple way to do this?

    This is my data now.

    LABORLC

    152685A B C D E

    152969A B

    This is what I would like it to look like.

    LABORLC

    152685A

    152685B

    152685C

    152685D

    152685E

    152969A

    152969B

    We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/16/2016)


    erouse (9/16/2016)


    Can somebody please help? Is there a fairly simple way to do this?

    This is my data now.

    LABORLC

    152685A B C D E

    152969A B

    This is what I would like it to look like.

    LABORLC

    152685A

    152685B

    152685C

    152685D

    152685E

    152969A

    152969B

    We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.

    Sorry, here is code to generate 2 tables. I am trying to take the data from the first table (LC are grouped by LABOR and in same row) and make a new table with LC in separate row with LABOR number repeated as displayed in the second table. I cant provide what I have so far as I have no idea where to start on this!

    --===== If the test table already exists, drop it

    IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL

    DROP TABLE #mytable

    --===== Create the test table with

    CREATE TABLE #mytable

    (

    LABOR VARCHAR(50),

    LC VARCHAR(50)

    )

    --===== Insert the test data into the test table

    INSERT INTO #mytable

    (LABOR, LC)

    SELECT '152736', 'A C D' UNION ALL

    SELECT '152756', 'A B C D E'

    --===== View the data

    SELECT LABOR, LC FROM #mytable

    --===== If the 2nd test table already exists, drop it

    IF OBJECT_ID('TempDB..#mytable2','U') IS NOT NULL

    DROP TABLE #mytable2

    --===== Create the test table with

    CREATE TABLE #mytable2

    (

    LABOR VARCHAR(50),

    LC VARCHAR(50)

    )

    --===== Insert the test data into the test table

    INSERT INTO #mytable2

    (LABOR, LC)

    SELECT '152736', 'A' UNION ALL

    SELECT '152736', 'C' UNION ALL

    SELECT '152736', 'D' UNION ALL

    SELECT '152756', 'A' UNION ALL

    SELECT '152756', 'B' UNION ALL

    SELECT '152756', 'C' UNION ALL

    SELECT '152756', 'D' UNION ALL

    SELECT '152756', 'E'

    --===== View the data

    SELECT LABOR, LC FROM #mytable2

  • erouse (9/21/2016)


    Jeff Moden (9/16/2016)


    erouse (9/16/2016)


    Can somebody please help? Is there a fairly simple way to do this?

    This is my data now.

    LABORLC

    152685A B C D E

    152969A B

    This is what I would like it to look like.

    LABORLC

    152685A

    152685B

    152685C

    152685D

    152685E

    152969A

    152969B

    We can help... a lot! But we need a little more, please. Please see the first link under "helpful Links" in my signature line below.

    Sorry, here is code to generate 2 tables. I am trying to take the data from the first table (LC are grouped by LABOR and in same row) and make a new table with LC in separate row with LABOR number repeated as displayed in the second table. I cant provide what I have so far as I have no idea where to start on this!

    --===== If the test table already exists, drop it

    IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL

    DROP TABLE #mytable

    --===== Create the test table with

    CREATE TABLE #mytable

    (

    LABOR VARCHAR(50),

    LC VARCHAR(50)

    )

    --===== Insert the test data into the test table

    INSERT INTO #mytable

    (LABOR, LC)

    SELECT '152736', 'A C D' UNION ALL

    SELECT '152756', 'A B C D E'

    --===== View the data

    SELECT LABOR, LC FROM #mytable

    --===== If the 2nd test table already exists, drop it

    IF OBJECT_ID('TempDB..#mytable2','U') IS NOT NULL

    DROP TABLE #mytable2

    --===== Create the test table with

    CREATE TABLE #mytable2

    (

    LABOR VARCHAR(50),

    LC VARCHAR(50)

    )

    --===== Insert the test data into the test table

    INSERT INTO #mytable2

    (LABOR, LC)

    SELECT '152736', 'A' UNION ALL

    SELECT '152736', 'C' UNION ALL

    SELECT '152736', 'D' UNION ALL

    SELECT '152756', 'A' UNION ALL

    SELECT '152756', 'B' UNION ALL

    SELECT '152756', 'C' UNION ALL

    SELECT '152756', 'D' UNION ALL

    SELECT '152756', 'E'

    --===== View the data

    SELECT LABOR, LC FROM #mytable2

    Well done on the sample data! All you need is a copy of the house splitter[/url] and this is as easy as pie.

    SELECT LABOR, Item

    FROM #mytable

    CROSS APPLY dbo.DelimitedSplit8K(LC,' ');

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Thanks! You guys are the best!

  • This was removed by the editor as SPAM

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply