SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE VIEW [transform].[TBL_SERVICE_GUV_O21] AS
SELECT ' ' AS [Acct Nr]
     , ' ' AS [Ledger Accts Name]
     , ' ' AS [Product Group]
     , ' ' AS [Repair Group]
     , cast([Rg].[Order Number] AS varchar(50)) AS [Order Number]
     , cast([Rg].[Invoice Type] AS varchar(50)) AS [RG-Typ]
     , cast([Rg].[Invoice Number] AS varchar(50)) AS [RG-Nummer]
     , [Rg].[Invoice_Type_Invoice_Number] AS [RG-Typ-Nr]
     , [Rg].[Hauptbetrieb Id] AS [Rechtseinheit_ID]
     , [Rg].[Hauptbetrieb Name] AS [Rechtseinheit_Name]
     , ISNULL([GC_Marke].[Order_by], '99') AS [Fabrikat_Order_By]
     , [Rg].[Standort Id] AS [Betrieb_ID]
     , [Rg].[Standort Name] AS [Betrieb_Name]
     , [Rg].[Serviceberater] AS [Serviceberater]
     , [Rg].[Umsatzart] AS [Umsatzart]
     , [Rg].[Time Units] / 10 AS [verk. Stunden]
--	  ,COUNT(Invoice_Type_Invoice_Number) OVER (PARTITION BY [Order Number]) AS "Anzahl"
--   , cast([ben_AW].[Summe_Duration_Minutes] / 60.0 AS decimal(26, 12)) AS [benutzte Zeit (Auftrag)]
	  ,ben_AW.ben_Std_Auftrag / COUNT(Invoice_Type_Invoice_Number) OVER (PARTITION BY [Order Number]) AS [benutzte Zeit (Auftrag)]
     , 1.0 / COUNT(Invoice_Type_Invoice_Number) OVER (PARTITION BY [Order Number]) AS [DG]
	 , ben_AW.Monteur_und_Stunden
     , ' ' AS [Zuordnung_Funktion]
     , [Rg].[Zuordnung] AS [Zuordnung_Produktbuchungsgruppe]
     , [Rg].[Produktbuchungsgruppe] AS [Produktbuchungsgruppe]
     , [Rg].[Kostenstelle] AS [Kostenstelle]
     , [Rg].[Auftragsart] AS [Auftragsart]
     , [Rg].[Kunde_Auftrag] AS [Kunde]
     , ' ' AS [Kundenart]
     , cast([Rg].[Order Number] AS varchar(50)) + ' - ' + cast([Rg].[Kunde_Auftrag] AS varchar(255)) AS [Order_Desc_100]
     , [Rg].[Auftragsposition] AS [Auftragsposition]
     , ' ' AS [Repair_Group_Desc]
     , 'Rechnung' AS [Rechnung/Gutschrift]
     , ' ' AS [FZG-Altersstaffel]
     , ' ' AS [VK < EK]
     , ' ' AS [PLZ_1_Stelle]
     , ' ' AS [PLZ_2_Stelle]
     , ' ' AS [PLZ_3_Stelle]
     , ' ' AS [PLZ_4_Stelle]
     , ' ' AS [PLZ]
     , [Rg].[Invoice Date] AS [Invoice Date]
     , [Rg].[Lohn]
     , [Rg].[NL Lohn]
     , [Rg].[Fremdl.]
     , [Rg].[EW Fremdl.]
     , [Rg].[Teile] AS [Teile_gesamt]
     , [Rg].[EW Teile] AS [EW Teile_gesamt]
     , [Rg].[NL Teile]
     , [Rg].[Mietw.]
     , [Rg].[Sonst.]
     , 0 AS [EW Sonst.]
     , 0 AS [EW Mietw.]
     , [Rg].[Rebate Percent] AS [NL %]
     , [Rg].[Goodwill Percent] AS [Kulanz %]
 --    , CASE
 --          WHEN [Rg].[Source Table] = 'labours' AND [Rg].[NL Lohn] <> 0 THEN [Rg].[Rebate Percent]
 --          ELSE NULL
 --      END AS [NL Lohn %]
	   , CASE
           WHEN [Rg].[Source Table] = 'labours' AND [Rg].[NL Lohn] <> 0 THEN ([Rg].[NL Lohn] / ([Rg].[NL Lohn] + [Rg].[Lohn]) *100)
           ELSE NULL
       END AS [NL Lohn %]
 --    , CASE
 --          WHEN [Rg].[Source Table] = 'parts' AND [Rg].[NL Teile] <> 0 THEN [Rg].[Rebate Percent]
 --          ELSE NULL
 --      END AS [NL Teile %]
	    , CASE
           WHEN [Rg].[Source Table] = 'parts' AND [Rg].[NL Teile] <> 0 THEN ([Rg].[NL Teile] / ([Rg].[NL Teile] + [Rg].[Teile]) *100)
           ELSE NULL
       END AS [NL Teile %]
     , CASE
           WHEN [Rg].[Source Table] = 'labours' AND [Rg].[NL Lohn] <> 0 THEN [Rg].[Goodwill Percent]
           ELSE NULL
       END AS [Kul. Lohn %]
     , CASE
           WHEN [Rg].[Source Table] = 'parts' AND [Rg].[NL Teile] <> 0 THEN [Rg].[Goodwill Percent]
           ELSE NULL
       END AS [Kul. Teile %]
     , [Rg].[Time Units] / 10 AS [verk. Std. Monteur]
     , [Rg].[Hauptbetrieb Id] AS [Gruppe Id]
     , ' ' AS [Woche]
     , [Rg].[Standort Name] AS [Gruppe Name]
     , [Rg].[Fabrikat] AS [Fabrikat operativ]
     , ISNULL([GC_Marke].[Fabrikat], 'Fremd') AS [Fabrikat_GC_Marken_Order_Line]
	 , [Rg].[Model] AS [Model]
     , [Rg].[Position] AS [Position]
     , [Rg].[Kunde] AS [Kunde_RG]
     , [Rg].[Source Table]
	 , CASE 
		WHEN [Rg].[Source Table] = 'labours' THEN 'Arbeit'
		WHEN [Rg].[Source Table] = 'parts' THEN 'Teile'
	   ELSE NULL
	   END AS Positionsart
     , cast([Rg].[Invoice Date] AS date) AS [Datum]
     , format([Rg].[Invoice Date], 'yyyy/MM') AS [Jahr/Monat]
     , cast(getdate() AS date) AS [Aktueller Tag]
     , cast(dateadd(MONTH, datediff(MONTH, 0, [Rg].[Invoice Date]), 0) AS date) AS [Monatserster]
     , eomonth([Rg].[Invoice Date]) AS [Monatsletzter]
     , '("' +
       convert(varchar, dateadd(MONTH, datediff(MONTH, 0, [Rg].[Invoice Date]), 0), 23) +
       '","' +
       convert(varchar, eomonth([Rg].[Invoice Date]), 23) +
       '")' AS [MUN_DATE]

--[20250201-20250228]
     , convert(varchar, dateadd(MONTH, datediff(MONTH, 0, [Rg].[Invoice Date]), 0), 112) +
       '-' +
       convert(varchar, eomonth([Rg].[Invoice Date]), 112) AS [Mitgliedsname]
     , datefromparts(year([Rg].[Invoice Date]), 1, 1) AS [Beginn Geschäftsjahr]
     , format([Rg].[Invoice Date], 'MMM', 'de') + './' + format([Rg].[Invoice Date], 'yyyy', 'de') AS [Monat_Jahr]
     , [AnzahlRechnungen].[Anz. Rg.] AS [Anz. Rg.]
--     , 'Anz. Rg.: ' + cast([AnzahlRechnungen].[Anz. Rg.] AS varchar(10)) AS [Anz. Rg. / Auftr.]
, CONCAT(
    'Anz. Rg.: ',
    LTRIM(STR([AnzahlRechnungen].[Anz. Rg.], 20, 0))
  ) AS [Anz. Rg. / Auftr.]
FROM [transform_imr].[Aftersales_Rechnungen_operativ] [Rg]
         LEFT JOIN [transform_ims].[ben_AW_Order_Number] [ben_AW] ON [ben_AW].[Order_Number] = [Rg].[Order Number]
		 LEFT JOIN [data].[GC_Marken] [GC_Marke] ON [GC_Marke].[Description] = [Rg].[Fabrikat]
         CROSS APPLY (SELECT count(DISTINCT [Rg2].[Invoice Number]) AS [Anz. Rg.]
                      FROM [transform_imr].[Aftersales_Rechnungen_operativ] [Rg2]
                      WHERE [Rg2].[Order Number] = [Rg].[Order Number]) AS [AnzahlRechnungen]
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
