SELECT (
		CASE 
			WHEN (T1."Make" IS NULL)
				THEN ('00')
			ELSE (T1."Make")
			END
		) + '-' + (
		CASE 
			WHEN (
					(((rtrim(T2."ACCT_NR"))) LIKE '2%')
					AND (T3."SITE" IS NULL)
					)
				THEN ('01')
			ELSE ((right('00' + (left(T3."SITE", 2)), 2)))
			END
		) + '-' + ((rtrim(T2."ACCT_NR"))) + '-' + (
		CASE 
			WHEN (
					(T3."DEPARTMENT" IS NULL)
					OR ((rtrim(T3."DEPARTMENT")) = '')
					)
				THEN ('00')
			ELSE ((rtrim(T3."DEPARTMENT")))
			END
		) + '-' + (
		CASE 
			WHEN (
					(T3."DESTINATION" IS NULL)
					OR ((rtrim(T3."DESTINATION")) = '')
					)
				THEN ('00')
			ELSE ((right('00' + (rtrim(T3."DESTINATION")), 2)))
			END
		) + '-' + (
		(
			rtrim(CASE 
					WHEN (
							(T4."MODEL_LINE" IS NOT NULL)
							AND (T4."MODEL_LINE" <> '')
							)
						THEN (T4."MODEL_LINE")
					WHEN (
							(T3."MODEL_LINE" IS NOT NULL)
							AND (T3."MODEL_LINE" <> '')
							)
						THEN (T3."MODEL_LINE")
					WHEN (
							(T3."PRODUCT_GROUP" IS NOT NULL)
							AND (T3."PRODUCT_GROUP" <> '')
							)
						THEN (T3."PRODUCT_GROUP")
					WHEN (
							(T3."REPAIR_GROUP" IS NOT NULL)
							AND (T3."REPAIR_GROUP" <> '')
							)
						THEN (T3."REPAIR_GROUP")
					ELSE ('00')
					END)
			)
		) AS "Acct Nr",
	T2."LEDGER_ACCTS_NAME" AS "Ledger Accts Name",
	T2."LEDGER_ACCTS_NAME2" AS "Ledger Accts Name2",
	T2."HANDLER" AS "Handler_2",
	T2."DEPT_SPLIT" AS "Dept Split",
	T2."TYPE_ACCTT" AS "Type Acctt",
	T3."ACCT_NO" AS "Acct No",
	T3."BOOKKEEP_DATE" AS "Bookkeep Date",
	T3."BOOKKEEP_PERIOD" AS "Bookkeep Period",
	T3."DOCUMENT_NO" AS "Document No",
	T3."ORIGIN" AS "Origin",
	T3."STATUS" AS "Status",
	T3."DEBIT_AMOUNT" AS "Debit Amount",
	T3."CREDIT_AMOUNT" AS "Credit Amount",
	T3."DEBIT_QUANTITY" AS "Debit Quantity",
	T3."CREDIT_QUANTITY" AS "Credit Quantity",
	T3."AA_TRTYPE" AS "Aa Trtype",
	T3."DEPARTMENT" AS "Department",
	T3."STOCK" AS "Stock",
	T3."MAKE_FAMILY" AS "Make Family",
	T3."MAKE" AS "Make",
	T3."VEHICLE_TYPE" AS "Vehicle Type",
	T3."MODEL_LINE" AS "Model Line",
	T3."FACTORY_MODEL" AS "Factory Model",
	T3."WORKSHOP_MODEL" AS "Workshop Model",
	T3."PRODUCT_GROUP" AS "Product Group",
	T3."REPAIR_GROUP" AS "Repair Group",
	T3."KIT_GROUP" AS "Kit Group",
	T3."TIME_CODE" AS "Time Code",
	T3."INT_VOUCHER_NO" AS "Int Voucher No",
	T3."BALANCING_MARK" AS "Balancing Mark",
	T3."USED_VEH_DEST_CODE" AS "Used Veh Dest Code",
	T3."USE_OF_VEHICLE" AS "Use Of Vehicle",
	T3."ACCT_NO_NEXT_CHART" AS "Acct No Next Chart",
	T5."REFERENCE_IDENT" AS "Reference Ident",
	T5."TRANSACT_DATE" AS "Transact Date",
	T5."HANDLER" AS "Handler",
	T5."PROGRAM" AS "Program",
	T5."FUNCTION_CODE" AS "Function Code",
	T5."MODUL" AS "Modul",
	T5."DOCUMENT_KEY" AS "Document Key",
	T5."COMMENT" AS "Comment",
	T6."DEPARTMENT_TYPE_ID" AS "Department Type Id",
	T6."DESCRIPTION" AS "Description_2",
	T6."DEPARTMENT_GROUP" AS "Department Group",
	T7."AA_TRTYPE_ID" AS "Aa Trtype Id",
	T7."DESCRIPTION" AS "Description",
	T7."OWN_DESCRIPTION" AS "Own Description",
	'1' AS "Rechtseinheit",
	'01' AS "Betrieb",
	CASE 
		WHEN (T1."Make" IS NULL)
			THEN ('00')
		ELSE (T1."Make")
		END AS "Marke",
	T3."DEBIT_AMOUNT" + T3."CREDIT_AMOUNT" AS "Betrag",
	'1' AS "Mandant",
	(T3."DEBIT_QUANTITY" + T3."CREDIT_QUANTITY") AS "Menge",
	CASE 
		WHEN ((- 1 * datediff(day, (getdate()), T3."BOOKKEEP_DATE")) <= 120)
			THEN (T5."COMMENT")
		ELSE NULL
		END AS "Text",
	(left(T3."ACCT_NO", 1)) AS "Susa_2",
	T8."ORDER_NUMBER" AS "Order Number",
	T8."LINE_NUMBER" AS "Line Number",
	T8."INV_TIME" AS "Inv Time",
	T8."INV_TIME_INT" AS "Inv Time Int",
	T8."MAKE_TIME_UNIT" AS "Make Time Unit",
	T3."SITE" AS "Site",
	CASE 
		WHEN (T3."DEPARTMENT" = '    ')
			THEN ('00')
		ELSE (T3."DEPARTMENT")
		END AS "KST",
	T4."MODEL_LINE" AS "Model Line_vpp5q",
	T4."MOD_LIN_SPECIFY" AS "Mod Lin Specify_vpp5q",
	CASE 
		WHEN (T4."MODEL_LINE" IS NOT NULL)
			THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
		WHEN (
				(T3."MODEL_LINE" IS NOT NULL)
				AND (T3."MODEL_LINE" <> '')
				)
			THEN (T3."MODEL_LINE")
		ELSE ('00 - ohne')
		END AS "Kostentr�ger_mit_Bez_",
	(
		substring((
				CASE 
					WHEN (T4."MODEL_LINE" IS NOT NULL)
						THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
					WHEN (
							(T3."MODEL_LINE" IS NOT NULL)
							AND (T3."MODEL_LINE" <> '')
							)
						THEN (T3."MODEL_LINE")
					ELSE ('00 - ohne')
					END
				), 1, 2)
		) AS "Kostentr�ger",
	T9."CUSTOMER_NUMBER" AS "Customer Number",
	T10."CUSTOMER_GROUP" AS "Customer Group",
	CASE 
		WHEN (T3."DESTINATION" = 'FZ')
			THEN ('11')
		WHEN (
				(T3."DESTINATION" = '00')
				AND (T3."PRICE_CODE" <> '')
				)
			THEN (T3."PRICE_CODE")
		WHEN (
				(
					(T3."DESTINATION" = '00')
					AND (T3."PRICE_CODE" = '')
					)
				AND (T10."CUSTOMER_GROUP" <> '')
				)
			THEN (T10."CUSTOMER_GROUP")
		WHEN ((rtrim(T3."DESTINATION")) = '')
			THEN ('00')
		ELSE (T3."DESTINATION")
		END AS "Absatzkanal",
	T2."TYPE_ACCTT" AS "GuV_Bilanz",
	(left(T3."ACCT_NO", 1)) AS "Susa",
	CASE 
		WHEN (
				(
					(
						substring((
								CASE 
									WHEN (T3."DEPARTMENT" = '    ')
										THEN ('00')
									ELSE (T3."DEPARTMENT")
									END
								), 1, 1)
						) = '1'
					)
				AND (
					NOT (
						(
							substring((
									CASE 
										WHEN (T4."MODEL_LINE" IS NOT NULL)
											THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
										WHEN (
												(T3."MODEL_LINE" IS NOT NULL)
												AND (T3."MODEL_LINE" <> '')
												)
											THEN (T3."MODEL_LINE")
										ELSE ('00 - ohne')
										END
									), 1, 2)
							)
						) IN ('00')
					)
				)
			THEN ('Neuwagen')
		WHEN (
				(
					(
						substring((
								CASE 
									WHEN (T3."DEPARTMENT" = '    ')
										THEN ('00')
									ELSE (T3."DEPARTMENT")
									END
								), 1, 1)
						) = '2'
					)
				AND (
					NOT (
						(
							substring((
									CASE 
										WHEN (T4."MODEL_LINE" IS NOT NULL)
											THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
										WHEN (
												(T3."MODEL_LINE" IS NOT NULL)
												AND (T3."MODEL_LINE" <> '')
												)
											THEN (T3."MODEL_LINE")
										ELSE ('00 - ohne')
										END
									), 1, 2)
							)
						) IN ('00')
					)
				)
			THEN ('Gebrauchtwagen')
		ELSE ('Ohne Kostentr�ger')
		END AS "Ebene31",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (T1."Make" IS NULL)
							THEN ('00')
						ELSE (T1."Make")
						END
					) IN ('OP')
				)
			THEN ('OP')
		ELSE NULL
		END AS "Marke f�r Kostentr�ger",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (T1."Make" IS NULL)
							THEN ('00')
						ELSE (T1."Make")
						END
					) IN ('OP')
				)
			THEN (
					(
						CASE 
							WHEN (
									(
										CASE 
											WHEN (T1."Make" IS NULL)
												THEN ('00')
											ELSE (T1."Make")
											END
										) IN ('OP')
									)
								THEN ('OP')
							ELSE NULL
							END
						) + (
						CASE 
							WHEN (T4."MODEL_LINE" IS NOT NULL)
								THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
							WHEN (
									(T3."MODEL_LINE" IS NOT NULL)
									AND (T3."MODEL_LINE" <> '')
									)
								THEN (T3."MODEL_LINE")
							ELSE ('00 - ohne')
							END
						)
					)
		WHEN (
				(
					CASE 
						WHEN (T1."Make" IS NULL)
							THEN ('00')
						ELSE (T1."Make")
						END
					) IN ('VW')
				)
			THEN (
					(
						CASE 
							WHEN (
									(
										CASE 
											WHEN (T1."Make" IS NULL)
												THEN ('00')
											ELSE (T1."Make")
											END
										) IN ('OP')
									)
								THEN ('OP')
							ELSE NULL
							END
						) + (
						CASE 
							WHEN (T4."MODEL_LINE" IS NOT NULL)
								THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
							WHEN (
									(T3."MODEL_LINE" IS NOT NULL)
									AND (T3."MODEL_LINE" <> '')
									)
								THEN (T3."MODEL_LINE")
							ELSE ('00 - ohne')
							END
						)
					)
		ELSE (
				(
					CASE 
						WHEN (T4."MODEL_LINE" IS NOT NULL)
							THEN (T4."MODEL_LINE" + ' - ' + T4."MOD_LIN_SPECIFY")
						WHEN (
								(T3."MODEL_LINE" IS NOT NULL)
								AND (T3."MODEL_LINE" <> '')
								)
							THEN (T3."MODEL_LINE")
						ELSE ('00 - ohne')
						END
					)
				)
		END AS "Kostentr�ger_mit_Bez",
	'' AS "ACCT_Detail",
	'mit Marke' AS "Buchungen ohne Marke",
	'mit KST' AS "Buchungen ohne KST",
	'mit Absatzkanal' AS "Buchungen ohne Absatzkanal",
	T3."DESTINATION" AS "Destination",
	T11."Hauptbetrieb_ID" AS "Hauptbetrieb Id",
	T11."Hauptbetrieb_Name" AS "Hauptbetrieb Name",
	T11."Standort_ID" AS "Standort Id",
	T11."Standort_Name" AS "Standort Name",
	(ucase((rtrim(T3."STRATEGIC_AREA")))) AS "Strategic Area",
	T1."Make" AS "Marke_Strategic_Area",
	CASE 
		WHEN (
				(
					CASE 
						WHEN (T1."Make" IS NULL)
							THEN ('00')
						ELSE (T1."Make")
						END
					) IN ('00')
				)
			THEN ('00 - ohne Marke')
		ELSE (
				(
					CASE 
						WHEN (T1."Make" IS NULL)
							THEN ('00')
						ELSE (T1."Make")
						END
					) + ' - ' + T1."Description"
				)
		END AS "Marke_mit_Bez"
FROM "OPTIMA"."import"."ACCOUNT_INFO" T2,
	(
		(
			(
				(
					(
						"OPTIMA"."import"."ACCT_DOC_KEY" T3 LEFT JOIN "OPTIMA"."data"."GC_Marken" T1 ON T1."Strategic_Area" = T3."STRATEGIC_AREA"
						) LEFT JOIN "OPTIMA"."import"."vPP5Q" T4 ON (T3."MODEL_LINE" = T4."MODEL_LINE")
						AND (T3."MAKE" = T4."MAKE_CD")
					) LEFT JOIN "OPTIMA"."import"."DEPARTMENT_TYPE" T6 ON T3."DEPARTMENT" = T6."DEPARTMENT_TYPE_ID"
				) LEFT JOIN "OPTIMA"."import"."AA_TRTYPE" T7 ON T3."AA_TRTYPE" = T7."AA_TRTYPE_ID"
			) LEFT JOIN "OPTIMA"."data"."GC_Department" T11 ON (T3."CLIENT_DB" = T11."Hauptbetrieb")
			AND (T3."SITE" = T11."Site")
		),
	(
		(
			(
				"OPTIMA"."import"."ACCT_DOC_DATA" T5 LEFT JOIN "OPTIMA"."import"."ACCT_DOC_SALESCLAS" T9 ON T5."REFERENCE_IDENT" = T9."REFERENCE_IDENT"
				) LEFT JOIN "OPTIMA"."import"."ORDER_LINE" T8 ON (T8."ORDER_NUMBER" = T9."ORDER_NUMBER")
				AND (T8."LINE_NUMBER" = T9."ORDER_LINE_NUMBER")
			) LEFT JOIN "OPTIMA"."import"."CUSTOMER" T10 ON T10."CUSTOMER_NUMBER" = T9."CUSTOMER_NUMBER"
		)
WHERE (T2."ACCT_NR" = T3."ACCT_NO")
	AND (T3."UNIQUE_IDENT" = T5."REFERENCE_IDENT")
	AND (
		(T2."TYPE_ACCTT" = '1')
		AND (T3."BOOKKEEP_DATE" >= convert(DATETIME, '2019-01-01 00:00:00.000'))
		)
