SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE   VIEW [transform_imr].[loc_belege_nw_gw_vk_stk_fibu]
AS
SELECT "Accounting Date" AS "Accounting Date",
	"Document Type" AS "Document Type",
	"Document Number" AS "Document Number",
	"Position In Document" AS "Position In Document",
	"Customer Number" AS "Customer Number",
	"Nominal Account Number" AS "Nominal Account Number",
	"Is Balanced" AS "Is Balanced",
	"Clearing Number" AS "Clearing Number",
	"Document Date" AS "Document Date",
	"Posted Value" AS "Posted Value",
	"Debit Or Credit" AS "Debit Or Credit",
	"Posted Count" AS "Posted Count",
	"Branch Number" AS "Branch Number",
	"Customer Contra Account" AS "Customer Contra Account",
	"Nominal Contra Account" AS "Nominal Contra Account",
	"Contra Account Text" AS "Contra Account Text",
	"Account Form Page Number" AS "Account Form Page Number",
	"Account Form Page Line" AS "Account Form Page Line",
	"Serial Number Each Month" AS "Serial Number Each Month",
	"Employee Number" AS "Employee Number",
	"Invoice Date_journal_accountings" AS "Invoice Date_journal_accountings",
	"Invoice Number" AS "Invoice Number",
	"Dunning Level" AS "Dunning Level",
	"Last Dunning Date" AS "Last Dunning Date",
	"Journal Page" AS "Journal Page",
	"Journal Line" AS "Journal Line",
	"Cash Discount" AS "Cash Discount",
	"Term Of Payment" AS "Term Of Payment",
	"Posting Text" AS "Posting Text",
	"Vehicle Reference" AS "Vehicle Reference",
	"Vat Id Number" AS "Vat Id Number",
	"Account Statement Number" AS "Account Statement Number",
	"Account Statement Page" AS "Account Statement Page",
	"Vat Key" AS "Vat Key",
	"Days For Cash Discount" AS "Days For Cash Discount",
	"Day Of Actual Accounting" AS "Day Of Actual Accounting",
	"Skr51 Branch" AS "Skr51 Branch",
	"Skr51 Make" AS "Skr51 Make",
	"Skr51 Cost Center" AS "Skr51 Cost Center",
	"Skr51 Sales Channel" AS "Skr51 Sales Channel",
	"Skr51 Cost Unit" AS "Skr51 Cost Unit",
	"Previously Used Account No" AS "Previously Used Account No",
	"Free Form Accounting Text" AS "Free Form Accounting Text",
	"Free Form Document Text" AS "Free Form Document Text",
	"Nom_Account_Is Profit Loss Account" AS "Nom_Account_Is Profit Loss Account",
	"Rechtseinheit" AS "Rechtseinheit",
	"Betrieb" AS "Betrieb",
	"Bookkeep Date" AS "Bookkeep Date",
	"Marke" AS "Marke",
	"Text" AS "Text",
	"Mandant" AS "Mandant",
	"Betrag_ori" AS "Betrag_ori",
	"Konto_mit_Bezeichnung" AS "Konto_mit_Bezeichnung",
	"Menge" AS "Menge",
	"Stellen Cost Center" AS "Stellen Cost Center",
	"KST" AS "KST",
	"Stellen Sales Channel" AS "Stellen Sales Channel",
	"Absatzkanal" AS "Absatzkanal",
	"Stellen Cost Unit" AS "Stellen Cost Unit",
	"Kostenträger_mit_Null" AS "Kostenträger_mit_Null",
	"Kostenträger" AS "Kostenträger",
	"GuV_Bilanz" AS "GuV_Bilanz",
	"Susa" AS "Susa",
	"Acct Nr_ori" AS "Acct Nr_ori",
	"Document Type In Journal" AS "Document Type In Journal",
	"Document Type Description" AS "Document Type Description",
	"FZG_ja_nein" AS "FZG_ja_nein",
	"Summe_FZG_ja_nein" AS "Summe_FZG_ja_nein",
	"FZG_Geschäft ja/nein" AS "FZG_Geschäft ja/nein",
	"Invoice Type_Invoices" AS "Invoice Type_Invoices",
	"Invoice Number_Invoices" AS "Invoice Number_Invoices",
	"Invoice Date_Invoices" AS "Invoice Date_Invoices",
	"Is Canceled_Invoices" AS "Is Canceled_Invoices",
	"Vehicle Number_Invoices" AS "Vehicle Number_Invoices",
	"Vehicle_Number_Max" AS "Vehicle_Number_Max",
	"Invoice_Date_1" AS "Invoice_Date_1",
	"Invoice Date_FZG" AS "Invoice Date_FZG",
	"Invoice Date" AS "Invoice Date",
	SUM("Betrag_ori") OVER (PARTITION BY "Vehicle Reference") AS "Summe Betrag",
	"Internal Number_Vehicles" AS "Internal Number_Vehicles",
	"Vin_Vehicles" AS "Vin_Vehicles",
	"Dealer Vehicle Type" AS "Dealer Vehicle Type",
	"Fahrzeugtyp" AS "Fahrzeugtyp",
	"Fahrzeugart" AS "Fahrzeugart",
	"Make Number_Vehicles" AS "Make Number_Vehicles",
	"Description_Make" AS "Description_Make",
	"Fabrikat" AS "Fabrikat",
	"Model Code_Models" AS "Model Code_Models",
	"Model_Detail" AS "Model_Detail",
	"Model" AS "Model",
	"Owner Number_Vehicles" AS "Owner Number_Vehicles",
	"Holder Number_Vehicles" AS "Holder Number_Vehicles",
	"First Name_Owner_Customer" AS "First Name_Owner_Customer",
	"Family Name_Owner_Customer" AS "Family Name_Owner_Customer",
	"First Name_Holder_Customer" AS "First Name_Holder_Customer",
	"Family Name_Holder_Customer" AS "Family Name_Holder_Customer",
	"Kunde" AS "Kunde",
	"Sales_Channel_FZG" AS "Sales_Channel_FZG",
	"Kundenart_Zahl" AS "Kundenart_Zahl",
	"Kundenart" AS "Kundenart",
	"Hauptbetrieb" AS "Hauptbetrieb",
	"Standort_alt" AS "Standort_alt",
	"Salesman Number_Vehicles" AS "Salesman Number_Vehicles",
	"Employee Number_Employees_Salesman" AS "Employee Number_Employees_Salesman",
	"Name_Employees_Salesman" AS "Name_Employees_Salesman",
	"Verkäufer" AS "Verkäufer",
	"Standort_FZG_Verkauf" AS "Standort_FZG_Verkauf",
	"Standort_Zahl" AS "Standort_Zahl",
	"Standort" AS "Standort",
	"FZG" AS "FZG",
	"Konto" AS "Konto",
	"Body Paint Code" AS "Body Paint Code",
	"Body Paint Description" AS "Body Paint Description",
	"Farbe" AS "Farbe",
	"Erlös FZG" AS "Erlös FZG",
	"Nachlass" AS "Nachlass",
	"VAK FZG" AS "VAK FZG",
	"Erlös Zulass." AS "Erlös Zulass.",
	"VAK Zulass." AS "VAK Zulass.",
	"Erlös Gar." AS "Erlös Gar.",
	"VAK Gar." AS "VAK Gar.",
	"Erlös Nachr./Aufb." AS "Erlös Nachr./Aufb.",
	"VAK Nachr./Aufb." AS "VAK Nachr./Aufb.",
	"VAK Fertigm." AS "VAK Fertigm.",
	"Erlös Überf." AS "Erlös Überf.",
	"VAK Überf." AS "VAK Überf.",
	"Erlös Sonst._8900" AS "Erlös Sonst._8900",
	"Erlös Sonst." AS "Erlös Sonst.",
	"VAK Sonst." AS "VAK Sonst.",
	"VAK intern" AS "VAK intern",
	"Erlös Prov." AS "Erlös Prov.",
	"Boni/VK-Hilfen" AS "Boni/VK-Hilfen",
	"Erlös ges." AS "Erlös ges.",
	"VAK ges." AS "VAK ges.",
	"Menge_1" AS "Menge_1",
	"Menge_2" AS "Menge_2",
	"Betrag" AS "Betrag",
	"Vehicle Reference_berechnet" AS "Vehicle Reference_berechnet",
	"Acct Nr" AS "Acct Nr",
	"Dealer Vehicle Type_Vehicles" AS "Dealer Vehicle Type_Vehicles",
	"Dealer Vehicle Number_Vehicles" AS "Dealer Vehicle Number_Vehicles"
FROM (
	SELECT "Accounting Date" AS "Accounting Date",
		"Document Type" AS "Document Type",
		"Document Number" AS "Document Number",
		"Position In Document" AS "Position In Document",
		"Customer Number" AS "Customer Number",
		"Nominal Account Number" AS "Nominal Account Number",
		"Is Balanced" AS "Is Balanced",
		"Clearing Number" AS "Clearing Number",
		"Document Date" AS "Document Date",
		"Posted Value" AS "Posted Value",
		"Debit Or Credit" AS "Debit Or Credit",
		"Posted Count" AS "Posted Count",
		"Branch Number" AS "Branch Number",
		"Customer Contra Account" AS "Customer Contra Account",
		"Nominal Contra Account" AS "Nominal Contra Account",
		"Contra Account Text" AS "Contra Account Text",
		"Account Form Page Number" AS "Account Form Page Number",
		"Account Form Page Line" AS "Account Form Page Line",
		"Serial Number Each Month" AS "Serial Number Each Month",
		"Employee Number" AS "Employee Number",
		"Invoice Date_journal_accountings" AS "Invoice Date_journal_accountings",
		"Invoice Number" AS "Invoice Number",
		"Dunning Level" AS "Dunning Level",
		"Last Dunning Date" AS "Last Dunning Date",
		"Journal Page" AS "Journal Page",
		"Journal Line" AS "Journal Line",
		"Cash Discount" AS "Cash Discount",
		"Term Of Payment" AS "Term Of Payment",
		"Posting Text" AS "Posting Text",
		"Vehicle Reference" AS "Vehicle Reference",
		"Vat Id Number" AS "Vat Id Number",
		"Account Statement Number" AS "Account Statement Number",
		"Account Statement Page" AS "Account Statement Page",
		"Vat Key" AS "Vat Key",
		"Days For Cash Discount" AS "Days For Cash Discount",
		"Day Of Actual Accounting" AS "Day Of Actual Accounting",
		"Skr51 Branch" AS "Skr51 Branch",
		"Skr51 Make" AS "Skr51 Make",
		"Skr51 Cost Center" AS "Skr51 Cost Center",
		"Skr51 Sales Channel" AS "Skr51 Sales Channel",
		"Skr51 Cost Unit" AS "Skr51 Cost Unit",
		"Previously Used Account No" AS "Previously Used Account No",
		"Free Form Accounting Text" AS "Free Form Accounting Text",
		"Free Form Document Text" AS "Free Form Document Text",
		"Nom_Account_Is Profit Loss Account" AS "Nom_Account_Is Profit Loss Account",
		"Rechtseinheit" AS "Rechtseinheit",
		"Betrieb" AS "Betrieb",
		"Bookkeep Date" AS "Bookkeep Date",
		"Marke" AS "Marke",
		"Text" AS "Text",
		"Mandant" AS "Mandant",
		"Betrag_ori" AS "Betrag_ori",
		"Konto_mit_Bezeichnung" AS "Konto_mit_Bezeichnung",
		"Menge" AS "Menge",
		"Stellen Cost Center" AS "Stellen Cost Center",
		"KST" AS "KST",
		"Stellen Sales Channel" AS "Stellen Sales Channel",
		"Absatzkanal" AS "Absatzkanal",
		"Stellen Cost Unit" AS "Stellen Cost Unit",
		"Kostenträger_mit_Null" AS "Kostenträger_mit_Null",
		"Kostenträger" AS "Kostenträger",
		"GuV_Bilanz" AS "GuV_Bilanz",
		"Susa" AS "Susa",
		"Acct Nr_ori" AS "Acct Nr_ori",
		"Document Type In Journal" AS "Document Type In Journal",
		"Document Type Description" AS "Document Type Description",
		"FZG_ja_nein" AS "FZG_ja_nein",
		SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Summe_FZG_ja_nein",
		CASE 
			WHEN (SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet") > 0)
				THEN (N'FZG-Geschäft')
			ELSE (N'Buchungen ohne FZG-Geschäft')
			END AS "FZG_Geschäft ja/nein",
		"Invoice Type_Invoices" AS "Invoice Type_Invoices",
		"Invoice Number_Invoices" AS "Invoice Number_Invoices",
		"Invoice Date_Invoices" AS "Invoice Date_Invoices",
		"Is Canceled_Invoices" AS "Is Canceled_Invoices",
		"Vehicle Number_Invoices" AS "Vehicle Number_Invoices",
		MAX("Vehicle Number_Invoices") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Vehicle_Number_Max",
		"Invoice_Date_1" AS "Invoice_Date_1",
		MAX("Invoice_Date_1") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Invoice Date_FZG",
		CASE 
			WHEN (MAX("Invoice_Date_1") OVER (PARTITION BY "Vehicle Reference_berechnet") IS NULL)
				THEN ("Invoice Date_journal_accountings")
			ELSE MAX("Invoice_Date_1") OVER (PARTITION BY "Vehicle Reference_berechnet")
			END AS "Invoice Date",
		"Internal Number_Vehicles" AS "Internal Number_Vehicles",
		"Vin_Vehicles" AS "Vin_Vehicles",
		"Dealer Vehicle Type" AS "Dealer Vehicle Type",
		"Fahrzeugtyp" AS "Fahrzeugtyp",
		"Fahrzeugart" AS "Fahrzeugart",
		"Make Number_Vehicles" AS "Make Number_Vehicles",
		"Description_Make" AS "Description_Make",
		"Fabrikat" AS "Fabrikat",
		"Model Code_Models" AS "Model Code_Models",
		"Model_Detail" AS "Model_Detail",
		"Model" AS "Model",
		"Owner Number_Vehicles" AS "Owner Number_Vehicles",
		"Holder Number_Vehicles" AS "Holder Number_Vehicles",
		"First Name_Owner_Customer" AS "First Name_Owner_Customer",
		"Family Name_Owner_Customer" AS "Family Name_Owner_Customer",
		"First Name_Holder_Customer" AS "First Name_Holder_Customer",
		"Family Name_Holder_Customer" AS "Family Name_Holder_Customer",
		"Kunde" AS "Kunde",
		"Sales_Channel_FZG" AS "Sales_Channel_FZG",
		MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Kundenart_Zahl",
		CASE 
			WHEN (MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") BETWEEN 10 AND 19)
				THEN ('Privater Endkunde')
			WHEN (MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") BETWEEN 20 AND 29)
				THEN ('Gewerbekunde')
			WHEN (MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") BETWEEN 30 AND 39)
				THEN (N'Großkunde')
			WHEN (MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") BETWEEN 40 AND 49)
				THEN ('Sonderabnehmer')
			WHEN (MAX("Sales_Channel_FZG") OVER (PARTITION BY "Vehicle Reference_berechnet") BETWEEN 50 AND 59)
				THEN (N'Wiederverkäufer')
			ELSE NULL
			END AS "Kundenart",
		"Hauptbetrieb" AS "Hauptbetrieb",
		"Standort_alt" AS "Standort_alt",
		"Salesman Number_Vehicles" AS "Salesman Number_Vehicles",
		"Employee Number_Employees_Salesman" AS "Employee Number_Employees_Salesman",
		"Name_Employees_Salesman" AS "Name_Employees_Salesman",
		"Verkäufer" AS "Verkäufer",
		"Standort_FZG_Verkauf" AS "Standort_FZG_Verkauf",
		MAX("Standort_FZG_Verkauf") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Standort_Zahl",
		(convert(VARCHAR(50), MAX("Standort_FZG_Verkauf") OVER (PARTITION BY "Vehicle Reference_berechnet"))) AS "Standort",
		"FZG" AS "FZG",
		"Konto" AS "Konto",
		"Body Paint Code" AS "Body Paint Code",
		"Body Paint Description" AS "Body Paint Description",
		"Farbe" AS "Farbe",
		"Erlös FZG" AS "Erlös FZG",
		"Nachlass" AS "Nachlass",
		"VAK FZG" AS "VAK FZG",
		"Erlös Zulass." AS "Erlös Zulass.",
		"VAK Zulass." AS "VAK Zulass.",
		"Erlös Gar." AS "Erlös Gar.",
		"VAK Gar." AS "VAK Gar.",
		"Erlös Nachr./Aufb." AS "Erlös Nachr./Aufb.",
		"VAK Nachr./Aufb." AS "VAK Nachr./Aufb.",
		"VAK Fertigm." AS "VAK Fertigm.",
		"Erlös Überf." AS "Erlös Überf.",
		"VAK Überf." AS "VAK Überf.",
		"Erlös Sonst._8900" AS "Erlös Sonst._8900",
		"Erlös Sonst." AS "Erlös Sonst.",
		"VAK Sonst." AS "VAK Sonst.",
		"VAK intern" AS "VAK intern",
		"Erlös Prov." AS "Erlös Prov.",
		"Boni/VK-Hilfen" AS "Boni/VK-Hilfen",
		"Erlös ges." AS "Erlös ges.",
		"VAK ges." AS "VAK ges.",
		"Menge_1" AS "Menge_1",
		COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Menge_2",
		("Menge_1" / COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet")) AS "Betrag",
		"Vehicle Reference_berechnet" AS "Vehicle Reference_berechnet",
		"Acct Nr" AS "Acct Nr",
		"Dealer Vehicle Type_Vehicles" AS "Dealer Vehicle Type_Vehicles",
		"Dealer Vehicle Number_Vehicles" AS "Dealer Vehicle Number_Vehicles"
	FROM (
		SELECT c225 AS "Accounting Date",
			c268 AS "Document Type",
			c267 AS "Document Number",
			c266 AS "Position In Document",
			c265 AS "Customer Number",
			c264 AS "Nominal Account Number",
			c263 AS "Is Balanced",
			c262 AS "Clearing Number",
			c261 AS "Document Date",
			c260 AS "Posted Value",
			c259 AS "Debit Or Credit",
			c258 AS "Posted Count",
			c257 AS "Branch Number",
			c256 AS "Customer Contra Account",
			c255 AS "Nominal Contra Account",
			c254 AS "Contra Account Text",
			c253 AS "Account Form Page Number",
			c252 AS "Account Form Page Line",
			c251 AS "Serial Number Each Month",
			c250 AS "Employee Number",
			c150 AS "Invoice Date_journal_accountings",
			c249 AS "Invoice Number",
			c248 AS "Dunning Level",
			c247 AS "Last Dunning Date",
			c246 AS "Journal Page",
			c245 AS "Journal Line",
			c244 AS "Cash Discount",
			c243 AS "Term Of Payment",
			c242 AS "Posting Text",
			c241 AS "Vehicle Reference",
			c240 AS "Vat Id Number",
			c239 AS "Account Statement Number",
			c238 AS "Account Statement Page",
			c237 AS "Vat Key",
			c236 AS "Days For Cash Discount",
			c235 AS "Day Of Actual Accounting",
			c234 AS "Skr51 Branch",
			c233 AS "Skr51 Make",
			c232 AS "Skr51 Cost Center",
			c231 AS "Skr51 Sales Channel",
			c230 AS "Skr51 Cost Unit",
			c229 AS "Previously Used Account No",
			c228 AS "Free Form Accounting Text",
			c227 AS "Free Form Document Text",
			c226 AS "Nom_Account_Is Profit Loss Account",
			c184 AS "Rechtseinheit",
			c183 AS "Betrieb",
			c225 AS "Bookkeep Date",
			c224 AS "Marke",
			c223 AS "Text",
			'1' AS "Mandant",
			c222 AS "Betrag_ori",
			c176 AS "Konto_mit_Bezeichnung",
			c221 AS "Menge",
			c220 AS "Stellen Cost Center",
			c219 AS "KST",
			c218 AS "Stellen Sales Channel",
			c217 AS "Absatzkanal",
			c216 AS "Stellen Cost Unit",
			c215 AS "Kostenträger_mit_Null",
			c214 AS "Kostenträger",
			c213 AS "GuV_Bilanz",
			'3' AS "Susa",
			c212 AS "Acct Nr_ori",
			c211 AS "Document Type In Journal",
			c210 AS "Document Type Description",
			c209 AS "FZG_ja_nein",
			c208 AS "Invoice Type_Invoices",
			c207 AS "Invoice Number_Invoices",
			c206 AS "Invoice Date_Invoices",
			c205 AS "Is Canceled_Invoices",
			c204 AS "Vehicle Number_Invoices",
			c203 AS "Invoice_Date_1",
			c202 AS "Internal Number_Vehicles",
			c201 AS "Vin_Vehicles",
			c200 AS "Dealer Vehicle Type",
			c199 AS "Fahrzeugtyp",
			c198 AS "Fahrzeugart",
			c197 AS "Make Number_Vehicles",
			c196 AS "Description_Make",
			c196 AS "Fabrikat",
			c195 AS "Model Code_Models",
			c194 AS "Model_Detail",
			c193 AS "Model",
			c192 AS "Owner Number_Vehicles",
			c191 AS "Holder Number_Vehicles",
			c190 AS "First Name_Owner_Customer",
			c189 AS "Family Name_Owner_Customer",
			c188 AS "First Name_Holder_Customer",
			c187 AS "Family Name_Holder_Customer",
			c186 AS "Kunde",
			c185 AS "Sales_Channel_FZG",
			c184 AS "Hauptbetrieb",
			c183 AS "Standort_alt",
			c182 AS "Salesman Number_Vehicles",
			c181 AS "Employee Number_Employees_Salesman",
			c180 AS "Name_Employees_Salesman",
			c179 AS "Verkäufer",
			c178 AS "Standort_FZG_Verkauf",
			c177 AS "FZG",
			c176 AS "Konto",
			c175 AS "Body Paint Code",
			c174 AS "Body Paint Description",
			c174 AS "Farbe",
			c173 AS "Erlös FZG",
			c172 AS "Nachlass",
			c171 AS "VAK FZG",
			c170 AS "Erlös Zulass.",
			c169 AS "VAK Zulass.",
			c168 AS "Erlös Gar.",
			c167 AS "VAK Gar.",
			c166 AS "Erlös Nachr./Aufb.",
			c165 AS "VAK Nachr./Aufb.",
			c164 AS "VAK Fertigm.",
			c163 AS "Erlös Überf.",
			c162 AS "VAK Überf.",
			c161 AS "Erlös Sonst._8900",
			c160 AS "Erlös Sonst.",
			c159 AS "VAK Sonst.",
			c158 AS "VAK intern",
			c157 AS "Erlös Prov.",
			c156 AS "Boni/VK-Hilfen",
			c155 AS "Erlös ges.",
			c154 AS "VAK ges.",
			1.0 AS "Menge_1",
			c149 AS "Vehicle Reference_berechnet",
			c153 AS "Acct Nr",
			c152 AS "Dealer Vehicle Type_Vehicles",
			c151 AS "Dealer Vehicle Number_Vehicles",
			MAX(c204) OVER (PARTITION BY c149) AS c142,
			(
				CASE 
					WHEN ((MAX(c203) OVER (PARTITION BY c149)) IS NULL)
						THEN ((c150))
					ELSE ((MAX(c203) OVER (PARTITION BY c149)))
					END
				) AS c143,
			(
				CASE 
					WHEN ((SUM(c209) OVER (PARTITION BY c149)) > 0)
						THEN (N'FZG-Geschäft')
					ELSE (N'Buchungen ohne FZG-Geschäft')
					END
				) AS c144
		FROM (
			SELECT (substring(journal_accountings."vehicle_reference", 1, 8)) + '_' + (right(journal_accountings."vehicle_reference", 17)) AS c149,
				(convert(DATETIME, (((substring(journal_accountings."invoice_date", 1, 4)) + (substring(journal_accountings."invoice_date", 6, 2)) + (substring(journal_accountings."invoice_date", 9, 2)))))) AS c150,
				vehicles."dealer_vehicle_number" AS c151,
				vehicles."dealer_vehicle_type" AS c152,
				(reverse(rtrim(reverse(((substring((convert(VARCHAR(50), journal_accountings."nominal_account_number")), 1, 4)) + '_STK'))))) AS c153,
				(
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					(
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (7200))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											)
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (7210))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											)
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (7220))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											)
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (7240))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											)
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (7030))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											)
										)
							ELSE (0)
							END
						)
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7800, 7850))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) AS c154,
				(
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					(
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (8200))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											) * - 1
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (8210))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											) * - 1
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (8220))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											) * - 1
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (8030))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											) * - 1
										)
							ELSE (0)
							END
						) + (
						CASE 
							WHEN (journal_accountings."nominal_account_number" IN (8900))
								THEN (
										(
											CASE 
												WHEN (journal_accountings."debit_or_credit" = 'H')
													THEN (journal_accountings."posted_value" / 100 * - 1)
												ELSE (journal_accountings."posted_value" / 100)
												END
											) * - 1
										)
							ELSE (0)
							END
						)
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8001, 8011, 5002))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) AS c155,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7800, 7850))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c156,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c157,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c158,
				(
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7200))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7210))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7220))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7240))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (7030))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										)
									)
						ELSE (0)
						END
					) AS c159,
				(
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8200))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8210))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8220))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8030))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) + (
					CASE 
						WHEN (journal_accountings."nominal_account_number" IN (8900))
							THEN (
									(
										CASE 
											WHEN (journal_accountings."debit_or_credit" = 'H')
												THEN (journal_accountings."posted_value" / 100 * - 1)
											ELSE (journal_accountings."posted_value" / 100)
											END
										) * - 1
									)
						ELSE (0)
						END
					) AS c160,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8900))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c161,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7030))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c162,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8030))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c163,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7240))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c164,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7220))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c165,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8220))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c166,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7210))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c167,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8210))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c168,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7200))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c169,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8200))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c170,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									)
								)
					ELSE (0)
					END AS c171,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8001, 8011, 5002))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c172,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
						THEN (
								(
									CASE 
										WHEN (journal_accountings."debit_or_credit" = 'H')
											THEN (journal_accountings."posted_value" / 100 * - 1)
										ELSE (journal_accountings."posted_value" / 100)
										END
									) * - 1
								)
					ELSE (0)
					END AS c173,
				vehicles."body_paint_description" AS c174,
				vehicles."body_paint_code" AS c175,
				((substring((convert(VARCHAR(50), journal_accountings."nominal_account_number")), 1, 4)) + ' - ' + nominal_accounts."account_description") AS c176,
				(right(vehicles."vin", 5)) + ' / ' + ((substring((convert(VARCHAR(50), vehicles."internal_number")), 1, 5))) + ' - ' + models."description" + ' - ' + (
					CASE 
						WHEN (customers_suppliers_holder."first_name" <> ' ')
							THEN ((substring((convert(VARCHAR(50), vehicles."holder_number")), 1, 6)) + ' - ' + customers_suppliers_holder."first_name" + ' ' + customers_suppliers_holder."family_name")
						ELSE ((substring((convert(VARCHAR(50), vehicles."holder_number")), 1, 6)) + ' - ' + customers_suppliers_holder."family_name")
						END
					) AS c177,
				CASE 
					WHEN (
							(
								(
									CASE 
										WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
											THEN (1)
										ELSE (0)
										END
									) = 1
								)
							AND (invoices."is_canceled" <> '1')
							)
						THEN (journal_accountings."skr51_branch")
					ELSE NULL
					END AS c178,
				(substring((convert(VARCHAR(50), vehicles."salesman_number")), 1, 3)) + ' - ' + employees."name" AS c179,
				employees."name" AS c180,
				employees."employee_number" AS c181,
				vehicles."salesman_number" AS c182,
				((convert(VARCHAR, journal_accountings."skr51_branch"))) AS c183,
				convert(varchar, journal_accountings."subsidiary_to_company_ref") AS c184,
				CASE 
					WHEN (
							(
								(
									CASE 
										WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
											THEN (1)
										ELSE (0)
										END
									) = 1
								)
							AND (invoices."is_canceled" <> '1')
							)
						THEN (journal_accountings."skr51_sales_channel")
					END AS c185,
				CASE 
					WHEN (customers_suppliers_holder."first_name" <> ' ')
						THEN ((substring((convert(VARCHAR(50), vehicles."holder_number")), 1, 6)) + ' - ' + customers_suppliers_holder."first_name" + ' ' + customers_suppliers_holder."family_name")
					ELSE ((substring((convert(VARCHAR(50), vehicles."holder_number")), 1, 6)) + ' - ' + customers_suppliers_holder."family_name")
					END AS c186,
				customers_suppliers_holder."family_name" AS c187,
				customers_suppliers_holder."first_name" AS c188,
				customers_suppliers."family_name" AS c189,
				customers_suppliers."first_name" AS c190,
				vehicles."holder_number" AS c191,
				vehicles."owner_number" AS c192,
				(substring((upper(models."description")), 1, 3)) AS c193,
				models."description" AS c194,
				models."model_code" AS c195,
				makes."description" AS c196,
				vehicles."make_number" AS c197,
				CASE 
					WHEN (
							(
								CASE 
									WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('N'))
										THEN ('NW')
									WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('V'))
										THEN ('VFW')
									WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('G', 'D'))
										THEN ('GW')
									WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('T'))
										THEN ('TZ')
									ELSE NULL
									END
								) IN ('VFW', 'TZ', 'NW')
							)
						THEN ('Neuwagen')
					ELSE ('Gebrauchtwagen')
					END AS c198,
				CASE 
					WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('N'))
						THEN ('NW')
					WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('V'))
						THEN ('VFW')
					WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('G', 'D'))
						THEN ('GW')
					WHEN (((substring(journal_accountings."vehicle_reference", 1, 1))) IN ('T'))
						THEN ('TZ')
					END AS c199,
				(substring(journal_accountings."vehicle_reference", 1, 1)) AS c200,
				vehicles."vin" AS c201,
				vehicles."internal_number" AS c202,
				CASE 
					WHEN (
							(
								CASE 
									WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
										THEN (1)
									ELSE (0)
									END
								) = 1
							)
						THEN (((convert(DATETIME, (((substring(journal_accountings."invoice_date", 1, 4)) + (substring(journal_accountings."invoice_date", 6, 2)) + (substring(journal_accountings."invoice_date", 9, 2))))))))
					END AS c203,
				invoices."vehicle_number" AS c204,
				invoices."is_canceled" AS c205,
				invoices."invoice_date" AS c206,
				invoices."invoice_number" AS c207,
				invoices."invoice_type" AS c208,
				CASE 
					WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
						THEN (1)
					ELSE (0)
					END AS c209,
				document_types."document_type_description" AS c210,
				document_types."document_type_in_journal" AS c211,
				(substring((convert(VARCHAR(50), journal_accountings."nominal_account_number")), 1, 4)) + '_STK' AS c212,
				CASE 
					WHEN (nominal_accounts."is_profit_loss_account" = 'J')
						THEN ('2')
					ELSE ('1')
					END AS c213,
				CASE 
					WHEN (journal_accountings."skr51_cost_unit" BETWEEN 1 AND 49)
						THEN (((rtrim((convert(VARCHAR(50), journal_accountings."skr51_make")))) + ' - ' + (rtrim((convert(VARCHAR(50), journal_accountings."skr51_cost_unit")))) + ' - ' + (accounts_characteristics."skr51_cost_unit_name")))
					ELSE ((rtrim((convert(VARCHAR(50), journal_accountings."skr51_cost_unit")))) + ' - ' + (accounts_characteristics."skr51_cost_unit_name"))
					END AS c214,
				(rtrim((convert(VARCHAR(50), journal_accountings."skr51_make")))) + ' - ' + (rtrim((convert(VARCHAR(50), journal_accountings."skr51_cost_unit")))) + ' - ' + (accounts_characteristics."skr51_cost_unit_name") AS c215,
				(len((convert(VARCHAR(50), journal_accountings."skr51_cost_unit")))) AS c216,
				(rtrim((convert(VARCHAR(50), journal_accountings."skr51_sales_channel")))) + ' - ' + accounts_characteristics."skr51_sales_channel_name" AS c217,
				(len((convert(VARCHAR(50), journal_accountings."skr51_sales_channel")))) AS c218,
				(rtrim((convert(VARCHAR(50), journal_accountings."skr51_cost_center")))) + ' - ' + accounts_characteristics."skr51_cost_center_name" AS c219,
				(len((convert(VARCHAR(50), journal_accountings."skr51_cost_center")))) AS c220,
				CASE 
					WHEN (journal_accountings."debit_or_credit" = 'H')
						THEN (journal_accountings."posted_count" / 100 * - 1)
					ELSE (journal_accountings."posted_count" / 100)
					END AS c221,
				CASE 
					WHEN (journal_accountings."debit_or_credit" = 'H')
						THEN (journal_accountings."posted_value" / 100 * - 1)
					ELSE (journal_accountings."posted_value" / 100)
					END AS c222,
				CASE 
					WHEN ((datediff(day, (convert(DATETIME, journal_accountings."accounting_date")), getdate())) <= 360)
						THEN (journal_accountings."invoice_number" + ' - ' + journal_accountings."posting_text" + '/' + journal_accountings."vehicle_reference" + ' - ' + (convert(VARCHAR(50), journal_accountings."employee_number")))
					ELSE (N'Buchungen älter 360 Tage')
					END AS c223,
				(rtrim((convert(VARCHAR(50), journal_accountings."skr51_make")))) + ' - ' + accounts_characteristics."skr51_make_description" AS c224,
				journal_accountings."accounting_date" AS c225,
				nominal_accounts."is_profit_loss_account" AS c226,
				CASE 
					WHEN (journal_accountings."skr51_cost_unit" BETWEEN 1 AND 49)
						THEN ('Neuwagen')
					WHEN (journal_accountings."skr51_cost_unit" BETWEEN 50 AND 59)
						THEN ('Gebrauchtwagen')
					WHEN (journal_accountings."skr51_cost_unit" BETWEEN 60 AND 69)
						THEN (N'Teile & Zubehör')
					WHEN (journal_accountings."skr51_cost_unit" BETWEEN 70 AND 79)
						THEN ('Service')
					WHEN (journal_accountings."skr51_cost_unit" = 0)
						THEN (N'Ohne Kostenträger')
					END AS c227,
				journal_accountings."free_form_accounting_text" AS c228,
				journal_accountings."previously_used_account_no" AS c229,
				journal_accountings."skr51_cost_unit" AS c230,
				journal_accountings."skr51_sales_channel" AS c231,
				journal_accountings."skr51_cost_center" AS c232,
				convert(varchar, journal_accountings."skr51_make") AS c233,
				journal_accountings."skr51_branch" AS c234,
				journal_accountings."day_of_actual_accounting" AS c235,
				journal_accountings."days_for_cash_discount" AS c236,
				journal_accountings."vat_key" AS c237,
				journal_accountings."account_statement_page" AS c238,
				journal_accountings."account_statement_number" AS c239,
				journal_accountings."vat_id_number" AS c240,
				journal_accountings."vehicle_reference" AS c241,
				journal_accountings."posting_text" AS c242,
				journal_accountings."term_of_payment" AS c243,
				journal_accountings."cash_discount" AS c244,
				journal_accountings."journal_line" AS c245,
				journal_accountings."journal_page" AS c246,
				journal_accountings."last_dunning_date" AS c247,
				journal_accountings."dunning_level" AS c248,
				journal_accountings."invoice_number" AS c249,
				journal_accountings."employee_number" AS c250,
				journal_accountings."serial_number_each_month" AS c251,
				journal_accountings."account_form_page_line" AS c252,
				journal_accountings."account_form_page_number" AS c253,
				journal_accountings."contra_account_text" AS c254,
				journal_accountings."nominal_contra_account" AS c255,
				journal_accountings."customer_contra_account" AS c256,
				journal_accountings."branch_number" AS c257,
				journal_accountings."posted_count" AS c258,
				journal_accountings."debit_or_credit" AS c259,
				journal_accountings."posted_value" AS c260,
				journal_accountings."document_date" AS c261,
				journal_accountings."clearing_number" AS c262,
				journal_accountings."is_balanced" AS c263,
				journal_accountings."nominal_account_number" AS c264,
				journal_accountings."customer_number" AS c265,
				journal_accountings."position_in_document" AS c266,
				journal_accountings."document_number" AS c267,
				journal_accountings."document_type" AS c268
			FROM LOCOSOFT."dbo"."nominal_accounts" nominal_accounts,
				(
					(
						(
							(
								(
									(
										(
											(
												(
													LOCOSOFT."dbo"."journal_accountings" journal_accountings LEFT OUTER JOIN LOCOSOFT."dbo"."accounts_characteristics" accounts_characteristics ON (
															(
																(
																	(
																		(accounts_characteristics."skr51_make" = journal_accountings."skr51_make")
																		AND (accounts_characteristics."skr51_cost_center" = journal_accountings."skr51_cost_center")
																		)
																	AND (accounts_characteristics."skr51_sales_channel" = journal_accountings."skr51_sales_channel")
																	)
																AND (accounts_characteristics."skr51_cost_unit" = journal_accountings."skr51_cost_unit")
																)
															AND (accounts_characteristics."skr51_branch" = journal_accountings."skr51_branch")
															)
														AND (accounts_characteristics."subsidiary_to_company_ref" = journal_accountings."subsidiary_to_company_ref")
													) LEFT OUTER JOIN LOCOSOFT."dbo"."document_types" document_types ON document_types."document_type_in_journal" = journal_accountings."document_type"
												) LEFT OUTER JOIN LOCOSOFT."dbo"."invoices" invoices ON invoices."invtype_invnr" = journal_accountings."invoice_number"
											) LEFT OUTER JOIN LOCOSOFT."dbo"."vehicles" vehicles ON (right(journal_accountings."vehicle_reference", 17)) = vehicles."vin"
										) LEFT OUTER JOIN LOCOSOFT."dbo"."makes" makes ON vehicles."make_number" = makes."make_number"
									) LEFT OUTER JOIN LOCOSOFT."dbo"."models" models ON (vehicles."make_number" = models."make_number")
									AND (vehicles."model_code" = models."model_code")
								) LEFT OUTER JOIN LOCOSOFT."dbo"."customers_suppliers" customers_suppliers ON customers_suppliers."customer_number" = vehicles."owner_number"
							) LEFT OUTER JOIN LOCOSOFT."dbo"."customers_suppliers" customers_suppliers_holder ON customers_suppliers_holder."customer_number" = vehicles."holder_number"
						) LEFT OUTER JOIN LOCOSOFT."dbo"."employees" employees ON (vehicles."salesman_number" <> 0)
						AND (vehicles."salesman_number" = employees."salesman_number")
					)
			WHERE (
					(nominal_accounts."nominal_account_number" = journal_accountings."nominal_account_number")
					AND (nominal_accounts."subsidiary_to_company_ref" = journal_accountings."subsidiary_to_company_ref")
					)
				AND (
					(
						(
							(nominal_accounts."is_profit_loss_account" = 'J')
							AND (journal_accountings."vehicle_reference" <> ' ')
							)
						AND (journal_accountings."nominal_account_number" IN (8000, 8001, 8010, 8011, 5002, 8100, 8110, 8111, 8200, 8210, 8510, 8800, 8820, 8830, 8870, 8928, 8934, 7000, 7010, 7100, 7101, 7110, 7111, 7120, 7200, 7210, 7240, 7510, 7800, 7850, 5001, 5005, 5006, 5007, 5008, 5003, 5004))
						)
					AND (
						(
							CASE 
								WHEN (journal_accountings."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
									THEN (1)
								ELSE (0)
								END
							) = 1
						)
					)
			) D2
		) D1
	WHERE (
			(
				(
					(
						(
							(c142 = "Internal Number_Vehicles")
							OR ("Internal Number_Vehicles" IS NULL)
							)
						OR (c142 IS NULL)
						)
					AND (c143 >= convert(DATE, '2017-01-01'))
					)
				AND (c144 = N'FZG-Geschäft')
				)
			AND (c143 = "Invoice_Date_1")
			)
	) D4
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

GO
