SELECT "Unique Ident" AS "Unique Ident",
	"Activity Code" AS "Activity Code",
	"Profile Code" AS "Profile Code",
	"Handler" AS "Handler",
	"Function Code" AS "Function Code",
	"Program" AS "Program",
	"Transact Date Long" AS "Transact Date Long",
	"Start Punch Function" AS "Start Punch Function",
	"Start Punch Program" AS "Start Punch Program",
	"End Punch Function" AS "End Punch Function",
	"End Punch Program" AS "End Punch Program",
	"Done For Department" AS "Done For Department",
	"Done For Work Leader" AS "Done For Work Leader",
	"Ended Punch" AS "Ended Punch",
	"Order Number" AS "Order Number",
	"Line Number" AS "Line Number",
	"Start Date Time" AS "Start Date Time",
	"End Date Time" AS "End Date Time",
	"Tmcs Idle Punch" AS "Tmcs Idle Punch",
	"Duration Int" AS "Duration Int",
	"Used Time Int" AS "Used Time Int",
	"Wage Extracted" AS "Wage Extracted",
	"Punch Remark Code" AS "Punch Remark Code",
	"Remark Accepted" AS "Remark Accepted",
	"Punch Period Start Id" AS "Punch Period Start Id",
	"Conv Flag" AS "Conv Flag",
	"Seller Code" AS "Seller Code",
	"Sel Name" AS "Sel Name",
	"Sel Department" AS "Sel Department",
	"Sel First Name" AS "Sel First Name",
	"Sel Family Name" AS "Sel Family Name",
	"Work Leader Group" AS "Work Leader Group",
	"Workleader Text" AS "Workleader Text",
	"Activity Code_Activity" AS "Activity Code_Activity",
	"Activity Description" AS "Activity Description",
	"Present_Activity" AS "Present_Activity",
	"Anwesenheit" AS "Anwesenheit",
	"Nacharbeit" AS "Nacharbeit",
	"Leerlauf/Wartezeit" AS "Leerlauf/Wartezeit",
	"Prob.fahrt/Endkontr._" AS "Prob.fahrt/Endkontr._",
	"Instandhltg. Werkstatt" AS "Instandhltg. Werkstatt",
	"Unprod. Anwes." AS "Unprod. Anwes.",
	"Hilfslohn" AS "Hilfslohn",
	"Schulung intern" AS "Schulung intern",
	"Schulung extern" AS "Schulung extern",
	"Reifenwechsel" AS "Reifenwechsel",
	"Krank" AS "Krank",
	"Arzt" AS "Arzt",
	"Urlaub" AS "Urlaub",
	"Sonderurlaub" AS "Sonderurlaub",
	"Feiertag" AS "Feiertag",
	"zu sp�t" AS "zu sp�t",
	"�berstunden" AS "�berstunden",
	"Order Number_Auftrag" AS "Order Number_Auftrag",
	"Customer Group" AS "Customer Group",
	"Umsatzart" AS "Umsatzart",
	"Extern" AS "Extern",
	"GWL" AS "GWL",
	"Intern" AS "Intern",
	"produktiv" AS "produktiv",
	RSUM("produktiv") AS "Summe  (produktiv) Nr.1",
	"unproduktiv" AS "unproduktiv",
	RSUM("unproduktiv") AS "Summe  (unproduktiv) Nr.1",
	"abwesend" AS "abwesend",
	"Monteur_Gruppe_ori" AS "Monteur_Gruppe_ori",
	"Datum" AS "Datum",
	"Hauptbetrieb" AS "Hauptbetrieb",
	"Department Type Id" AS "Department Type Id",
	"Description" AS "Description",
	"Standort" AS "Standort",
	"Kostenstelle" AS "Kostenstelle",
	"Monteur_ori" AS "Monteur_ori",
	"Konv Zeitkonto minus" AS "Konv Zeitkonto minus",
	"Berufsschule" AS "Berufsschule",
	"Ausb.Zeit mit Handwerker nur Azubi" AS "Ausb.Zeit mit Handwerker nur Azubi",
	"Work Leader Group Id" AS "Work Leader Group Id",
	"Fahrten f�r KDD_" AS "Fahrten f�r KDD_",
	"MW tanken_" AS "MW tanken_",
	"Arbeiten Anlage B_" AS "Arbeiten Anlage B_",
	"Aushilfe Annahme_" AS "Aushilfe Annahme_",
	"Abzug T390" AS "Abzug T390",
	"Monteur_Gruppe" AS "Monteur_Gruppe",
	"Monteur" AS "Monteur",
	"Aushilfe GW_" AS "Aushilfe GW_",
	"Activity_Codes_Group1" AS "Activity_Codes_Group1",
	"Activity_Codes_Group2" AS "Activity_Codes_Group2",
	"Activity_Desc" AS "Activity_Desc",
	"Hauptbetrieb Id" AS "Hauptbetrieb Id",
	"Hauptbetrieb Name" AS "Hauptbetrieb Name",
	"Standort Id" AS "Standort Id",
	"Standort Name" AS "Standort Name"
FROM (
	SELECT T1."UNIQUE_IDENT" AS "Unique Ident",
		T1."ACTIVITY_CODE" AS "Activity Code",
		T1."PROFILE_CODE" AS "Profile Code",
		T1."HANDLER" AS "Handler",
		T1."FUNCTION_CODE" AS "Function Code",
		T1."PROGRAM" AS "Program",
		T1."TRANSACT_DATE_LONG" AS "Transact Date Long",
		T1."START_PUNCH_FUNCTION" AS "Start Punch Function",
		T1."START_PUNCH_PROGRAM" AS "Start Punch Program",
		T1."END_PUNCH_FUNCTION" AS "End Punch Function",
		T1."END_PUNCH_PROGRAM" AS "End Punch Program",
		T1."DONE_FOR_DEPARTMENT" AS "Done For Department",
		T1."DONE_FOR_WORK_LEADER" AS "Done For Work Leader",
		T1."ENDED_PUNCH" AS "Ended Punch",
		T1."ORDER_NUMBER" AS "Order Number",
		T1."LINE_NUMBER" AS "Line Number",
		T1."START_DATE_TIME" AS "Start Date Time",
		T1."END_DATE_TIME" AS "End Date Time",
		T1."TMCS_IDLE_PUNCH" AS "Tmcs Idle Punch",
		T1."DURATION_INT" AS "Duration Int",
		T1."USED_TIME_INT" AS "Used Time Int",
		T1."WAGE_EXTRACTED" AS "Wage Extracted",
		T1."PUNCH_REMARK_CODE" AS "Punch Remark Code",
		T1."REMARK_ACCEPTED" AS "Remark Accepted",
		T1."PUNCH_PERIOD_START_ID" AS "Punch Period Start Id",
		T1."CONV_FLAG" AS "Conv Flag",
		T2."SELLER_CODE" AS "Seller Code",
		T2."SEL_NAME" AS "Sel Name",
		T2."SEL_DEPARTMENT" AS "Sel Department",
		T2."SEL_FIRST_NAME" AS "Sel First Name",
		T2."SEL_FAMILY_NAME" AS "Sel Family Name",
		T3."WORK_LEADER_GROUP" AS "Work Leader Group",
		T3."WORKLEADER_TEXT" AS "Workleader Text",
		T4."ACTIVITY_CODE" AS "Activity Code_Activity",
		T4."ACTIVITY_DESCRIPTION" AS "Activity Description",
		T4."PRESENT" AS "Present_Activity",
		CASE 
			WHEN (T4."PRESENT" = 1)
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Anwesenheit",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('W4  ', '2200'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Nacharbeit",
		CASE 
			WHEN ((substring(T1."ACTIVITY_CODE", 2, 1)) = 'L')
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Leerlauf/Wartezeit",
		'' AS "Prob.fahrt/Endkontr._",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('W1  ', '2210'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Instandhltg. Werkstatt",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1000', '1002', '1001', '1003  '))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Unprod. Anwes.",
		CASE 
			WHEN ((substring(T1."ACTIVITY_CODE", 2, 1)) = 'H')
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Hilfslohn",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('2900'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Schulung intern",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('5300'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Schulung extern",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1060'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Reifenwechsel",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('5100  ', '5110'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Krank",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('3020'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Arzt",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('5000'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Urlaub",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('5010'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Sonderurlaub",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('5200'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Feiertag",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('4010'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "zu sp�t",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('4020'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "�berstunden",
		T5."ORDER_NUMBER" AS "Order Number_Auftrag",
		T5."CUSTOMER_GROUP" AS "Customer Group",
		CASE 
			WHEN (
					(T5."CUSTOMER_GROUP" BETWEEN '10' AND '59')
					OR (T5."CUSTOMER_GROUP" LIKE '7%')
					)
				THEN ('extern')
			WHEN (T5."CUSTOMER_GROUP" LIKE '6%')
				THEN ('GWL')
			WHEN (
					(
						(T5."CUSTOMER_GROUP" LIKE '9%')
						OR (T5."PMT_TERM" = 'IN')
						)
					OR ((left(T5."CUSTOMER_GROUP", 1)) BETWEEN 'A' AND 'Z')
					)
				THEN ('intern')
			ELSE NULL
			END AS "Umsatzart",
		CASE 
			WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('11', '12', '13'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Extern",
		CASE 
			WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('21', '22', '23'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "GWL",
		CASE 
			WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('31', '32', '33'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Intern",
		(
			CASE 
				WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('11', '12', '13'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('21', '22', '23'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN ((substring(T1."ACTIVITY_CODE", 3, 2)) IN ('31', '32', '33'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) AS "produktiv",
		(
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1000', '1002', '1001', '1003  '))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN ((substring(T1."ACTIVITY_CODE", 2, 1)) = 'L')
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('W4  ', '2200'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('W1  ', '2210'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN ((substring(T1."ACTIVITY_CODE", 2, 1)) = 'H')
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('2900'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('W3  '))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1060'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) AS "unproduktiv",
		(
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('3020'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('5300'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('3920'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('4010'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('4020'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('5000'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('5010'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('5100  ', '5110'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('5200'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('996'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('2999'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) AS "abwesend",
		T3."WORK_LEADER_GROUP" + ' - ' + T3."WORKLEADER_TEXT" AS "Monteur_Gruppe_ori",
		((T1."START_DATE_TIME")) AS "Datum",
		T6."CLIENT_DB" AS "Hauptbetrieb",
		T7."DEPARTMENT_TYPE_ID" AS "Department Type Id",
		T7."DESCRIPTION" AS "Description",
		(left(T2."SEL_DEPARTMENT", 2)) AS "Standort",
		(substring(T2."SEL_DEPARTMENT", 4, 1)) AS "Kostenstelle",
		T2."SEL_NAME" AS "Monteur_ori",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('2999'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Konv Zeitkonto minus",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('3920'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Berufsschule",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('W3  '))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Ausb.Zeit mit Handwerker nur Azubi",
		T6."WORK_LEADER_GROUP_ID" AS "Work Leader Group Id",
		'' AS "Fahrten f�r KDD_",
		'' AS "MW tanken_",
		'' AS "Arbeiten Anlage B_",
		'' AS "Aushilfe Annahme_",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('996'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Abzug T390",
		(T3."WORK_LEADER_GROUP" + ' - ' + T3."WORKLEADER_TEXT") AS "Monteur_Gruppe",
		T1."PROFILE_CODE" + ' - ' + T2."SEL_NAME" AS "Monteur",
		'' AS "Aushilfe GW_",
		T8."Zeitkategorie_1" AS "Activity_Codes_Group1",
		T8."Zeitkategorie_2" AS "Activity_Codes_Group2",
		T8."Activity_Code" + ' - ' + T8."Activity_Desc" AS "Activity_Desc",
		T9."Hauptbetrieb_ID" AS "Hauptbetrieb Id",
		T9."Hauptbetrieb_Name" AS "Hauptbetrieb Name",
		T9."Standort_ID" AS "Standort Id",
		T9."Standort_Name" AS "Standort Name"
	FROM "OPTIMA"."import"."EMPLOYEE" T6,
		"OPTIMA"."import"."VPP91" T3,
		(
			(
				(
					(
						(
							(
								(
									"OPTIMA"."import"."PUNCH" T1 LEFT JOIN "OPTIMA"."import"."VPP43" T2 ON (T1."PROFILE_CODE" = T2."SELLER_CODE")
										AND (T1."CLIENT_DB" = T2."CLIENT_DB")
									) LEFT JOIN "OPTIMA"."import"."PROFILE" T10 ON (T2."SELLER_CODE" = T10."PROFILE_CODE")
									AND (T2."CLIENT_DB" = T10."CLIENT_DB")
								) LEFT JOIN "OPTIMA"."import"."ACTIVITY" T4 ON (T1."ACTIVITY_CODE" = T4."ACTIVITY_CODE")
								AND (T1."CLIENT_DB" = T4."CLIENT_DB")
							) LEFT JOIN "OPTIMA"."import"."ORDER_HEADER" T5 ON (T5."ORDER_NUMBER" = T1."ORDER_NUMBER")
							AND (T5."CLIENT_DB" = T1."CLIENT_DB")
						) LEFT JOIN "OPTIMA"."import"."DEPARTMENT_TYPE" T7 ON (T2."SEL_DEPARTMENT" = T7."DEPARTMENT_TYPE_ID")
						AND (T2."CLIENT_DB" = T7."CLIENT_DB")
					) LEFT JOIN "OPTIMA"."data"."GC_Activity_Codes" T8 ON (T4."CLIENT_DB" = T8."Client_DB")
					AND (T4."ACTIVITY_CODE" = T8."Activity_Code")
				) LEFT JOIN "OPTIMA"."data"."GC_Department" T9 ON (T2."CLIENT_DB" = T9."Hauptbetrieb")
				AND ((left(T2."SEL_DEPARTMENT", 2)) = T9."Standort")
			)
	WHERE (
			(T6."PERSON_ID" = T10."PERSON_ID")
			AND (T6."CLIENT_DB" = T10."CLIENT_DB")
			)
		AND (
			(T3."WORK_LEADER_GROUP" = T6."WORK_LEADER_GROUP_ID")
			AND (T3."CLIENT_DB" = T6."CLIENT_DB")
			)
		AND (
			(
				(NOT T1."PROFILE_CODE" IN ('MARE'))
				AND ((((T1."START_DATE_TIME"))) >= convert(DATE, '2020-01-01'))
				)
			AND (((T3."WORK_LEADER_GROUP" + ' - ' + T3."WORKLEADER_TEXT")) IN ('1100 - LH KDD Mechaniker', '1700 - LH VW', '2100 - SE KDD Mechaniker', '2120 - SE KDD 2 Mechaniker', '2700 - SE VW', '3100 - L� KDD Mechaniker', '3200 - L� KAR', '3300 - L� LACK', '3700 - L� VW', '4100 - WE KDD Mechaniker', '4700 - WE VW', '6100 - DO KDD Mechaniker', '6700 - DO VW'))
			)
		-- order by "Transact Date Long" asc
	) D1
