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 ('1011', '1026', '1027', '2011', '3011', '3026', '3027'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Nacharbeit",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('999', '1012', '1013', '2012', '2013', '3012', '3013'))
				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', '1021', '1022', '1023  ', '1024', '1060', '1062', '1092', '1104', '1600', '1700', '1800', '1900', '2000', '2021', '2022', '2023', '2024', '2092', '2600', '2700', '2800', '2900', '3000', '3021', '3022', '3023', '3024', '3025', '3092', '3600', '3700', '3800', '3900'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Unprod. Anwes.",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1051', '1052', '1053', '1054', '1055', '1056', '1057', '1058', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '3051', '3052', '3053', '3054', '3055', '3056', '3057', '3058'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Hilfslohn",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1091', '2091', '3091'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Schulung intern",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('3090', '2090', '1090'))
				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 ('1101  ', '1103', '2101', '2103', '3101', '3103'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Krank",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1102', '2102', '3102'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Arzt",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1105', '2105', '3105'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Urlaub",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1107', '2107', '3107'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Sonderurlaub",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1106', '2106', '3106'))
				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 ('1199', '1200', '2199', '2200', '3199', '3200'))
				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 (T1."ACTIVITY_CODE" IN ('1001', '1002', '1003', '2001', '2002', '2003', '3001', '3002', '3003', '1061'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Extern",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1004', '1014', '1015', '2004', '3004', '3014', '3015'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "GWL",
		CASE 
			WHEN (T1."ACTIVITY_CODE" IN ('1005', '1006', '1017', '2005', '2006', '3005', '3006', '3017'))
				THEN (T1."USED_TIME_INT")
			ELSE (0)
			END AS "Intern",
		(
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1001', '1002', '1003', '2001', '2002', '2003', '3001', '3002', '3003', '1061'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1004', '1014', '1015', '2004', '3004', '3014', '3015'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1005', '1006', '1017', '2005', '2006', '3005', '3006', '3017'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) AS "produktiv",
		(
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1000', '1021', '1022', '1023  ', '1024', '1060', '1062', '1092', '1104', '1600', '1700', '1800', '1900', '2000', '2021', '2022', '2023', '2024', '2092', '2600', '2700', '2800', '2900', '3000', '3021', '3022', '3023', '3024', '3025', '3092', '3600', '3700', '3800', '3900'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('999', '1012', '1013', '2012', '2013', '3012', '3013'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1011', '1026', '1027', '2011', '3011', '3026', '3027'))
					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 (T1."ACTIVITY_CODE" IN ('1051', '1052', '1053', '1054', '1055', '1056', '1057', '1058', '2051', '2052', '2053', '2054', '2055', '2056', '2057', '2058', '3051', '3052', '3053', '3054', '3055', '3056', '3057', '3058'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1091', '2091', '3091'))
					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 ('1102', '2102', '3102'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('3090', '2090', '1090'))
					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 ('1199', '1200', '2199', '2200', '3199', '3200'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1105', '2105', '3105'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1107', '2107', '3107'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1101  ', '1103', '2101', '2103', '3101', '3103'))
					THEN (T1."USED_TIME_INT")
				ELSE (0)
				END
			) + (
			CASE 
				WHEN (T1."ACTIVITY_CODE" IN ('1106', '2106', '3106'))
					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", 1)) = 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 (
			((((T1."START_DATE_TIME"))) >= convert(DATE, '2020-01-01'))
			AND (((T3."WORK_LEADER_GROUP" + ' - ' + T3."WORKLEADER_TEXT")) IN ('1000 - Mechanik Menden', '2000 - Mechanik Hemer', '3000 - Mechanik Balve', '1100 - Karosserie Menden', '1200 - Lackiererei Menden', '2100 - Karosserie Hemer', '2200 - Lackiererei Hemer', '3100 - karosserie Balve', '3200 - Lackiererei Balve'))
			)
		-- order by "Transact Date Long" asc
	) D1
