Help with a SQL Query

  • CREATE TABLE [dbo].[Shift](

    [EmployeeID] [varchar](50) NULL,

    [Shift] [varchar](50) NULL,

    [Month] [int] NULL,

    [Year] [int] NULL,

    [Day1] [bit] NULL,

    [Day2] [bit] NULL,

    [Day3] [bit] NULL

    )

    INSERT INTO Shift ([EmployeeID], [Shift], [Month], [Year], [Day1], [Day2], [Day3])

    VALUES

    ('NV01', 'A1', 5, 2015, 1, 0, 1),

    ('NV01', 'A2', 5, 2015, 0, 1, 0),

    ('NV02', 'A1', 4, 2015, 1, 1, 1)

    Shift Table:

    EmployeeID-----Shift-----Month----Year-----D1-----D2------D3

    NV01---------------A1-------5---------2015----True----False---True

    NV01---------------A2-------5---------2015----False---True----False

    NV02---------------A1-------4---------2015----True----True----True

    How to OUT PUT:

    EmployeeID----Shift-------Date------------Value

    NV01--------------A1------2015-05-01--------True

    NV01--------------A1------2015-05-02--------False

    NV01--------------A1------2015-05-03--------True

    NV02--------------A1------2015-04-01--------True

    NV02--------------A1------2015-04-02--------True

    NV02--------------A1------2015-04-03--------True

    NV01--------------A2------2015-05-01--------False

    NV01--------------A2------2015-05-02--------True

    NV01--------------A2------2015-05-03--------False

    Thanks

  • th02b0 (5/19/2015)


    IN PUT:

    EmployeeID-----Shift-----Month----Year-----D1-----D2------D3

    NV01---------------A1-------5---------2015----True----False---True

    NV01---------------A2-------5---------2015----False---True----False

    NV02---------------A1-------4---------2015----True----True----True

    OUT PUT:

    EmployeeID----Shift-------Date------------Value

    NV01--------------A1------2015-05-01--------True

    NV01--------------A1------2015-05-02--------False

    NV01--------------A1------2015-05-03--------True

    NV02--------------A1------2015-04-01--------True

    NV02--------------A1------2015-04-02--------True

    NV02--------------A1------2015-04-03--------True

    NV01--------------A2------2015-05-01--------False

    NV01--------------A2------2015-05-02--------True

    NV01--------------A2------2015-05-03--------False

    Thanks

    This should be straightforward enough to do, but since you did not supply DDL (sample table) and some consumable INSERTs of your data into that sample data, I'll just make a couple of suggestions.

    Overall, the query is a basic un-pivot operation, with the wrinkle that you need to construct the date on each un-pivoted row. An Alternative (Better?) Method to UNPIVOT[/url].

    Now take a look at the results from the following to see how easy it is to fabricate the correct date from your month and year (although I'm a little unclear on how you get days 1-3 from the month).

    DECLARE @Month INT = 5, @Year INT = 2015;

    SELECT DATEADD(month, @Month-1, DATEADD(year, @Year-1900, 0));


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 2 posts - 1 through 1 (of 1 total)

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