SELECT "Invoice Type",
	"Type_Invoice_Types",
	"Description_Invoice_Types",
	"Invoice Number_2",
	"Subsidiary",
	"Paying Customer",
	"Order Customer",
	"Invoice Date",
	"Service Date",
	"Is Canceled",
	"Cancelation Number",
	"Cancelation Date",
	"Cancelation Employee",
	"Is Own Vehicle",
	"Is Credit",
	"Credit Invoice Type",
	"Credit Invoice Number",
	"Odometer Reading",
	"Creating Employee",
	"Internal Cost Account",
	"Vehicle Number",
	"Full Vat Basevalue",
	"Full Vat Percentage",
	"Full Vat Value",
	"Reduced Vat Basevalue",
	"Reduced Vat Percentage",
	"Reduced Vat Value",
	"Used Part Vat Value",
	"Job Amount Net",
	"Job Amount Gross",
	"Job Rebate",
	"Part Amount Net",
	"Part Amount Gross",
	"Part Rebate",
	"Part Disposal",
	"Total Gross",
	"Total Net",
	"Inv_Type_Invoice_Number",
	"Accounting Date",
	"Document Type",
	"Document Number",
	"Position In Document",
	"Customer Number",
	"Nominal Account Number",
	"Is Balanced",
	"Clearing Number",
	"Document Date",
	"Posted Value",
	"Debit Or Credit",
	"Posted Count",
	"Branch Number",
	"Customer Contra Account",
	"Nominal Contra Account",
	"Contra Account Text",
	"Account Form Page Number",
	"Account Form Page Line",
	"Serial Number Each Month",
	"Employee Number",
	"Invoice Date_falsch",
	"Invoice Number",
	"Dunning Level",
	"Last Dunning Date",
	"Journal Page",
	"Journal Line",
	"Cash Discount",
	"Term Of Payment",
	"Posting Text",
	"Vehicle Reference",
	"Vat Id Number",
	"Account Statement Number",
	"Account Statement Page",
	"Vat Key",
	"Days For Cash Discount",
	"Day Of Actual Accounting",
	"Skr51 Branch",
	"Skr51 Make",
	"Skr51 Cost Center",
	"Skr51 Sales Channel",
	"Skr51 Cost Unit",
	"Previously Used Account No",
	"Free Form Accounting Text",
	"Free Form Document Text",
	"Hauptbetrieb",
	"Standort",
	"Accounting Date" AS "Invoice Date_accounting",
	"Employee Number_Employees",
	"Name_Employees",
	"Mechanic Number_Employees",
	"Salesman Number_Employees",
	"Is Business Executive_Employees",
	"Is Master Craftsman_Employees",
	"Serviceberater_Rg_Steller",
	"Customer Number_Customers_Suppliers",
	"First Name_Customers_Suppliers",
	"Family Name_Customers_Suppliers",
	"Kunde",
	"Betrag",
	"Kostenstelle",
	"Skr51 Make" AS "Marke",
	"Kundenart",
	"KST_1_Stelle",
	"Umsatzart",
	"Auftragsart",
	'' AS "Gesch�ftsart",
	"Make Number",
	"Free Form Make Text",
	"Model Code",
	"Free Form Model Text",
	"Description_Makes",
	"Description_Models",
	"Description_Makes" AS "Fabrikat",
	"Description_Models" AS "Model_Detail",
	"Model",
	(0) AS "Umsatz Lohn",
	(0) AS "Umsatz FL",
	(0) AS "Einsatz FL",
	(0) AS "Umsatz Teile",
	(0) AS "Einsatz Teile",
	(0) AS "Umsatz MW",
	(0) AS "Einsatz MW",
	(0) AS "Umsatz Sonst.",
	(0) AS "Einsatz Sonst.",
	1 AS "DG_1",
	COUNT("Invoice Type") OVER (PARTITION BY "Order Number") AS "DG_2",
	1 / (COUNT("Invoice Type") OVER (PARTITION BY "Order Number")) AS "DG",
	"Fahrzeug",
	"Order Number",
	"Invoice_Date_Uhrzeit",
	"Order Number_Rg_Ausg",
	'' AS "Konto Nr",
	'' AS "Konto Bezeichnung",
	'' AS "Ebene1",
	'' AS "Ebene2",
	'' AS "Ebene3",
	'' AS "Ebene4",
	"Serviceberater_order_pos",
	"Serviceberater"
FROM (
	SELECT (
			CASE 
				WHEN (
						(
							CASE 
								WHEN (T1."invoice_type" = 5)
									THEN (((left((((T5."employee_number"))), 4)) + ' - ' + T5."name"))
								ELSE (T10."Serviceberater")
								END
							) IS NOT NULL
						)
					THEN (
							((left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9))) + ' - ' + (
								CASE 
									WHEN (T1."invoice_type" = 5)
										THEN (((left((((T5."employee_number"))), 4)) + ' - ' + T5."name"))
									ELSE (T10."Serviceberater")
									END
								) + ' - ' + (
								CASE 
									WHEN (T4."first_name" IS NULL)
										THEN ((left((((T4."customer_number"))), 7)) + ' - ' + T4."family_name")
									ELSE ((left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name")
									END
								)
							)
				ELSE (
						((left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9))) + ' - ' + (
							CASE 
								WHEN (T4."first_name" IS NULL)
									THEN ((left((((T4."customer_number"))), 7)) + ' - ' + T4."family_name")
								ELSE ((left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name")
								END
							)
						)
				END
			) AS "Order Number",
		CASE 
			WHEN (T1."invoice_type" = 5)
				THEN (((left((((T5."employee_number"))), 4)) + ' - ' + T5."name"))
			ELSE (T10."Serviceberater")
			END AS "Serviceberater",
		T10."Serviceberater" AS "Serviceberater_order_pos",
		CASE 
			WHEN ((- 1 * datediff(day, (getdate()), ((convert(DATETIME, T1."invoice_date"))))) <= 4)
				THEN (
						(
							CASE 
								WHEN (
										(
											CASE 
												WHEN (T1."invoice_type" = 5)
													THEN (((left((((T5."employee_number"))), 4)) + ' - ' + T5."name"))
												ELSE (T10."Serviceberater")
												END
											) IS NOT NULL
										)
									THEN (
											((left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9))) + ' - ' + (
												CASE 
													WHEN (T1."invoice_type" = 5)
														THEN (((left((((T5."employee_number"))), 4)) + ' - ' + T5."name"))
													ELSE (T10."Serviceberater")
													END
												) + ' - ' + (
												CASE 
													WHEN (T4."first_name" IS NULL)
														THEN ((left((((T4."customer_number"))), 7)) + ' - ' + T4."family_name")
													ELSE ((left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name")
													END
												)
											)
								ELSE (
										((left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9))) + ' - ' + (
											CASE 
												WHEN (T4."first_name" IS NULL)
													THEN ((left((((T4."customer_number"))), 7)) + ' - ' + T4."family_name")
												ELSE ((left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name")
												END
											)
										)
								END
							)
						)
			ELSE NULL
			END AS "Order Number_Rg_Ausg",
		(convert(DATETIME, T1."invoice_date")) AS "Invoice_Date_Uhrzeit",
		(left((left((((T1."vehicle_number"))), 5)) + ' - ' + T9."description", 100)) AS "Fahrzeug",
		(left((ucase(T9."description")), 3)) AS "Model",
		T9."description" AS "Description_Models",
		T8."description" AS "Description_Makes",
		T7."free_form_model_text" AS "Free Form Model Text",
		T7."model_code" AS "Model Code",
		T7."free_form_make_text" AS "Free Form Make Text",
		T7."make_number" AS "Make Number",
		CASE 
			WHEN (T1."invoice_type" = 5)
				THEN ('Teile')
			ELSE ('Service')
			END AS "Auftragsart",
		CASE 
			WHEN (T1."invoice_type" = 6)
				THEN ('GWL')
			WHEN (
					(T1."invoice_type" = 4)
					OR (T4."customer_number" IN (1000000, 1000001, 1000002, 1000003, 1000010))
					)
				THEN ('intern')
			ELSE ('extern')
			END AS "Umsatzart",
		(left(((rtrim((((T3."skr51_cost_center"))))) + ' - ' + T6."skr51_cost_center_name"), 1)) AS "KST_1_Stelle",
		(rtrim((((T3."skr51_sales_channel"))))) + ' - ' + T6."skr51_sales_channel_name" AS "Kundenart",
		T3."skr51_make" AS "Skr51 Make",
		(rtrim((((T3."skr51_cost_center"))))) + ' - ' + T6."skr51_cost_center_name" AS "Kostenstelle",
		CASE 
			WHEN (T3."debit_or_credit" = 'H')
				THEN (((convert(FLOAT, T3."posted_value"))) / 100 * - 1)
			ELSE (((convert(FLOAT, T3."posted_value"))) / 100)
			END AS "Betrag",
		CASE 
			WHEN (T4."first_name" IS NULL)
				THEN ((left((((T4."customer_number"))), 7)) + ' - ' + T4."family_name")
			ELSE ((left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name")
			END AS "Kunde",
		T4."family_name" AS "Family Name_Customers_Suppliers",
		T4."first_name" AS "First Name_Customers_Suppliers",
		T4."customer_number" AS "Customer Number_Customers_Suppliers",
		(left((((T5."employee_number"))), 4)) + ' - ' + T5."name" AS "Serviceberater_Rg_Steller",
		T5."is_master_craftsman" AS "Is Master Craftsman_Employees",
		T5."is_business_executive" AS "Is Business Executive_Employees",
		T5."salesman_number" AS "Salesman Number_Employees",
		T5."mechanic_number" AS "Mechanic Number_Employees",
		T5."name" AS "Name_Employees",
		T5."employee_number" AS "Employee Number_Employees",
		T3."accounting_date" AS "Accounting Date",
		(((T3."branch_number"))) AS "Standort",
		T3."subsidiary_to_company_ref" AS "Hauptbetrieb",
		CASE 
			WHEN (T3."skr51_cost_unit" BETWEEN 1 AND 49)
				THEN ('Neuwagen')
			WHEN (T3."skr51_cost_unit" BETWEEN 50 AND 59)
				THEN ('Gebrauchtwagen')
			WHEN (T3."skr51_cost_unit" BETWEEN 60 AND 69)
				THEN ('Teile & Zubeh�r')
			WHEN (T3."skr51_cost_unit" BETWEEN 70 AND 79)
				THEN ('Service')
			WHEN (T3."skr51_cost_unit" = 0)
				THEN ('Ohne Kostentr�ger')
			ELSE NULL
			END AS "Free Form Document Text",
		T4."zip_code" AS "Free Form Accounting Text",
		T3."previously_used_account_no" AS "Previously Used Account No",
		T3."skr51_cost_unit" AS "Skr51 Cost Unit",
		T3."skr51_sales_channel" AS "Skr51 Sales Channel",
		T3."skr51_cost_center" AS "Skr51 Cost Center",
		T3."skr51_branch" AS "Skr51 Branch",
		T3."day_of_actual_accounting" AS "Day Of Actual Accounting",
		T3."days_for_cash_discount" AS "Days For Cash Discount",
		T3."vat_key" AS "Vat Key",
		T3."account_statement_page" AS "Account Statement Page",
		T3."account_statement_number" AS "Account Statement Number",
		T3."vat_id_number" AS "Vat Id Number",
		T3."vehicle_reference" AS "Vehicle Reference",
		T3."posting_text" AS "Posting Text",
		T3."term_of_payment" AS "Term Of Payment",
		T3."cash_discount" AS "Cash Discount",
		T3."journal_line" AS "Journal Line",
		T3."journal_page" AS "Journal Page",
		T3."last_dunning_date" AS "Last Dunning Date",
		T3."dunning_level" AS "Dunning Level",
		T3."invoice_number" AS "Invoice Number",
		T3."invoice_date" AS "Invoice Date_falsch",
		T3."employee_number" AS "Employee Number",
		T3."serial_number_each_month" AS "Serial Number Each Month",
		T3."account_form_page_line" AS "Account Form Page Line",
		T3."account_form_page_number" AS "Account Form Page Number",
		CASE 
			WHEN (T3."nominal_account_number" = 5701)
				THEN ((rtrim((((T3."nominal_account_number"))))) + '_' + (rtrim((((T3."skr51_cost_center"))))))
			ELSE ((rtrim((((T3."nominal_account_number"))))))
			END AS "Contra Account Text",
		T3."nominal_contra_account" AS "Nominal Contra Account",
		T3."customer_contra_account" AS "Customer Contra Account",
		T3."branch_number" AS "Branch Number",
		T3."posted_count" AS "Posted Count",
		T3."debit_or_credit" AS "Debit Or Credit",
		(convert(FLOAT, T3."posted_value")) AS "Posted Value",
		T3."document_date" AS "Document Date",
		T3."clearing_number" AS "Clearing Number",
		T3."is_balanced" AS "Is Balanced",
		T3."nominal_account_number" AS "Nominal Account Number",
		T3."customer_number" AS "Customer Number",
		T3."position_in_document" AS "Position In Document",
		T3."document_number" AS "Document Number",
		T3."document_type" AS "Document Type",
		(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) AS "Inv_Type_Invoice_Number",
		T1."total_net" AS "Total Net",
		T1."total_gross" AS "Total Gross",
		T1."part_disposal" AS "Part Disposal",
		T1."part_rebate" AS "Part Rebate",
		T1."part_amount_gross" AS "Part Amount Gross",
		T1."part_amount_net" AS "Part Amount Net",
		T1."job_rebate" AS "Job Rebate",
		T1."job_amount_gross" AS "Job Amount Gross",
		T1."job_amount_net" AS "Job Amount Net",
		T1."used_part_vat_value" AS "Used Part Vat Value",
		T1."reduced_vat_value" AS "Reduced Vat Value",
		T1."reduced_vat_percentage" AS "Reduced Vat Percentage",
		T1."reduced_vat_basevalue" AS "Reduced Vat Basevalue",
		T1."full_vat_value" AS "Full Vat Value",
		T1."full_vat_percentage" AS "Full Vat Percentage",
		T1."full_vat_basevalue" AS "Full Vat Basevalue",
		T1."vehicle_number" AS "Vehicle Number",
		T1."internal_cost_account" AS "Internal Cost Account",
		T1."creating_employee" AS "Creating Employee",
		T1."odometer_reading" AS "Odometer Reading",
		T1."credit_invoice_number" AS "Credit Invoice Number",
		T1."credit_invoice_type" AS "Credit Invoice Type",
		T1."is_credit" AS "Is Credit",
		T1."is_own_vehicle" AS "Is Own Vehicle",
		T1."cancelation_employee" AS "Cancelation Employee",
		T1."cancelation_date" AS "Cancelation Date",
		T1."cancelation_number" AS "Cancelation Number",
		T1."is_canceled" AS "Is Canceled",
		T1."service_date" AS "Service Date",
		T1."invoice_date" AS "Invoice Date",
		T1."order_customer" AS "Order Customer",
		T1."paying_customer" AS "Paying Customer",
		T1."subsidiary" AS "Subsidiary",
		T1."invoice_number" AS "Invoice Number_2",
		T2."description" AS "Description_Invoice_Types",
		T2."type" AS "Type_Invoice_Types",
		T1."invoice_type" AS "Invoice Type"
	FROM (
		(
			(
				(
					(
						(
							(
								(
									(
										"dbo"."invoices" T1 LEFT JOIN "dbo"."invoice_types" T2 ON T2."type" = T1."invoice_type"
										) LEFT JOIN "dbo"."journal_accountings" T3 ON T3."invoice_number" = T1."invtype_invnr"
									) LEFT JOIN "dbo"."customers_suppliers" T4 ON T4."customer_number" = T1."paying_customer"
								) LEFT JOIN "dbo"."employees" T5 ON T1."creating_employee" = T5."employee_number"
							) LEFT JOIN "dbo"."accounts_characteristics" T6 ON (
								(
									(
										(
											(T6."skr51_branch" = T3."skr51_branch")
											AND (T6."skr51_make" = T3."skr51_make")
											)
										AND (T6."skr51_cost_center" = T3."skr51_cost_center")
										)
									AND (T6."skr51_sales_channel" = T3."skr51_sales_channel")
									)
								AND (T6."skr51_cost_unit" = T3."skr51_cost_unit")
								)
							AND (T6."subsidiary_to_company_ref" = T3."subsidiary_to_company_ref")
						) LEFT JOIN "dbo"."vehicles" T7 ON T1."vehicle_number" = T7."internal_number"
					) LEFT JOIN "dbo"."makes" T8 ON T7."make_number" = T8."make_number"
				) LEFT JOIN "dbo"."models" T9 ON (T7."make_number" = T9."make_number")
				AND (T7."model_code" = T9."model_code")
			) LEFT JOIN "ims"."Serviceberater_Rechnung" T10 ON T1."invtype_invnr" = T10."invtype_invnr"
		)
	WHERE (
			(
				(
					(
						(T1."invoice_type" BETWEEN 2 AND 6)
						AND ((left((((T3."nominal_account_number"))), 1)) IN ('4', '5', '7', '8'))
						)
					AND (T1."invoice_date" >= convert(DATE, '2019-01-01'))
					)
				AND (T1."is_canceled" <> 1)
				)
			AND (((len((rtrim((((T3."nominal_account_number"))))) + 'Z') - 1)) = 4)
			)
		-- order by "Order Number" asc,"Document Number" asc,"Position In Document" asc,"Inv_Type_Invoice_Number" asc
	) D1
