Crosstab query NOT using pivot

  • HI,

    Here is the new test data in the correct format.

    INSERT INTO [TblAppointments]

    ([ClientID],[ApptDate],[ApptTime],[StaffID],[Duration])

    VALUES

    (2528,'10/01/2016','10:00',22011063, 30),

    (2528,'11/01/2016','10:00',22011063, 30),

    (2528,'12/01/2016','10:00',22011063, 30),

    (2528,'13/01/2016','10:00',22011063, 30),

    (2528,'14/01/2016','10:00',22011063, 30),

    (2528,'15/01/2016','10:00',22011063, 30),

    (2528,'16/01/2016','10:00',22011063, 30),

    (2528,'17/01/2016','10:00',22011063, 30),

    (2528,'10/01/2016','15:00',22011063, 45),

    (2528,'12/01/2016','15:00',22011063, 45),

    (2528,'13/01/2016','15:00',22011063, 45),

    (2528,'14/01/2016','15:00',22011063, 45),

    (2528,'15/01/2016','15:00',22011063, 45),

    (2528,'16/01/2016','15:00',22011063, 45),

    (2528,'17/01/2016','15:00',22011063, 45),

    (2528,'10/01/2016','10:00',22016486, 30),

    (2528,'11/01/2016','10:00',22016486, 30),

    (2528,'12/01/2016','10:00',22016486, 30),

    (2528,'13/01/2016','10:00',22016486, 30),

    (2528,'14/01/2016','10:00',22016486, 30),

    (2528,'16/01/2016','10:00',22016486, 30),

    (2528,'17/01/2016','10:00',22016486, 30)

  • Tallboy (1/7/2016)


    Hi Folks;

    Jeffs code ran brilliantly until changed my test data and added two appointment rows for the same client with same date and times data but with different StaffID's.

    The result is the MAX get the highest StaffID I think! and ignores the the other row, whereas I want the crosstab to show 2 rows for each date and time but different StaffID's in the middle.

    New test data is here...

    2528,'10/01/2016','10:00',22011063, 30

    2528,'11/01/2016','10:00',22011063, 30,

    2528,'12/01/2016','10:00',22011063, 30

    2528,'13/01/2016','10:00',22011063, 30

    2528,'14/01/2016','10:00',22011063, 30

    2528,'15/01/2016','10:00',22011063, 30

    2528,'16/01/2016','10:00',22011063, 30

    2528,'17/01/2016','10:00',22011063, 30

    2528,'10/01/2016','15:00',22011063, 45

    2528,'11/01/2016','15:00',22011063, 45

    2528,'12/01/2016','15:00',22011063, 45

    2528,'13/01/2016','15:00',22011063, 45

    2528,'14/01/2016','15:00',22011063, 45

    2528,'15/01/2016','15:00',22011063, 45

    2528,'16/01/2016','15:00',22011063, 45

    2528,'17/01/2016','15:00',22011063, 45

    2528,'10/01/2016','10:00',22016486, 30

    2528,'11/01/2016','10:00',22016486, 30

    2528,'12/01/2016','10:00',22016486, 30

    2528,'13/01/2016','10:00',22016486, 30

    2528,'14/01/2016','10:00',22016486, 30

    2528,'15/01/2016','10:00',22016486, 30

    2528,'16/01/2016','10:00',22016486, 30

    2528,'17/01/2016','10:00',22016486, 30

    add another column to your view

    ROW_NUMBER() OVER (partition BY ClientID, ApptDate, ApptTime

    ORDER BY StaffID) AS rn

    edit the proc to read as

    +'

    FROM VwAppointments1 appt

    WHERE appt.ApptDate >= @pSDate

    AND appt.ApptDate < DATEADD(dd,1,@pEDate)

    AND appt.ClientID = @pClientID

    GROUP BY appt.ApptTime, appt.rn

    ORDER BY appt.ApptTime, appt.rn;

    '

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Exactly. Thanks for the early morning cover, Graham.

    --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)

  • Thanks guys looks good..

    Tried and appears to Work Brilliant !!!!:-D:-D:-D:-D

  • Tallboy (1/8/2016)


    Thanks guys looks good..

    Tried and appears to Work Brilliant !!!!:-D:-D:-D:-D

    Thanks for the feedback. To finish this off, do you understand it all well enough to be able to maintain it if the need should arise?

    --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)

Viewing 5 posts - 16 through 19 (of 19 total)

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