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

SELECT "offene_Auftraege_Teile"."Hauptbetrieb" AS [Hauptbetrieb_imr]
     , "offene_Auftraege_Teile"."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"
     , "offene_Auftraege_Teile"."Serviceberater" AS "Serviceberater"
     , "offene_Auftraege_Teile"."Order Number" AS "Order_Number"
     , "offene_Auftraege_Teile"."Fabrikat" AS [Fabrikat_imr]
     , CASE WHEN "GC_Marken"."Fabrikat" IS NULL THEN 'Fremd' ELSE "GC_Marken"."Fabrikat" END AS "Fabrikat"
     , CASE
           WHEN CASE WHEN "GC_Marken"."Fabrikat" IS NULL THEN 'Fremd' ELSE "GC_Marken"."Fabrikat" END = 'Fremd' THEN 9
           ELSE "GC_Marken"."Order_By"
       END AS "Fabrikat_Order_By"
     , "offene_Auftraege_Teile"."Model" AS "Model"
     , '' AS "Fahrzeug"
     , '' AS "Kostenstelle"
     , "offene_Auftraege_Teile"."Description_Makes" AS "Marke"
     , "offene_Auftraege_Teile"."Kunde" AS "Kunde"
     , "offene_Auftraege_Teile"."Umsatzart" AS "Turnover_Type_Desc"
     , 0.0 AS "Tage_offen"
     , 0.0 AS "DG"
     , 0.0 AS "Lohn"
     , "offene_Auftraege_Teile"."Teile" AS "Teile"
     , 0.0 AS "Sonst_"
     , "offene_Auftraege_Teile"."Order Date" AS "Invoice_Date"
	 , "offene_Auftraege_Teile"."Order Position" AS "Order Position"
	 , "offene_Auftraege_Teile"."Part Number" AS "Part Number"
	 , "offene_Auftraege_Teile"."Text Line" AS "Text Line"
	 , LTRIM(RTRIM(
      CAST("offene_Auftraege_Teile"."Order Position" AS VARCHAR(20))
      + ' - '
      + ISNULL(LTRIM(RTRIM("offene_Auftraege_Teile"."Part Number")), '')
      + ' - '
      + ISNULL(LTRIM(RTRIM("offene_Auftraege_Teile"."Text Line")), '')
       )) AS "Position"
FROM [transform_imr]."offene_Auftraege_Teile" "offene_Auftraege_Teile"
         LEFT OUTER JOIN "data"."GC_Department" "GC_Department" ON "offene_Auftraege_Teile"."Hauptbetrieb" = "GC_Department"."Hauptbetrieb" AND "offene_Auftraege_Teile"."Standort" = "GC_Department"."Standort"
         LEFT OUTER JOIN "data"."GC_Marken" "GC_Marken" ON "offene_Auftraege_Teile"."Fabrikat" = "GC_Marken"."Description"
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
