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