SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE VIEW [transform].[Zeit_ben_Std_LG] AS

SELECT '1' AS [Hauptbetrieb_imr]
     , convert(varchar, "Zeit_ben_Std_LG3"."Standort") AS [Standort_imr]
     , "GC_Department"."Hauptbetrieb_ID" AS "Hauptbetrieb_ID"
     , "GC_Department"."Hauptbetrieb_Name" AS "Hauptbetrieb_Name"
     , "GC_Department"."Standort_ID" AS "Standort_ID"
     , "GC_Department"."Standort_Name" AS "Standort_Name"
     , convert(varchar, "Zeit_ben_Std_LG3"."Employee_Number_Employee") AS [Employee_ID_imr]
     , "GC_Mitarbeiter"."Zuordnung_Produktiv" AS "Monteur_Gruppe"
     , "GC_Mitarbeiter"."Monteur_Ebene_1" AS "Monteur_Gruppe_2"
     , "Zeit_ben_Std_LG3"."Monteur" AS "Monteur"
     , convert(varchar, "Zeit_ben_Std_LG3"."Order_Number") + ' - ' + "Zeit_ben_Std_LG3"."Description_Labour_Types" AS "Order_Number"
     , "Zeit_ben_Std_LG3"."Zeitkategorie" AS "Activity_Codes_Group1"
     , "Zeit_ben_Std_LG3"."Zeitkategorie2" AS "Activity_Codes_Group2"
     , '' AS "Activity_Desc"
     , '' AS "Kostenstelle"
     , "Zeit_ben_Std_LG3"."Datum" AS "Invoice_Date"
     , 0.0 AS "prod__"
     , 0.0 AS "unprod__falsch"
     , 0.0 AS "Abw_"
     , 0.0 AS "Extern"
     , 0.0 AS "GWL"
     , 0.0 AS "Intern"
     , 0.0 AS "Sollzeit"
     , 0.0 AS "TOY_produktiv_Zeit"
     , 0.0 AS "verr__Zeit_ohne_TOY"
     , "Zeit_ben_Std_LG3"."ben__Std_" AS "ben__Zeit_ohne_TOY"
     , 0.0 AS "Anz__AT"
     , "Zeit_ben_Std_LG3"."ben__Std_" AS "ben__Zeit"
     , '' AS "Order_Positions_Times"
     , 'abgerechnet' AS "Status"
     , "GC_Mitarbeiter"."productivity_factor" AS "productivity_factor"
     , CASE
           WHEN datediff(DD, "Zeit_ben_Std_LG3"."Datum",
                         convert(datetime, convert(char(8), current_timestamp, 112), 112)) < 7 THEN
               convert(varchar, "Zeit_ben_Std_LG3"."Order_Number") + ' - ' + "Zeit_ben_Std_LG3"."Description_Labour_Types"
           ELSE N'älter 7 Tage'
       END AS "Order_Number_Mont"
FROM ((SELECT "Zeit_ben_Std_LG"."Order Number" AS "Order_Number"
            , "Zeit_ben_Std_LG"."Description_Labour_Types" AS "Description_Labour_Types"
            , "Zeit_ben_Std_LG"."Zeitkategorie" AS "Zeitkategorie"
            , "Zeit_ben_Std_LG"."Zeitkategorie2" AS "Zeitkategorie2"
            , "Zeit_ben_Std_LG"."Employee Number_Employee" AS "Employee_Number_Employee"
            , "Zeit_ben_Std_LG"."Monteur" AS "Monteur"
            , "Zeit_ben_Std_LG"."Datum" AS "Datum"
            , "Zeit_ben_Std_LG"."ben. Std." AS "ben__Std_"
            , "Zeit_ben_Std_LG"."Subsidiary_Labours" AS "Standort"
       FROM [transform_imr]."Zeit_ben_Std_LG" "Zeit_ben_Std_LG") "Zeit_ben_Std_LG3"
    LEFT OUTER JOIN "data"."GC_Department" "GC_Department" ON "GC_Department"."Hauptbetrieb" = '1' AND convert(varchar, "Zeit_ben_Std_LG3"."Standort") = "GC_Department"."Standort"
    LEFT OUTER JOIN "data"."GC_Mitarbeiter" "GC_Mitarbeiter"
      ON convert(varchar, "Zeit_ben_Std_LG3"."Employee_Number_Employee") = "GC_Mitarbeiter"."Employee_ID")
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
