• OK, I think you need to be more thorough. You are trying to continually build on something that doesn't work, modifying a bit, but not modifying and understanding in an organized fashion. Really, this is where testing helps, but also you need to proceed methodically.

    Don't use "same" bug as that doesn't necessarily help. You want to specifically note issues. I'd suggest you organize, and don't post until you are sure you've got this:

    1. DDL for the source table(s). Get this organized and when you paste this into the edit box, make sure you have clicked the "Code=sql" item to the left. Format the code.

    2. Sample data. Get a set of specific sample data and use insert statements. There have been some in this thread, but get a consistent set to talk about a problem.

    3. Give the query code. Again, formatted, but what code are you running. If you use code from a person here, such as the item in your post a few back, give it a comment (--query 4 to pivot data). That way we can talk about it and understand what code is being referenced.

    4. Use a test. Here's an example. Changed the insert into #expected to be the results you need. This test will call your proc, and compare the results returned with what is in the #expected table:

    Once you have an issue, you can sort out which columns/rows don't match.

    Proceed logically and you can solve this.

    EXEC tSQLt.NewTestClass

    @ClassName = N'SingleRow';

    GO

    CREATE PROCEDURE SingleRow.[test get data from single row with one employee]

    AS

    BEGIN

    EXEC tSQLt.FakeTable

    'ShiftScheduler';

    INSERT INTO ShiftScheduler

    VALUES

    ( 635, '2014-05-03', 2, 1 )

    , ( 635, '2014-11-22', 2, 1 )

    , ( 635, '2015-10-28', 1, 1 );

    EXEC tSQLt.FakeTable

    @TableName = N'emploee';

    INSERT INTO emploee

    VALUES

    ( 635, 1, 1 );

    CREATE TABLE #Expected

    (

    Empid INT

    , d1shift INT

    , d1shifttype INT

    , d2shift INT

    , d2shifttype INT

    , d3shift INT

    , d3shifttype INT

    );

    INSERT #Expected

    VALUES

    ( 2424, 2, 4, 2, 4, 2, 4 );

    SELECT

    *

    INTO

    #actual

    FROM

    #Expected AS e

    WHERE

    1 = 0;

    -- act

    INSERT #actual

    EXEC Emp_Shift1;

    -- assert

    EXEC tSQLt.AssertEqualsTable

    @Expected = N'#expected'

    , @Actual = N'#actual'

    , @FailMsg = N'The emp_shift1 proc does not work';

    END;

    GO