SELECT "Order Number_ori_2",
	"Register Number",
	"Status",
	"State Key Date",
	"Debit Account",
	"Invoice Number",
	"Workshop Model",
	"State Code",
	"Transact Date",
	"Handler",
	"Delivery Account",
	"Department",
	"Debet Department",
	"Salesman_2",
	"Debit Perm",
	"Order Date",
	"Delivery Date",
	"Invoice Date_ori",
	"Tax Code",
	"Tax Perc",
	"Pmt Term",
	"Next Line Number",
	"Duedate 1",
	"Payment Text",
	"Costs",
	"Purch Tax",
	"Orders Grossvalue",
	"Tax Share",
	"Discount Amount",
	"Price Code",
	"Misc Adds",
	"Stock",
	"Place Code",
	"Internal Code",
	"Customer Group_2",
	"Small Accessories",
	"Invoice Copy Code",
	"Basis Number",
	"Mileage",
	"Prev Status",
	"Sales Class Number",
	"Invoice Disc Perc",
	"Invoice Rounded",
	"Invoice Charge",
	"Sales Tax Free",
	"Title",
	"Name",
	"Street Addr",
	"Addr 2",
	"Zipcode",
	"Mail Addr",
	"Discount Limit",
	"Reference Number",
	"Expected Order Tim",
	"Bol Tax Share",
	"Model Text",
	"Workshop Pricecode",
	"Split Counter",
	"Arrival Time",
	"Arrival Date",
	"End Date",
	"End Time",
	"Fac Model Code S",
	"Make Cd_3",
	"Year Model",
	"Transfer Make Cd",
	"Chassis Number",
	"Workshop Team",
	"Commission Salesman",
	"Ref Ident Inv Total",
	"Ref Ident Sales Class",
	"Use Parts Pre Picking",
	"Ldc Order",
	"Fhg Report",
	"Actual Inv Date Time",
	"Conv Flag",
	"Unique Ident_2",
	"Order Number_ori",
	"Line Number",
	"Order Linetype",
	"Reduction Code",
	"Reduction Amount",
	"Mechanic Code",
	"Salesman",
	"Discount",
	"Stdprice",
	"Lines Net Value",
	"Prod Code",
	"Make Cd_2",
	"Product Group",
	"Prod Name",
	"Order Quantity",
	"Delivery Quantity",
	"Line Costs",
	"Repair Code",
	"Repair Group_2",
	"Repair Name",
	"Used Time",
	"Est Time",
	"Inv Time",
	"Used Time Int",
	"Est Time Int",
	"Inv Time Int",
	"Make Time Unit",
	"Unique Ident",
	"Stat Code",
	"Stat Specify",
	"Department Type Id",
	"Description_2",
	"Seller Code_2",
	"Sel Name",
	"Sel Department_2",
	"Sel First Name",
	"Sel Family Name",
	"Customer Group",
	"Cust Group Specify",
	"Seller Code",
	"Sel Name_Monteur",
	"Sel Department",
	"Sel First Name_Monteur",
	"Sel Family Name_Monteur",
	"Repair Group",
	"Make Cd",
	12 AS "AW_Faktor",
	"Fabrikat_ori",
	"Repair Grp Specify",
	"Hauptbetrieb",
	"Standort",
	"Marke",
	"Kostenstelle",
	"Kundenart",
	"Umsatzart",
	"Gesch�ftsart",
	"Sel Name" AS "Serviceberater",
	"Sel Name_Monteur" AS "Monteur",
	"Auftragsart",
	"Auftragsstatus",
	"Umsatz Teile Service",
	"Umsatz Lohn",
	"Umsatz Teile (nur Teile)" AS "Umsatz Teile (nur Teile)",
	"Status_1",
	"Inv Time Int" AS "verk. Stunden",
	"Est Time Int" AS "Soll-Stunden (Auftrag)",
	"Used Time Int" AS "benutzte Zeit (Auftrag)",
	"Umsatz Sonstiges",
	"verk. AW",
	"Kunde",
	"Soll AW",
	"benutzte AW",
	"Global Make Cd",
	"Description",
	"Model",
	"Fahrzeug",
	"Auftrag_Det_S",
	"Auftrag_Det_T",
	"Anzahl Tage",
	"Order Number",
	"Fabrikat",
	1 AS "DG_1",
	COUNT("Status") OVER (PARTITION BY "Order Number_ori_2") AS "Anzahl_Datens�tze",
	1 / (COUNT("Status") OVER (PARTITION BY "Order Number_ori_2")) AS "DG",
	"Order Date" AS "Invoice Date",
	"Anzahl Tage" AS "Tage offen_1",
	("Anzahl Tage") / (COUNT("Status") OVER (PARTITION BY "Order Number_ori_2")) AS "Tage offen",
	"Hauptbetrieb_ID",
	"Hauptbetrieb_Name",
	"Standort_ID",
	"Standort_Name"
FROM (
	SELECT T1."ORDER_NUMBER" AS "Order Number_ori_2",
		T12."Standort_Name" AS "Standort_Name",
		T12."Standort_ID" AS "Standort_ID",
		T12."Hauptbetrieb_Name" AS "Hauptbetrieb_Name",
		CASE 
			WHEN (T12."Hauptbetrieb_ID" IS NULL)
				THEN ('1')
			ELSE (T12."Hauptbetrieb_ID")
			END AS "Hauptbetrieb_ID",
		(- 1 * datediff(day, (getdate()), T1."ORDER_DATE")) AS "Anzahl Tage",
		T1."ORDER_DATE" AS "Order Date",
		CASE 
			WHEN (T9."Fabrikat" IS NULL)
				THEN ('Fremd')
			ELSE (T9."Fabrikat")
			END AS "Fabrikat",
		(left((((T1."ORDER_NUMBER"))), 7)) + ' - ' + (rtrim(((rtrim(T1."DEBIT_ACCOUNT")) + ' - ' + T1."NAME"))) + ' - ' + T1."REGISTER_NUMBER" AS "Order Number",
		(left((((T1."ORDER_NUMBER"))), 7)) + ' - ' + (substring((convert(VARCHAR(50), year(T1."ORDER_DATE")) + '-' + convert(VARCHAR(50), month(T1."ORDER_DATE")) + '-' + convert(VARCHAR(50), day(T1."ORDER_DATE"))), 1, 10)) + ' - ' + T1."DELIVERY_ACCOUNT" + T1."NAME" + ' - ' + T1."REGISTER_NUMBER" + ' - ' + T1."STATUS" AS "Auftrag_Det_T",
		(left((((T1."ORDER_NUMBER"))), 7)) + ' - ' + (substring((convert(VARCHAR(50), year(T1."ORDER_DATE")) + '-' + convert(VARCHAR(50), month(T1."ORDER_DATE")) + '-' + convert(VARCHAR(50), day(T1."ORDER_DATE"))), 1, 10)) + ' - ' + (rtrim(T1."DELIVERY_ACCOUNT")) + ' ' + T1."NAME" + ' - ' + (rtrim(T1."REGISTER_NUMBER")) + ' - ' + T1."STATUS" AS "Auftrag_Det_S",
		T1."BASIS_NUMBER" + ' - ' + T1."CHASSIS_NUMBER" AS "Fahrzeug",
		T11."MOD_LIN_SPECIFY" AS "Model",
		T10."DESCRIPTION" AS "Description",
		T10."GLOBAL_MAKE_CD" AS "Global Make Cd",
		T2."USED_TIME_INT" * 12 AS "benutzte AW",
		T2."EST_TIME_INT" * 12 AS "Soll AW",
		(rtrim(T1."DEBIT_ACCOUNT")) + ' - ' + T1."NAME" AS "Kunde",
		T2."INV_TIME_INT" * 12 AS "verk. AW",
		CASE 
			WHEN (T2."ORDER_LINETYPE" = '3')
				THEN (T2."LINES_NET_VALUE")
			ELSE NULL
			END AS "Umsatz Sonstiges",
		T2."USED_TIME_INT" AS "Used Time Int",
		T2."EST_TIME_INT" AS "Est Time Int",
		T2."INV_TIME_INT" AS "Inv Time Int",
		CASE 
			WHEN (T1."STATUS" IN ('35', '37', '39', '47', '49', '34', '36'))
				THEN ('Rechnung/Gutschrift')
			WHEN (T1."STATUS" IN ('30', '40', '32'))
				THEN ('offen')
			WHEN (T1."STATUS" IN ('41', '31'))
				THEN ('Kostenvoranschlag')
			ELSE NULL
			END AS "Status_1",
		CASE 
			WHEN (
					(T2."ORDER_LINETYPE" = '1')
					AND (T1."STATUS" BETWEEN '30' AND '39')
					)
				THEN (T2."LINES_NET_VALUE")
			ELSE NULL
			END AS "Umsatz Teile (nur Teile)",
		CASE 
			WHEN (T2."INV_TIME" <> 0)
				THEN (T2."LINES_NET_VALUE")
			ELSE NULL
			END AS "Umsatz Lohn",
		CASE 
			WHEN (
					(T2."ORDER_LINETYPE" = '1')
					AND (T1."STATUS" BETWEEN '40' AND '49')
					)
				THEN (T2."LINES_NET_VALUE")
			ELSE NULL
			END AS "Umsatz Teile Service",
		T1."STATUS" + ' - ' + T3."STAT_SPECIFY" AS "Auftragsstatus",
		CASE 
			WHEN (T1."STATUS" BETWEEN '30' AND '39')
				THEN ('Teile')
			WHEN (T1."STATUS" BETWEEN '40' AND '49')
				THEN ('Service')
			WHEN (T1."STATUS" = '70')
				THEN ('sonst. Auftrag')
			WHEN (T1."STATUS" = '91')
				THEN ('Anfrage')
			ELSE NULL
			END AS "Auftragsart",
		T7."SEL_NAME" AS "Sel Name_Monteur",
		T5."SEL_NAME" AS "Sel Name",
		T1."PMT_TERM" + ' - ' + T1."PAYMENT_TEXT" AS "Gesch�ftsart",
		CASE 
			WHEN (T1."CUSTOMER_GROUP" BETWEEN '10' AND '59')
				THEN ('extern')
			WHEN (T1."CUSTOMER_GROUP" IN ('91', '92', '60', '61', '62', '63', '64', '70', '71', '72', '74'))
				THEN ('GWL')
			WHEN (
					(
						(T1."CUSTOMER_GROUP" IN ('99'))
						OR (T1."PMT_TERM" = 'IN')
						)
					OR ((left(T1."CUSTOMER_GROUP", 1)) BETWEEN 'A' AND 'Z')
					)
				THEN ('intern')
			ELSE NULL
			END AS "Umsatzart",
		T6."CUSTOMER_GROUP" + ' - ' + T6."CUST_GROUP_SPECIFY" AS "Kundenart",
		T4."DEPARTMENT_TYPE_ID" + ' - ' + T4."DESCRIPTION" AS "Kostenstelle",
		CASE 
			WHEN (((left(T1."DEPARTMENT", 2))) IN ('05', '09', '111', '06', '08', '01', '10', '14', '12', '03', '04', '02'))
				THEN ('1')
			ELSE ((substring(T1."DEPARTMENT", 2, 1)))
			END AS "Marke",
		(left(T1."DEPARTMENT", 2)) AS "Standort",
		T1."CLIENT_DB" AS "Hauptbetrieb",
		T8."REPAIR_GRP_SPECIFY" AS "Repair Grp Specify",
		T9."Fabrikat" AS "Fabrikat_ori",
		T8."MAKE_CD" AS "Make Cd",
		T8."REPAIR_GROUP" AS "Repair Group",
		T7."SEL_FAMILY_NAME" AS "Sel Family Name_Monteur",
		T7."SEL_FIRST_NAME" AS "Sel First Name_Monteur",
		T7."SEL_DEPARTMENT" AS "Sel Department",
		T7."SELLER_CODE" AS "Seller Code",
		T6."CUST_GROUP_SPECIFY" AS "Cust Group Specify",
		T6."CUSTOMER_GROUP" AS "Customer Group",
		T5."SEL_FAMILY_NAME" AS "Sel Family Name",
		T5."SEL_FIRST_NAME" AS "Sel First Name",
		T5."SEL_DEPARTMENT" AS "Sel Department_2",
		T5."SELLER_CODE" AS "Seller Code_2",
		T4."DESCRIPTION" AS "Description_2",
		T4."DEPARTMENT_TYPE_ID" AS "Department Type Id",
		T3."STAT_SPECIFY" AS "Stat Specify",
		T3."STAT_CODE" AS "Stat Code",
		T2."UNIQUE_IDENT" AS "Unique Ident",
		T2."MAKE_TIME_UNIT" AS "Make Time Unit",
		T2."INV_TIME" AS "Inv Time",
		T2."EST_TIME" AS "Est Time",
		T2."USED_TIME" AS "Used Time",
		T2."REPAIR_NAME" AS "Repair Name",
		T2."REPAIR_GROUP" AS "Repair Group_2",
		T2."REPAIR_CODE" AS "Repair Code",
		T2."LINE_COSTS" AS "Line Costs",
		T2."DELIVERY_QUANTITY" AS "Delivery Quantity",
		T2."ORDER_QUANTITY" AS "Order Quantity",
		T2."PROD_NAME" AS "Prod Name",
		T2."PRODUCT_GROUP" AS "Product Group",
		T2."MAKE_CD" AS "Make Cd_2",
		T2."PROD_CODE" AS "Prod Code",
		T2."LINES_NET_VALUE" AS "Lines Net Value",
		T2."STDPRICE" AS "Stdprice",
		T2."DISCOUNT" AS "Discount",
		T2."SALESMAN" AS "Salesman",
		T2."MECHANIC_CODE" AS "Mechanic Code",
		T2."REDUCTION_AMOUNT" AS "Reduction Amount",
		T2."REDUCTION_CODE" AS "Reduction Code",
		T2."ORDER_LINETYPE" AS "Order Linetype",
		T2."LINE_NUMBER" AS "Line Number",
		T2."ORDER_NUMBER" AS "Order Number_ori",
		T1."UNIQUE_IDENT" AS "Unique Ident_2",
		T1."CONV_FLAG" AS "Conv Flag",
		T1."ACTUAL_INV_DATE_TIME" AS "Actual Inv Date Time",
		T1."FHG_REPORT" AS "Fhg Report",
		T1."LDC_ORDER" AS "Ldc Order",
		T1."USE_PARTS_PRE_PICKING" AS "Use Parts Pre Picking",
		T1."REF_IDENT_SALES_CLASS" AS "Ref Ident Sales Class",
		T1."REF_IDENT_INV_TOTAL" AS "Ref Ident Inv Total",
		T1."COMMISSION_SALESMAN" AS "Commission Salesman",
		T1."WORKSHOP_TEAM" AS "Workshop Team",
		T1."CHASSIS_NUMBER" AS "Chassis Number",
		T1."TRANSFER_MAKE_CD" AS "Transfer Make Cd",
		T1."YEAR_MODEL" AS "Year Model",
		T1."MAKE_CD" AS "Make Cd_3",
		T1."FAC_MODEL_CODE_S" AS "Fac Model Code S",
		T1."END_TIME" AS "End Time",
		T1."END_DATE" AS "End Date",
		T1."ARRIVAL_DATE" AS "Arrival Date",
		T1."ARRIVAL_TIME" AS "Arrival Time",
		T1."SPLIT_COUNTER" AS "Split Counter",
		T1."WORKSHOP_PRICECODE" AS "Workshop Pricecode",
		T1."MODEL_TEXT" AS "Model Text",
		T1."BOL_TAX_SHARE" AS "Bol Tax Share",
		T1."EXPECTED_ORDER_TIM" AS "Expected Order Tim",
		T1."REFERENCE_NUMBER" AS "Reference Number",
		T1."DISCOUNT_LIMIT" AS "Discount Limit",
		T1."MAIL_ADDR" AS "Mail Addr",
		T1."ZIPCODE" AS "Zipcode",
		T1."ADDR_2" AS "Addr 2",
		T1."STREET_ADDR" AS "Street Addr",
		T1."NAME" AS "Name",
		T1."TITLE" AS "Title",
		T1."SALES_TAX_FREE" AS "Sales Tax Free",
		T1."INVOICE_CHARGE" AS "Invoice Charge",
		T1."INVOICE_ROUNDED" AS "Invoice Rounded",
		T1."INVOICE_DISC_PERC" AS "Invoice Disc Perc",
		T1."SALES_CLASS_NUMBER" AS "Sales Class Number",
		T1."PREV_STATUS" AS "Prev Status",
		T1."MILEAGE" AS "Mileage",
		T1."BASIS_NUMBER" AS "Basis Number",
		T1."INVOICE_COPY_CODE" AS "Invoice Copy Code",
		T1."SMALL_ACCESSORIES" AS "Small Accessories",
		T1."CUSTOMER_GROUP" AS "Customer Group_2",
		T1."INTERNAL_CODE" AS "Internal Code",
		T1."PLACE_CODE" AS "Place Code",
		T1."STOCK" AS "Stock",
		T1."MISC_ADDS" AS "Misc Adds",
		T1."PRICE_CODE" AS "Price Code",
		T1."DISCOUNT_AMOUNT" AS "Discount Amount",
		T1."TAX_SHARE" AS "Tax Share",
		T1."ORDERS_GROSSVALUE" AS "Orders Grossvalue",
		T1."PURCH_TAX" AS "Purch Tax",
		T1."COSTS" AS "Costs",
		T1."PAYMENT_TEXT" AS "Payment Text",
		T1."DUEDATE_1" AS "Duedate 1",
		T1."NEXT_LINE_NUMBER" AS "Next Line Number",
		T1."PMT_TERM" AS "Pmt Term",
		T1."TAX_PERC" AS "Tax Perc",
		T1."TAX_CODE" AS "Tax Code",
		T1."INVOICE_DATE" AS "Invoice Date_ori",
		T1."DELIVERY_DATE" AS "Delivery Date",
		T1."DEBIT_PERM" AS "Debit Perm",
		T1."SALESMAN" AS "Salesman_2",
		T1."DEBET_DEPARTMENT" AS "Debet Department",
		T1."DEPARTMENT" AS "Department",
		T1."DELIVERY_ACCOUNT" AS "Delivery Account",
		T1."HANDLER" AS "Handler",
		T1."TRANSACT_DATE" AS "Transact Date",
		T1."STATE_CODE" AS "State Code",
		T1."WORKSHOP_MODEL" AS "Workshop Model",
		T1."INVOICE_NUMBER" AS "Invoice Number",
		T1."DEBIT_ACCOUNT" AS "Debit Account",
		T1."STATE_KEY_DATE" AS "State Key Date",
		T1."STATUS" AS "Status",
		T1."REGISTER_NUMBER" AS "Register Number"
	FROM (
		(
			(
				(
					(
						(
							(
								(
									(
										(
											(
												(
													"OPTIMA"."import"."ORDER_HEADER" T1 LEFT JOIN "OPTIMA"."import"."ORDER_LINE" T2 ON (T1."ORDER_NUMBER" = T2."ORDER_NUMBER")
														AND (T1."CLIENT_DB" = T2."CLIENT_DB")
													) LEFT JOIN "OPTIMA"."import"."VPP25" T3 ON (T1."STATUS" = T3."STAT_CODE")
													AND (T1."CLIENT_DB" = T3."CLIENT_DB")
												) LEFT JOIN "OPTIMA"."import"."DEPARTMENT_TYPE" T4 ON (T1."DEPARTMENT" = T4."DEPARTMENT_TYPE_ID")
												AND (T1."CLIENT_DB" = T4."CLIENT_DB")
											) LEFT JOIN "OPTIMA"."import"."VPP43" T5 ON (T1."SALESMAN" = T5."SELLER_CODE")
											AND (T1."CLIENT_DB" = T5."CLIENT_DB")
										) LEFT JOIN "OPTIMA"."import"."VPP48" T6 ON (T1."CUSTOMER_GROUP" = T6."CUSTOMER_GROUP")
										AND (T1."CLIENT_DB" = T6."CLIENT_DB")
									) LEFT JOIN "OPTIMA"."import"."VPP43" T7 ON (T2."MECHANIC_CODE" = T7."SELLER_CODE")
									AND (T2."CLIENT_DB" = T7."CLIENT_DB")
								) LEFT JOIN "OPTIMA"."import"."VPP73" T8 ON (
									(T2."REPAIR_GROUP" = T8."REPAIR_GROUP")
									AND (T2."MAKE_CD" = T8."MAKE_CD")
									)
								AND (T2."CLIENT_DB" = T8."CLIENT_DB")
							) LEFT JOIN "OPTIMA"."data"."GC_Marken" T9 ON (T1."MAKE_CD" = T9."Make")
							AND (T1."CLIENT_DB" = T9."Client_DB")
						) LEFT JOIN "OPTIMA"."import"."GLOBAL_MAKE" T10 ON (T1."MAKE_CD" = T10."GLOBAL_MAKE_CD")
						AND (T1."CLIENT_DB" = T10."CLIENT_DB")
					) LEFT JOIN "OPTIMA"."import"."VEHICLE" T13 ON (
						(T13."CHASSIS_NUMBER" = T1."CHASSIS_NUMBER")
						AND (T13."BASIS_NUMBER" = T1."BASIS_NUMBER")
						)
					AND (T13."CLIENT_DB" = T1."CLIENT_DB")
				) LEFT JOIN "OPTIMA"."import"."VPP5Q" T11 ON (
					(T13."MODEL_LINE" = T11."MODEL_LINE")
					AND (T13."MAKE_CD" = T11."MAKE_CD")
					)
				AND (T13."CLIENT_DB" = T11."CLIENT_DB")
			) LEFT JOIN "OPTIMA"."data"."GC_Department" T12 ON (
				((left(T1."DEPARTMENT", 2)) = T12."Standort")
				AND (T1."CLIENT_DB" = T12."Hauptbetrieb")
				)
			OR (
				((left(T1."DEPARTMENT", 1)) = T12."Standort")
				AND (T1."CLIENT_DB" = T12."Hauptbetrieb")
				)
		)
	WHERE (
			(
				(
					(T1."STATUS" IN ('30', '40', '42', '43', '44'))
					AND (T1."INVOICE_DATE" = convert(DATETIME, '1800-01-01 00:00:00.000'))
					)
				AND (T1."ORDER_DATE" >= convert(DATETIME, '2020-01-01 00:00:00.000'))
				)
			AND (T2."ORDER_LINETYPE" <> '2')
			)
		-- order by "Order Number_ori_2" asc
	) D1
