2016-05-12 11 views
-4

Bitte helfen Sie und vielen Dank!Ändern Sie SQL-Code und Abfragen in Microsoft SQL Server

Wie bekomme ich dieses Ergebnis?
http://i.stack.imgur.com/EedXW.png

Das ist mein aktuelles Ergebnis:
http://i.stack.imgur.com/Ydzn0.png

Code:

WITH shiftHours AS 
(
    SELECT 
     RowID, 
     y.EMPLOYEENAME AS EMPLOYEENAME, 
     -- flatten the first hour to remove the minutes and get the initial current hour 
     DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) AS currentHour, 
     ShiftA_Start, 
     ShiftA_End, 
     DATEPART(hour, ShiftA_Start) AS hourOrdinal, 
     -- determine how much of the first hour is applicable. if it is minute 0 then the whole hour counts 
     CAST(CASE 
      WHEN DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) = DATEADD(hour, DATEDIFF(hour, 0, ShiftA_End), 0) THEN DATEDIFF(minute, ShiftA_Start, ShiftA_End)/60.0 
      WHEN DATEPART(minute, ShiftA_Start) = 0 THEN 1.0 
      ELSE (60 - DATEPART(minute, ShiftA_Start))/60.0 
     END AS DECIMAL(5,3)) AS hourValue 
    FROM 
     (-- use a ROW_NUMBER() to generate row IDs for the shifts to ensure each row is unique once it gets to the pivot 
     SELECT 
      ROW_NUMBER() OVER(ORDER BY ShiftA_Start, ShiftA_End) AS RowID, 
      EMPLOYEENAME, 
      ShiftA_Start, 
      ShiftA_End 
     FROM 
      (-- this is where the data gets pulled from the source table and where the data types are converted from string to DATETIME 
       SELECT 
        EMPLOYEENAME, 
        CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_Start, 
        CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_end, 
        CAST(CASE 
          WHEN DATEPART (day, [ShiftA_Start]) = DATEPART (day, [SHIFTA_END]) 
           THEN CONVERT(DATETIME, LEFT(SHIFTA_end, 17), 103) 
          WHEN DATEPART (hour, [ShiftA_Start]) = DATEPART (hour, [SHIFTA_END]) 
           THEN CONVERT(DATETIME, LEFT(SHIFTA_end, 17), 103) + '23:59:00.000' END AS VARCHAR(30)) AS S_END 
       FROM 
        [DatabaseName].[dbo].[TMS_PEOPLE] 
       WHERE 
        CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) IS NOT NULL 
        AND CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) IS NOT NULL 
        AND CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) != CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) 
        AND CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) != '1900-01-01 00:00:00.000' 
        AND CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) != '1900-01-01 00:00:00.000' 
        --AND CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) = '2016-01-24 14:09:00.000' 
        AND EMPLOYEENAME = 'MUHAMMAD BIN PARMIN' 

        -- this is also where you would add any filtering from the source table such as date ranges 
      ) x 
) AS y 

UNION ALL 

SELECT RowID, 
EMPLOYEENAME, 
    -- add an hour to the currentHour each time the recursive CTE is called 
    DATEADD(hour, 1, currentHour) AS currentHour, 
    ShiftA_Start, 
    ShiftA_End, 
    DATEPART(hour, DATEADD(hour, 1, currentHour)) AS hourOrdinal, 
    CAST(CASE 
     -- when this is the last time period determine the amount of the hour that is applicable 
     WHEN DATEADD(hour, 2, currentHour) > ShiftA_End THEN DATEPART(minute, ShiftA_End)/60.0 
     ELSE 1 
    END AS DECIMAL(5,3)) AS hourValue 
from shiftHours 

-- contine recursion until the next hour is after the ShiftEnd 
WHERE DATEADD(hour, 1, currentHour) < ShiftA_End 
) 
SELECT * 
FROM (
SELECT RowID, 
EMPLOYEENAME, 
    ShiftA_Start, 
    ShiftA_End, 
    hourValue, 
    hourOrdinal 
from shiftHours 

) AS t 
PIVOT (
SUM(hourValue) 
FOR hourOrdinal IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23]) 
) AS pvt 
OPTION (MAXRECURSION 0); 

JKQDJKBDJAJSDudkbsjkdbjkBhsjkdbnbjasadbjbdjajdbka dhajdkbobKJBDJBAJBDO

+1

Ihre Frage scheint ziemlich komplex für die Frage, und genau, was ist die Frage? Ist der gewünschte Wert, 23:59:00 am Tag der Schichtende? Was sind die Quelldaten, die Sie für die angezeigte Zeile haben? –

+0

@Vannessa, ich muss lernen, wie man jemand anderen dazu bringt, Software für mich kostenlos zu schreiben, ohne auch nur ein Dankeschön an diese Leute zu sagen. Kannst du helfen? – Alex

+0

@Vanessa Ich schlage vor, Sie bemühen sich, die anderen 7 Fragen, die Sie in diesen Zeilen gestellt haben, zu schließen. Wenn Sie sich bemüht haben, diese andere Frage zu verstehen und zu vervollständigen, könnten Sie feststellen, dass Sie Ihr wiederkehrendes Schichtproblem lösen könnten. –

Antwort

1

In Ihrem inneren Abfrage haben Sie diese Zeile

CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_end, 

sollten Sie nicht stattdessen das Ende der Schicht verwenden?

Verwandte Themen