SELECT "Order Number_ori",
	"Order Position",
	"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",
	"Code_Labour_Type",
	"Description_Labour_Type",
	'1' AS "Hauptbetrieb",
	"Standort",
	"Name_Serviceberater",
	"Serviceberater",
	"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("Subsidiary") OVER (PARTITION BY "Order Number_ori") AS "DG_2",
	"Order Number_Labours",
	"Order Position_Labours",
	"Net Price In Order",
	"Text Line",
	"Order Number_ben_AW",
	"Summe Duration Minutes_ben_AW",
	c57 / (COUNT("Subsidiary") OVER (PARTITION BY "Order Number_ori")) AS "ben. Std",
	"Net Price In Order" AS "Umsatz Lohn"
FROM (
	SELECT T1."order_number" AS "Order Number_ori",
		T2."net_price_in_order" AS "Net Price In Order",
		T9."Summe_Duration_Minutes" / 60 AS c57,
		T9."Summe_Duration_Minutes" AS "Summe Duration Minutes_ben_AW",
		T9."order_number" AS "Order Number_ben_AW",
		T2."text_line" AS "Text Line",
		T2."order_position" AS "Order Position_Labours",
		T2."order_number" AS "Order Number_Labours",
		(left((ucase(T8."description")), 3)) AS "Model",
		T8."description" AS "Description_Models",
		T7."description" AS "Description_Makes",
		T7."make_number" AS "Make Number_Makes",
		T6."make_number" AS "Make Number_Vehicle",
		T6."license_plate" AS "License Plate_Vehicle",
		T6."vin" AS "Vin_Vehicle",
		T6."internal_number" AS "Internal Number_Vehicle",
		CASE 
			WHEN (T6."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)) + ' - ' + T4."name")
								END
							) + ' - ' + T5."family_name" + ' - ' + (convert(VARCHAR(50), year(T1."order_date")) + '-' + convert(VARCHAR(50), month(T1."order_date")) + '-' + convert(VARCHAR(50), day(T1."order_date"))) + ' - ' + T6."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)) + ' - ' + T4."name")
							END
						) + ' - ' + T5."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((((T5."customer_number"))), 7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" AS "Kunde",
		T5."family_name" AS "Family Name_Cust",
		T5."first_name" AS "First Name_Cust",
		T5."customer_number" AS "Customer Number_Cust",
		T3."code" + ' - ' + T3."description" AS "Umsatzart",
		CASE 
			WHEN (T1."order_taking_employee_no" = 0)
				THEN ('SB fehlt')
			ELSE ((left((((T1."order_taking_employee_no"))), 4)) + ' - ' + T4."name")
			END AS "Serviceberater",
		T4."name" AS "Name_Serviceberater",
		(((T1."subsidiary"))) AS "Standort",
		T3."description" AS "Description_Labour_Type",
		T3."code" AS "Code_Labour_Type",
		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."labour_type" AS "Labour Type",
		T1."order_position" AS "Order Position"
	FROM (
		(
			(
				(
					(
						(
							(
								(
									"dbo"."order_positions" T1 LEFT JOIN "dbo"."labours" T2 ON T1."order_number" = T2."order_number"
									) LEFT JOIN "dbo"."labour_types" T3 ON T3."code" = T2."labour_type"
								) LEFT JOIN "dbo"."employees" T4 ON T1."order_taking_employee_no" = T4."employee_number"
							) LEFT JOIN "dbo"."customers_suppliers" T5 ON T1."order_customer" = T5."customer_number"
						) LEFT JOIN "dbo"."vehicles" T6 ON T1."vehicle_number" = T6."internal_number"
					) LEFT JOIN "dbo"."makes" T7 ON T6."make_number" = T7."make_number"
				) LEFT JOIN "dbo"."models" T8 ON (T6."make_number" = T8."make_number")
				AND (T6."model_code" = T8."model_code")
			) LEFT JOIN "ims"."ben_AW_Order_Number" T9 ON T2."order_number" = T9."order_number"
		)
	WHERE (
			(
				(
					(T2."invoice_number" IS NULL)
					AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
					)
				AND (T2."net_price_in_order" IS NOT NULL)
				)
			AND (T2."labour_type" <> 'F')
			)
		-- order by "Order Number_ori" asc
	) D1
