SELECT DISTINCT "Order Number_ori",
	"Order Position",
	'' AS "Labour Type",
	"Subsidiary",
	"Is Invoiced",
	"Invoice Type",
	"Invoice Number",
	"Order Date",
	"Estimated Inbound Time",
	"Estimated Outbound Time",
	"Order Print Date",
	"Order Taking Employee No",
	"Order Delivery Employee No",
	"Vehicle Number",
	"Order Mileage",
	"Order Customer",
	"Paying Customer",
	"Holder Number",
	"Parts Rebate Group Sell",
	"Clearing Delay Type",
	'' AS "Code_Labour_Type",
	'' AS "Description_Labour_Type",
	'1' AS "Hauptbetrieb",
	"Standort",
	"Name_Serviceberater",
	"Serviceberater",
	'Teile' AS "Umsatzart",
	"Customer Number_Cust",
	"First Name_Cust",
	"Family Name_Cust",
	"Kunde",
	"Order Number",
	"Internal Number_Vehicle",
	"Vin_Vehicle",
	"License Plate_Vehicle",
	"Make Number_Vehicle",
	'Service' AS "Auftragsart",
	"Make Number_Makes",
	"Description_Makes",
	"Description_Makes" AS "Fabrikat",
	"Description_Models",
	"Model",
	1 AS "DG_1",
	COUNT('') OVER (PARTITION BY "Order Number_ori") AS "DG_2",
	"Part Number",
	"Stock No",
	"Stock Removal Date",
	"Amount",
	"Sum",
	"Parts Type",
	"Text Line",
	"Sum" AS "Teile"
FROM (
	SELECT T1."order_number" AS "Order Number_ori",
		T2."sum" AS "Sum",
		T2."text_line" AS "Text Line",
		T2."parts_type" AS "Parts Type",
		T2."amount" AS "Amount",
		T2."stock_removal_date" AS "Stock Removal Date",
		T2."stock_no" AS "Stock No",
		T2."part_number" AS "Part Number",
		(left((ucase(T7."description")), 3)) AS "Model",
		T7."description" AS "Description_Models",
		T6."description" AS "Description_Makes",
		T6."make_number" AS "Make Number_Makes",
		T5."make_number" AS "Make Number_Vehicle",
		T5."license_plate" AS "License Plate_Vehicle",
		T5."vin" AS "Vin_Vehicle",
		T5."internal_number" AS "Internal Number_Vehicle",
		CASE 
			WHEN (T5."license_plate" IS NOT NULL)
				THEN (
						(left((((T1."order_number"))), 6)) + ' - ' + (
							CASE 
								WHEN (T1."order_taking_employee_no" = 0)
									THEN ('SB fehlt')
								ELSE ((left((((T1."order_taking_employee_no"))), 4)) + ' - ' + T3."name")
								END
							) + ' - ' + T4."family_name" + ' - ' + (convert(VARCHAR(50), year(T1."order_date")) + '-' + convert(VARCHAR(50), month(T1."order_date")) + '-' + convert(VARCHAR(50), day(T1."order_date"))) + ' - ' + T5."license_plate"
						)
			ELSE (
					(left((((T1."order_number"))))) + ' - ' + (
						CASE 
							WHEN (T1."order_taking_employee_no" = 0)
								THEN ('SB fehlt')
							ELSE ((left((((T1."order_taking_employee_no"))), 4)) + ' - ' + T3."name")
							END
						) + ' - ' + T4."family_name" + ' - ' + (convert(VARCHAR(50), year(T1."order_date")) + '-' + convert(VARCHAR(50), month(T1."order_date")) + '-' + convert(VARCHAR(50), day(T1."order_date")))
					)
			END AS "Order Number",
		(left((((T4."customer_number"))), 7)) + ' - ' + T4."first_name" + ' ' + T4."family_name" AS "Kunde",
		T4."family_name" AS "Family Name_Cust",
		T4."first_name" AS "First Name_Cust",
		T4."customer_number" AS "Customer Number_Cust",
		CASE 
			WHEN (T1."order_taking_employee_no" = 0)
				THEN ('SB fehlt')
			ELSE ((left((((T1."order_taking_employee_no"))), 4)) + ' - ' + T3."name")
			END AS "Serviceberater",
		T3."name" AS "Name_Serviceberater",
		(((T1."subsidiary"))) AS "Standort",
		T1."clearing_delay_type" AS "Clearing Delay Type",
		T1."parts_rebate_group_sell" AS "Parts Rebate Group Sell",
		T1."holder_number" AS "Holder Number",
		T1."paying_customer" AS "Paying Customer",
		T1."order_customer" AS "Order Customer",
		T1."order_mileage" AS "Order Mileage",
		T1."vehicle_number" AS "Vehicle Number",
		T1."order_delivery_employee_no" AS "Order Delivery Employee No",
		T1."order_taking_employee_no" AS "Order Taking Employee No",
		T1."order_print_date" AS "Order Print Date",
		T1."estimated_outbound_time" AS "Estimated Outbound Time",
		T1."estimated_inbound_time" AS "Estimated Inbound Time",
		T1."order_date" AS "Order Date",
		T2."invoice_number" AS "Invoice Number",
		T2."invoice_type" AS "Invoice Type",
		T2."is_invoiced" AS "Is Invoiced",
		T1."subsidiary" AS "Subsidiary",
		T2."order_position" AS "Order Position"
	FROM (
		(
			(
				(
					(
						(
							"dbo"."order_positions" T1 LEFT JOIN "dbo"."parts" T2 ON T1."order_number" = T2."order_number"
							) LEFT JOIN "dbo"."employees" T3 ON T1."order_taking_employee_no" = T3."employee_number"
						) LEFT JOIN "dbo"."customers_suppliers" T4 ON T1."order_customer" = T4."customer_number"
					) LEFT JOIN "dbo"."vehicles" T5 ON T1."vehicle_number" = T5."internal_number"
				) LEFT JOIN "dbo"."makes" T6 ON T5."make_number" = T6."make_number"
			) LEFT JOIN "dbo"."models" T7 ON (T5."make_number" = T7."make_number")
			AND (T5."model_code" = T7."model_code")
		)
	WHERE (
			(
				(
					(T2."invoice_number" IS NULL)
					AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
					)
				AND (T2."sum" IS NOT NULL)
				)
			AND ('' <> 'F')
			)
	) D1
	-- order by "Order Number_ori" asc
