Bläddra i källkod

sqlformat.bat auf iqd_convert angewendet

Zumbuelt 1 år sedan
förälder
incheckning
be4c8dd665
65 ändrade filer med 13878 tillägg och 7904 borttagningar
  1. 108 108
      System/LOCOSOFT/IQD/Belege/Kontenrahmen.sql
  2. 67 30
      System/LOCOSOFT/IQD/Belege/current_date_Prognose.sql
  3. 64 30
      System/LOCOSOFT/IQD/Belege/current_date_Prognose_operativ_VJ-2.sql
  4. 108 108
      System/LOCOSOFT/IQD/Belege/kontenrahmen_gc_struct_skr.sql
  5. 194 69
      System/LOCOSOFT/IQD/Belege/loc_belege.sql
  6. 167 70
      System/LOCOSOFT/IQD/Belege/loc_belege_bilanz.sql
  7. 1198 406
      System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK.sql
  8. 1321 545
      System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK_Stk_FIBU.sql
  9. 1321 545
      System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK_Stk_FIBU_Vorlage.sql
  10. 471 252
      System/LOCOSOFT/IQD/NW/NW_GW_BE_auf_NW_GW_Bestand_Cat.sql
  11. 0 111
      System/LOCOSOFT/IQD/OP/Kreditorenbewegungen.sql
  12. 19 13
      System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_8520.sql
  13. 0 190
      System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_Deb_Saldo.sql
  14. 306 160
      System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_nur_8520.sql
  15. 0 190
      System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_ohne_8520.sql
  16. 184 69
      System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_ben_AW_final.sql
  17. 418 255
      System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_neu.sql
  18. 148 32
      System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_verk_AW_final.sql
  19. 13 7
      System/LOCOSOFT/IQD/Serv_Teile/Imvoice_No_Order_No.sql
  20. 18 11
      System/LOCOSOFT/IQD/Serv_Teile/Serviceberater_Rechnung.sql
  21. 6 6
      System/LOCOSOFT/IQD/Serv_Teile/ben_AW_Order_Number.sql
  22. 142 93
      System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_DG_Anz_Tage.sql
  23. 150 100
      System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Teile.sql
  24. 158 104
      System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Ums_ben_AW.sql
  25. 74 18
      System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Ums_ben_AW_fuer_Monteure.sql
  26. 125 117
      System/LOCOSOFT/IQD/Zeit/Zeit_Abwesenheit_neu.sql
  27. 210 168
      System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_Pausen.sql
  28. 214 171
      System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_Pausen_produktiv.sql
  29. 55 35
      System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_neu.sql
  30. 91 75
      System/LOCOSOFT/IQD/Zeit/Zeit_ben_Std_LG.sql
  31. 45 36
      System/LOCOSOFT/IQD/Zeit/Zeit_verk_Std.sql
  32. 27 14
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Imvoice_No_Order_No.sql
  33. 121 114
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Kontenrahmen.sql
  34. 33 20
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.OP_aus_LOC_Belege_8520.sql
  35. 32 18
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Serviceberater_Rechnung.sql
  36. 20 13
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.ben_AW_Order_Number.sql
  37. 88 25
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.offene_Auftraege_Ums_ben_AW_fuer_Monteure.sql
  38. 197 75
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_ben_aw_final_imr.sql
  39. 431 261
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_neu_imr.sql
  40. 162 39
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_verk_aw_final_imr.sql
  41. 81 37
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.current_date_prognose_imr.sql
  42. 78 37
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.current_date_prognose_operativ_vj-2_imr.sql
  43. 121 114
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.kontenrahmen_gc_struct_skr_imr.sql
  44. 3 114
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.kreditorenbewegungen_imr.sql
  45. 180 76
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_bilanz_imr.sql
  46. 207 75
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_imr.sql
  47. 1211 412
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_nw_gw_vk_imr.sql
  48. 1334 551
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_nw_gw_vk_stk_fibu_imr.sql
  49. 484 258
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.nw_gw_be_auf_nw_gw_bestand_cat_imr.sql
  50. 155 99
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_dg_anz_tage_imr.sql
  51. 163 106
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_teile_imr.sql
  52. 171 110
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_ums_ben_aw_imr.sql
  53. 33 20
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_8520_imr.sql
  54. 3 193
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_deb_saldo_imr.sql
  55. 319 166
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_nur_8520_imr.sql
  56. 3 193
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_ohne_8520_imr.sql
  57. 138 123
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_abwesenheit_neu_imr.sql
  58. 104 81
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_ben_std_lg_imr.sql
  59. 69 42
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_neu_imr.sql
  60. 223 174
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_pausen_imr.sql
  61. 227 177
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_pausen_produktiv_imr.sql
  62. 59 43
      System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_verk_std_imr.sql
  63. 3 0
      Tasks/LOCOSOFT_imr_csv.bat
  64. BIN
      Tasks/scripts/SqlFormatter.exe
  65. 3 0
      Tasks/scripts/sqlformat.bat

+ 108 - 108
System/LOCOSOFT/IQD/Belege/Kontenrahmen.sql

@@ -1,108 +1,108 @@
-select T1."KONTO_NR" as "Konto Nr",
-	   T1."KONTO_BEZEICHNUNG" as "Konto Bezeichnung",
-	   T1."KONTO_ART" as "Konto Art_ori",
-	   T1."KOSTENSTELLE" as "Kostenstelle",
-	   T1."STK" as "Stk",
-	   T1."KONTO_1" as "Konto 1",
-	   T1."KONTO_2" as "Konto 2",
-	   T1."KONTO_3" as "Konto 3",
-	   T1."KONTO_4" as "Konto 4",
-	   T1."KONTO_5" as "Konto 5",
-	   T1."EBENE1" as "Ebene1",
-	   T1."EBENE2" as "Ebene2",
-	   T1."EBENE3" as "Ebene3",
-	   T1."EBENE4" as "Ebene4",
-	   T1."EBENE5" as "Ebene5",
-	   T1."EBENE6" as "Ebene6",
-	   T1."EBENE7" as "Ebene7",
-	   T1."EBENE8" as "Ebene8",
-	   T1."EBENE9" as "Ebene9",
-	   T1."EBENE10" as "Ebene10",
-	   T1."EBENE11" as "Ebene11",
-	   T1."EBENE12" as "Ebene12",
-	   T1."EBENE13" as "Ebene13",
-	   T1."EBENE14" as "Ebene14",
-	   T1."EBENE15" as "Ebene15",
-	   T1."EBENE16" as "Ebene16",
-	   T1."EBENE17" as "Ebene17",
-	   T1."EBENE18" as "Ebene18",
-	   T1."EBENE19" as "Ebene19",
-	   T1."EBENE20" as "Ebene20",
-	   T1."EBENE21" as "Ebene21",
-	   T1."EBENE22" as "Ebene22",
-	   T1."EBENE23" as "Ebene23",
-	   T1."EBENE24" as "Ebene24",
-	   T1."EBENE25" as "Ebene25",
-	   T1."EBENE26" as "Ebene26",
-	   T1."EBENE27" as "Ebene27",
-	   T1."EBENE28" as "Ebene28",
-	   T1."EBENE29" as "Ebene29",
-	   T1."EBENE30" as "Ebene30",
-	   T1."EBENE31" as "Ebene31",
-	   T1."EBENE32" as "Ebene32",
-	   T1."EBENE33" as "Ebene33",
-	   T1."EBENE34" as "Ebene34",
-	   T1."EBENE35" as "Ebene35",
-	   T1."EBENE36" as "Ebene36",
-	   T1."EBENE37" as "Ebene37",
-	   T1."EBENE38" as "Ebene38",
-	   T1."EBENE39" as "Ebene39",
-	   T1."EBENE40" as "Ebene40",
-	   T1."EBENE41" as "Ebene41",
-	   T1."EBENE42" as "Ebene42",
-	   T1."EBENE43" as "Ebene43",
-	   T1."EBENE44" as "Ebene44",
-	   T1."EBENE45" as "Ebene45",
-	   T1."EBENE46" as "Ebene46",
-	   T1."EBENE47" as "Ebene47",
-	   T1."EBENE48" as "Ebene48",
-	   T1."EBENE49" as "Ebene49",
-	   T1."EBENE50" as "Ebene50",
-	   T1."EBENE51" as "Ebene51",
-	   T1."EBENE52" as "Ebene52",
-	   T1."EBENE53" as "Ebene53",
-	   T1."EBENE54" as "Ebene54",
-	   T1."EBENE55" as "Ebene55",
-	   T1."EBENE56" as "Ebene56",
-	   T1."EBENE57" as "Ebene57",
-	   T1."EBENE58" as "Ebene58",
-	   T1."EBENE59" as "Ebene59",
-	   T1."EBENE60" as "Ebene60",
-	   T1."EBENE61" as "Ebene61",
-	   T1."EBENE62" as "Ebene62",
-	   T1."EBENE63" as "Ebene63",
-	   T1."EBENE64" as "Ebene64",
-	   T1."EBENE65" as "Ebene65",
-	   T1."EBENE66" as "Ebene66",
-	   T1."EBENE67" as "Ebene67",
-	   T1."EBENE68" as "Ebene68",
-	   T1."EBENE69" as "Ebene69",
-	   T1."EBENE70" as "Ebene70",
-	   T1."EBENE71" as "Ebene71",
-	   T1."EBENE72" as "Ebene72",
-	   T1."EBENE73" as "Ebene73",
-	   T1."EBENE74" as "Ebene74",
-	   T1."EBENE75" as "Ebene75",
-	   T1."EBENE76" as "Ebene76",
-	   T1."EBENE77" as "Ebene77",
-	   T1."EBENE78" as "Ebene78",
-	   T1."EBENE79" as "Ebene79",
-	   T1."EBENE80" as "Ebene80",
-	   T1."EBENE81" as "Ebene81",
-	   T1."EBENE82" as "Ebene82",
-	   T1."EBENE83" as "Ebene83",
-	   T1."EBENE84" as "Ebene84",
-	   T1."EBENE85" as "Ebene85",
-	   T1."EBENE86" as "Ebene86",
-	   T1."EBENE87" as "Ebene87",
-	   T1."EBENE88" as "Ebene88",
-	   T1."EBENE89" as "Ebene89",
-	   T1."EBENE90" as "Ebene90",
-	   (len(T1."KONTO_NR" + 'Z') - 1) as "Anzahl Stellen_Konto_Nr",
-	   T1."KONTO_ART" as "Konto Art",
-	   T1."KONTO_ART" as "GuV_Bilanz",
-	   T1."KONTO_NR" as "Acct Nr",
-	   T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" as "Konto",
-	   (left(T1."KONTO_NR",1)) as "Susa"
-from "Kontenrahmen.csv" T1
-where (((left(T1."KONTO_NR",1))) <> '9')
+SELECT T1."KONTO_NR" AS "Konto Nr",
+	T1."KONTO_BEZEICHNUNG" AS "Konto Bezeichnung",
+	T1."KONTO_ART" AS "Konto Art_ori",
+	T1."KOSTENSTELLE" AS "Kostenstelle",
+	T1."STK" AS "Stk",
+	T1."KONTO_1" AS "Konto 1",
+	T1."KONTO_2" AS "Konto 2",
+	T1."KONTO_3" AS "Konto 3",
+	T1."KONTO_4" AS "Konto 4",
+	T1."KONTO_5" AS "Konto 5",
+	T1."EBENE1" AS "Ebene1",
+	T1."EBENE2" AS "Ebene2",
+	T1."EBENE3" AS "Ebene3",
+	T1."EBENE4" AS "Ebene4",
+	T1."EBENE5" AS "Ebene5",
+	T1."EBENE6" AS "Ebene6",
+	T1."EBENE7" AS "Ebene7",
+	T1."EBENE8" AS "Ebene8",
+	T1."EBENE9" AS "Ebene9",
+	T1."EBENE10" AS "Ebene10",
+	T1."EBENE11" AS "Ebene11",
+	T1."EBENE12" AS "Ebene12",
+	T1."EBENE13" AS "Ebene13",
+	T1."EBENE14" AS "Ebene14",
+	T1."EBENE15" AS "Ebene15",
+	T1."EBENE16" AS "Ebene16",
+	T1."EBENE17" AS "Ebene17",
+	T1."EBENE18" AS "Ebene18",
+	T1."EBENE19" AS "Ebene19",
+	T1."EBENE20" AS "Ebene20",
+	T1."EBENE21" AS "Ebene21",
+	T1."EBENE22" AS "Ebene22",
+	T1."EBENE23" AS "Ebene23",
+	T1."EBENE24" AS "Ebene24",
+	T1."EBENE25" AS "Ebene25",
+	T1."EBENE26" AS "Ebene26",
+	T1."EBENE27" AS "Ebene27",
+	T1."EBENE28" AS "Ebene28",
+	T1."EBENE29" AS "Ebene29",
+	T1."EBENE30" AS "Ebene30",
+	T1."EBENE31" AS "Ebene31",
+	T1."EBENE32" AS "Ebene32",
+	T1."EBENE33" AS "Ebene33",
+	T1."EBENE34" AS "Ebene34",
+	T1."EBENE35" AS "Ebene35",
+	T1."EBENE36" AS "Ebene36",
+	T1."EBENE37" AS "Ebene37",
+	T1."EBENE38" AS "Ebene38",
+	T1."EBENE39" AS "Ebene39",
+	T1."EBENE40" AS "Ebene40",
+	T1."EBENE41" AS "Ebene41",
+	T1."EBENE42" AS "Ebene42",
+	T1."EBENE43" AS "Ebene43",
+	T1."EBENE44" AS "Ebene44",
+	T1."EBENE45" AS "Ebene45",
+	T1."EBENE46" AS "Ebene46",
+	T1."EBENE47" AS "Ebene47",
+	T1."EBENE48" AS "Ebene48",
+	T1."EBENE49" AS "Ebene49",
+	T1."EBENE50" AS "Ebene50",
+	T1."EBENE51" AS "Ebene51",
+	T1."EBENE52" AS "Ebene52",
+	T1."EBENE53" AS "Ebene53",
+	T1."EBENE54" AS "Ebene54",
+	T1."EBENE55" AS "Ebene55",
+	T1."EBENE56" AS "Ebene56",
+	T1."EBENE57" AS "Ebene57",
+	T1."EBENE58" AS "Ebene58",
+	T1."EBENE59" AS "Ebene59",
+	T1."EBENE60" AS "Ebene60",
+	T1."EBENE61" AS "Ebene61",
+	T1."EBENE62" AS "Ebene62",
+	T1."EBENE63" AS "Ebene63",
+	T1."EBENE64" AS "Ebene64",
+	T1."EBENE65" AS "Ebene65",
+	T1."EBENE66" AS "Ebene66",
+	T1."EBENE67" AS "Ebene67",
+	T1."EBENE68" AS "Ebene68",
+	T1."EBENE69" AS "Ebene69",
+	T1."EBENE70" AS "Ebene70",
+	T1."EBENE71" AS "Ebene71",
+	T1."EBENE72" AS "Ebene72",
+	T1."EBENE73" AS "Ebene73",
+	T1."EBENE74" AS "Ebene74",
+	T1."EBENE75" AS "Ebene75",
+	T1."EBENE76" AS "Ebene76",
+	T1."EBENE77" AS "Ebene77",
+	T1."EBENE78" AS "Ebene78",
+	T1."EBENE79" AS "Ebene79",
+	T1."EBENE80" AS "Ebene80",
+	T1."EBENE81" AS "Ebene81",
+	T1."EBENE82" AS "Ebene82",
+	T1."EBENE83" AS "Ebene83",
+	T1."EBENE84" AS "Ebene84",
+	T1."EBENE85" AS "Ebene85",
+	T1."EBENE86" AS "Ebene86",
+	T1."EBENE87" AS "Ebene87",
+	T1."EBENE88" AS "Ebene88",
+	T1."EBENE89" AS "Ebene89",
+	T1."EBENE90" AS "Ebene90",
+	(len(T1."KONTO_NR" + 'Z') - 1) AS "Anzahl Stellen_Konto_Nr",
+	T1."KONTO_ART" AS "Konto Art",
+	T1."KONTO_ART" AS "GuV_Bilanz",
+	T1."KONTO_NR" AS "Acct Nr",
+	T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" AS "Konto",
+	(left(T1."KONTO_NR", 1)) AS "Susa"
+FROM "Kontenrahmen.csv" T1
+WHERE (((left(T1."KONTO_NR", 1))) <> '9')

+ 67 - 30
System/LOCOSOFT/IQD/Belege/current_date_Prognose.sql

@@ -1,30 +1,67 @@
-select "Bundeslaender Id" as "Bundeslaender Id",
-	   "Datum" as "Datum",
-	   "Wochentage Id" as "Wochentage Id",
-	   "Arbeitstag Mofr" as "Arbeitstag Mofr",
-	   "Zaehler Mofr" as "Zaehler Mofr",
-	   "Summe Mofr" as "Summe Mofr",
-	   "Arbeitstag Mosa" as "Arbeitstag Mosa",
-	   "Zaehler Mosa" as "Zaehler Mosa",
-	   "Summe Mosa" as "Summe Mosa",
-	   "Feiertage Id" as "Feiertage Id",
-	   "Jahr" as "Jahr",
-	   CASE WHEN ("Arbeitstag Mofr" = 1) THEN (RCOUNT("Arbeitstag Mofr")) ELSE null END as "Arbeitstag Nr Jahr",
-	   "Gesamt Arbeitstage" as "Gesamt Arbeitstage"
-from 
-(select T1."bundeslaender_id" as "Bundeslaender Id",
-	   T1."datum" as "Datum",
-	   T1."wochentage_id" as "Wochentage Id",
-	   T1."arbeitstag_mofr" as "Arbeitstag Mofr",
-	   T1."zaehler_mofr" as "Zaehler Mofr",
-	   T1."summe_mofr" as "Summe Mofr",
-	   T1."arbeitstag_mosa" as "Arbeitstag Mosa",
-	   T1."zaehler_mosa" as "Zaehler Mosa",
-	   T1."summe_mosa" as "Summe Mosa",
-	   T1."feiertage_id" as "Feiertage Id",
-	   (getdate()) - 1 as "Jahr",
-	   CASE WHEN (T1."datum" BETWEEN convert(datetime, '2008-01-01 00:00:00.000') AND convert(datetime, '2008-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2009-01-01 00:00:00.000') AND convert(datetime, '2009-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2010-01-01 00:00:00.000') AND convert(datetime, '2010-12-31 00:00:00.000')) THEN (253) WHEN (T1."datum" BETWEEN convert(datetime, '2011-01-01 00:00:00.000') AND convert(datetime, '2011-12-31 00:00:00.000')) THEN (250) WHEN (T1."datum" BETWEEN convert(datetime, '2012-01-01 00:00:00.000') AND convert(datetime, '2012-12-31 00:00:00.000')) THEN (249) ELSE (250) END as "Gesamt Arbeitstage"
-from "Arbeitstage" T1
-where (((((T1."bundeslaender_id" = '2') and (T1."arbeitstag_mofr" = 1)) and ((year(T1."datum")) = (year(((getdate()) - 1))))) and (T1."datum" <= ((getdate()) - 1))) or ((((T1."bundeslaender_id" = '2') and ((year(T1."datum")) = (year(((getdate()) - 1))))) and (T1."datum" <= ((getdate()) - 1))) and (T1."datum" IN (convert(datetime, '2022-01-01 00:00:00.000'),convert(datetime, '2023-01-01 00:00:00.000'),convert(datetime, '2024-01-01 00:00:00.000'),convert(datetime, '2025-01-01 00:00:00.000'),convert(datetime, '2026-01-01 00:00:00.000')))))
--- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
-) D1
+SELECT "Bundeslaender Id" AS "Bundeslaender Id",
+	"Datum" AS "Datum",
+	"Wochentage Id" AS "Wochentage Id",
+	"Arbeitstag Mofr" AS "Arbeitstag Mofr",
+	"Zaehler Mofr" AS "Zaehler Mofr",
+	"Summe Mofr" AS "Summe Mofr",
+	"Arbeitstag Mosa" AS "Arbeitstag Mosa",
+	"Zaehler Mosa" AS "Zaehler Mosa",
+	"Summe Mosa" AS "Summe Mosa",
+	"Feiertage Id" AS "Feiertage Id",
+	"Jahr" AS "Jahr",
+	CASE 
+		WHEN ("Arbeitstag Mofr" = 1)
+			THEN (RCOUNT("Arbeitstag Mofr"))
+		ELSE NULL
+		END AS "Arbeitstag Nr Jahr",
+	"Gesamt Arbeitstage" AS "Gesamt Arbeitstage"
+FROM (
+	SELECT T1."bundeslaender_id" AS "Bundeslaender Id",
+		T1."datum" AS "Datum",
+		T1."wochentage_id" AS "Wochentage Id",
+		T1."arbeitstag_mofr" AS "Arbeitstag Mofr",
+		T1."zaehler_mofr" AS "Zaehler Mofr",
+		T1."summe_mofr" AS "Summe Mofr",
+		T1."arbeitstag_mosa" AS "Arbeitstag Mosa",
+		T1."zaehler_mosa" AS "Zaehler Mosa",
+		T1."summe_mosa" AS "Summe Mosa",
+		T1."feiertage_id" AS "Feiertage Id",
+		(getdate()) - 1 AS "Jahr",
+		CASE 
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2008-01-01 00:00:00.000') AND convert(DATETIME, '2008-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2009-01-01 00:00:00.000') AND convert(DATETIME, '2009-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2010-01-01 00:00:00.000') AND convert(DATETIME, '2010-12-31 00:00:00.000'))
+				THEN (253)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2011-01-01 00:00:00.000') AND convert(DATETIME, '2011-12-31 00:00:00.000'))
+				THEN (250)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2012-01-01 00:00:00.000') AND convert(DATETIME, '2012-12-31 00:00:00.000'))
+				THEN (249)
+			ELSE (250)
+			END AS "Gesamt Arbeitstage"
+	FROM "Arbeitstage" T1
+	WHERE (
+			(
+				(
+					(
+						(T1."bundeslaender_id" = '2')
+						AND (T1."arbeitstag_mofr" = 1)
+						)
+					AND ((year(T1."datum")) = (year(((getdate()) - 1))))
+					)
+				AND (T1."datum" <= ((getdate()) - 1))
+				)
+			OR (
+				(
+					(
+						(T1."bundeslaender_id" = '2')
+						AND ((year(T1."datum")) = (year(((getdate()) - 1))))
+						)
+					AND (T1."datum" <= ((getdate()) - 1))
+					)
+				AND (T1."datum" IN (convert(DATETIME, '2022-01-01 00:00:00.000'), convert(DATETIME, '2023-01-01 00:00:00.000'), convert(DATETIME, '2024-01-01 00:00:00.000'), convert(DATETIME, '2025-01-01 00:00:00.000'), convert(DATETIME, '2026-01-01 00:00:00.000')))
+				)
+			)
+		-- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
+	) D1

+ 64 - 30
System/LOCOSOFT/IQD/Belege/current_date_Prognose_operativ_VJ-2.sql

@@ -1,30 +1,64 @@
-select "Bundeslaender Id" as "Bundeslaender Id",
-	   "Datum" as "Datum",
-	   "Wochentage Id" as "Wochentage Id",
-	   "Arbeitstag Mofr" as "Arbeitstag Mofr",
-	   "Zaehler Mofr" as "Zaehler Mofr",
-	   "Summe Mofr" as "Summe Mofr",
-	   "Arbeitstag Mosa" as "Arbeitstag Mosa",
-	   "Zaehler Mosa" as "Zaehler Mosa",
-	   "Summe Mosa" as "Summe Mosa",
-	   "Feiertage Id" as "Feiertage Id",
-	   "Jahr" as "Jahr",
-	   CASE WHEN ("Arbeitstag Mofr" = 1) THEN (RCOUNT("Arbeitstag Mofr")) ELSE null END as "Arbeitstag Nr Jahr",
-	   "Gesamt Arbeitstage" as "Gesamt Arbeitstage"
-from 
-(select T1."bundeslaender_id" as "Bundeslaender Id",
-	   T1."datum" as "Datum",
-	   T1."wochentage_id" as "Wochentage Id",
-	   T1."arbeitstag_mofr" as "Arbeitstag Mofr",
-	   T1."zaehler_mofr" as "Zaehler Mofr",
-	   T1."summe_mofr" as "Summe Mofr",
-	   T1."arbeitstag_mosa" as "Arbeitstag Mosa",
-	   T1."zaehler_mosa" as "Zaehler Mosa",
-	   T1."summe_mosa" as "Summe Mosa",
-	   T1."feiertage_id" as "Feiertage Id",
-	   (getdate()) - INTERVAL '000 00:00:00.000' as "Jahr",
-	   CASE WHEN (T1."datum" BETWEEN convert(datetime, '2008-01-01 00:00:00.000') AND convert(datetime, '2008-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2009-01-01 00:00:00.000') AND convert(datetime, '2009-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2010-01-01 00:00:00.000') AND convert(datetime, '2010-12-31 00:00:00.000')) THEN (253) WHEN (T1."datum" BETWEEN convert(datetime, '2011-01-01 00:00:00.000') AND convert(datetime, '2011-12-31 00:00:00.000')) THEN (250) WHEN (T1."datum" BETWEEN convert(datetime, '2012-01-01 00:00:00.000') AND convert(datetime, '2012-12-31 00:00:00.000')) THEN (249) ELSE (250) END as "Gesamt Arbeitstage"
-from "Arbeitstage" T1
-where ((((T1."bundeslaender_id" = '4') and ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))) and (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))) or ((((T1."bundeslaender_id" = '4') and ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))) and (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))) and (T1."datum" IN (convert(datetime, '2012-01-01 00:00:00.000'),convert(datetime, '2013-01-01 00:00:00.000'),convert(datetime, '2014-01-01 00:00:00.000'),convert(datetime, '2015-01-01 00:00:00.000'),convert(datetime, '2016-01-01 00:00:00.000'),convert(datetime, '2017-01-01 00:00:00.000')))))
--- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
-) D1
+SELECT "Bundeslaender Id" AS "Bundeslaender Id",
+	"Datum" AS "Datum",
+	"Wochentage Id" AS "Wochentage Id",
+	"Arbeitstag Mofr" AS "Arbeitstag Mofr",
+	"Zaehler Mofr" AS "Zaehler Mofr",
+	"Summe Mofr" AS "Summe Mofr",
+	"Arbeitstag Mosa" AS "Arbeitstag Mosa",
+	"Zaehler Mosa" AS "Zaehler Mosa",
+	"Summe Mosa" AS "Summe Mosa",
+	"Feiertage Id" AS "Feiertage Id",
+	"Jahr" AS "Jahr",
+	CASE 
+		WHEN ("Arbeitstag Mofr" = 1)
+			THEN (RCOUNT("Arbeitstag Mofr"))
+		ELSE NULL
+		END AS "Arbeitstag Nr Jahr",
+	"Gesamt Arbeitstage" AS "Gesamt Arbeitstage"
+FROM (
+	SELECT T1."bundeslaender_id" AS "Bundeslaender Id",
+		T1."datum" AS "Datum",
+		T1."wochentage_id" AS "Wochentage Id",
+		T1."arbeitstag_mofr" AS "Arbeitstag Mofr",
+		T1."zaehler_mofr" AS "Zaehler Mofr",
+		T1."summe_mofr" AS "Summe Mofr",
+		T1."arbeitstag_mosa" AS "Arbeitstag Mosa",
+		T1."zaehler_mosa" AS "Zaehler Mosa",
+		T1."summe_mosa" AS "Summe Mosa",
+		T1."feiertage_id" AS "Feiertage Id",
+		(getdate()) - INTERVAL '000 00:00:00.000' AS "Jahr",
+		CASE 
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2008-01-01 00:00:00.000') AND convert(DATETIME, '2008-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2009-01-01 00:00:00.000') AND convert(DATETIME, '2009-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2010-01-01 00:00:00.000') AND convert(DATETIME, '2010-12-31 00:00:00.000'))
+				THEN (253)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2011-01-01 00:00:00.000') AND convert(DATETIME, '2011-12-31 00:00:00.000'))
+				THEN (250)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2012-01-01 00:00:00.000') AND convert(DATETIME, '2012-12-31 00:00:00.000'))
+				THEN (249)
+			ELSE (250)
+			END AS "Gesamt Arbeitstage"
+	FROM "Arbeitstage" T1
+	WHERE (
+			(
+				(
+					(T1."bundeslaender_id" = '4')
+					AND ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))
+					)
+				AND (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))
+				)
+			OR (
+				(
+					(
+						(T1."bundeslaender_id" = '4')
+						AND ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))
+						)
+					AND (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))
+					)
+				AND (T1."datum" IN (convert(DATETIME, '2012-01-01 00:00:00.000'), convert(DATETIME, '2013-01-01 00:00:00.000'), convert(DATETIME, '2014-01-01 00:00:00.000'), convert(DATETIME, '2015-01-01 00:00:00.000'), convert(DATETIME, '2016-01-01 00:00:00.000'), convert(DATETIME, '2017-01-01 00:00:00.000')))
+				)
+			)
+		-- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
+	) D1

+ 108 - 108
System/LOCOSOFT/IQD/Belege/kontenrahmen_gc_struct_skr.sql

@@ -1,108 +1,108 @@
-select T1."KONTO_NR" as "Konto Nr",
-	   T1."KONTO_BEZEICHNUNG" as "Konto Bezeichnung",
-	   T1."KONTO_ART" as "Konto Art_ori",
-	   T1."KOSTENSTELLE" as "Kostenstelle",
-	   T1."STK" as "Stk",
-	   T1."KONTO_1" as "Konto 1",
-	   T1."KONTO_2" as "Konto 2",
-	   T1."KONTO_3" as "Konto 3",
-	   T1."KONTO_4" as "Konto 4",
-	   T1."KONTO_5" as "Konto 5",
-	   T1."EBENE1" as "Ebene1",
-	   T1."EBENE2" as "Ebene2",
-	   T1."EBENE3" as "Ebene3",
-	   T1."EBENE4" as "Ebene4",
-	   T1."EBENE5" as "Ebene5",
-	   T1."EBENE6" as "Ebene6",
-	   T1."EBENE7" as "Ebene7",
-	   T1."EBENE8" as "Ebene8",
-	   T1."EBENE9" as "Ebene9",
-	   T1."EBENE10" as "Ebene10",
-	   T1."EBENE11" as "Ebene11",
-	   T1."EBENE12" as "Ebene12",
-	   T1."EBENE13" as "Ebene13",
-	   T1."EBENE14" as "Ebene14",
-	   T1."EBENE15" as "Ebene15",
-	   T1."EBENE16" as "Ebene16",
-	   T1."EBENE17" as "Ebene17",
-	   T1."EBENE18" as "Ebene18",
-	   T1."EBENE19" as "Ebene19",
-	   T1."EBENE20" as "Ebene20",
-	   T1."EBENE21" as "Ebene21",
-	   T1."EBENE22" as "Ebene22",
-	   T1."EBENE23" as "Ebene23",
-	   T1."EBENE24" as "Ebene24",
-	   T1."EBENE25" as "Ebene25",
-	   T1."EBENE26" as "Ebene26",
-	   T1."EBENE27" as "Ebene27",
-	   T1."EBENE28" as "Ebene28",
-	   T1."EBENE29" as "Ebene29",
-	   T1."EBENE30" as "Ebene30",
-	   T1."EBENE31" as "Ebene31",
-	   T1."EBENE32" as "Ebene32",
-	   T1."EBENE33" as "Ebene33",
-	   T1."EBENE34" as "Ebene34",
-	   T1."EBENE35" as "Ebene35",
-	   T1."EBENE36" as "Ebene36",
-	   T1."EBENE37" as "Ebene37",
-	   T1."EBENE38" as "Ebene38",
-	   T1."EBENE39" as "Ebene39",
-	   T1."EBENE40" as "Ebene40",
-	   T1."EBENE41" as "Ebene41",
-	   T1."EBENE42" as "Ebene42",
-	   T1."EBENE43" as "Ebene43",
-	   T1."EBENE44" as "Ebene44",
-	   T1."EBENE45" as "Ebene45",
-	   T1."EBENE46" as "Ebene46",
-	   T1."EBENE47" as "Ebene47",
-	   T1."EBENE48" as "Ebene48",
-	   T1."EBENE49" as "Ebene49",
-	   T1."EBENE50" as "Ebene50",
-	   T1."EBENE51" as "Ebene51",
-	   T1."EBENE52" as "Ebene52",
-	   T1."EBENE53" as "Ebene53",
-	   T1."EBENE54" as "Ebene54",
-	   T1."EBENE55" as "Ebene55",
-	   T1."EBENE56" as "Ebene56",
-	   T1."EBENE57" as "Ebene57",
-	   T1."EBENE58" as "Ebene58",
-	   T1."EBENE59" as "Ebene59",
-	   T1."EBENE60" as "Ebene60",
-	   T1."EBENE61" as "Ebene61",
-	   T1."EBENE62" as "Ebene62",
-	   T1."EBENE63" as "Ebene63",
-	   T1."EBENE64" as "Ebene64",
-	   T1."EBENE65" as "Ebene65",
-	   T1."EBENE66" as "Ebene66",
-	   T1."EBENE67" as "Ebene67",
-	   T1."EBENE68" as "Ebene68",
-	   T1."EBENE69" as "Ebene69",
-	   T1."EBENE70" as "Ebene70",
-	   T1."EBENE71" as "Ebene71",
-	   T1."EBENE72" as "Ebene72",
-	   T1."EBENE73" as "Ebene73",
-	   T1."EBENE74" as "Ebene74",
-	   T1."EBENE75" as "Ebene75",
-	   T1."EBENE76" as "Ebene76",
-	   T1."EBENE77" as "Ebene77",
-	   T1."EBENE78" as "Ebene78",
-	   T1."EBENE79" as "Ebene79",
-	   T1."EBENE80" as "Ebene80",
-	   T1."EBENE81" as "Ebene81",
-	   T1."EBENE82" as "Ebene82",
-	   T1."EBENE83" as "Ebene83",
-	   T1."EBENE84" as "Ebene84",
-	   T1."EBENE85" as "Ebene85",
-	   T1."EBENE86" as "Ebene86",
-	   T1."EBENE87" as "Ebene87",
-	   T1."EBENE88" as "Ebene88",
-	   T1."EBENE89" as "Ebene89",
-	   T1."EBENE90" as "Ebene90",
-	   (len(T1."KONTO_NR" + 'Z') - 1) as "Anzahl Stellen_Konto_Nr",
-	   T1."KONTO_ART" as "Konto Art",
-	   T1."KONTO_ART" as "GuV_Bilanz",
-	   T1."KONTO_NR" as "Acct Nr",
-	   T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" as "Konto",
-	   (left(T1."KONTO_NR",1)) as "Susa"
-from "Kontenrahmen.csv" T1
-where (((left(T1."KONTO_NR",1))) <> '9')
+SELECT T1."KONTO_NR" AS "Konto Nr",
+	T1."KONTO_BEZEICHNUNG" AS "Konto Bezeichnung",
+	T1."KONTO_ART" AS "Konto Art_ori",
+	T1."KOSTENSTELLE" AS "Kostenstelle",
+	T1."STK" AS "Stk",
+	T1."KONTO_1" AS "Konto 1",
+	T1."KONTO_2" AS "Konto 2",
+	T1."KONTO_3" AS "Konto 3",
+	T1."KONTO_4" AS "Konto 4",
+	T1."KONTO_5" AS "Konto 5",
+	T1."EBENE1" AS "Ebene1",
+	T1."EBENE2" AS "Ebene2",
+	T1."EBENE3" AS "Ebene3",
+	T1."EBENE4" AS "Ebene4",
+	T1."EBENE5" AS "Ebene5",
+	T1."EBENE6" AS "Ebene6",
+	T1."EBENE7" AS "Ebene7",
+	T1."EBENE8" AS "Ebene8",
+	T1."EBENE9" AS "Ebene9",
+	T1."EBENE10" AS "Ebene10",
+	T1."EBENE11" AS "Ebene11",
+	T1."EBENE12" AS "Ebene12",
+	T1."EBENE13" AS "Ebene13",
+	T1."EBENE14" AS "Ebene14",
+	T1."EBENE15" AS "Ebene15",
+	T1."EBENE16" AS "Ebene16",
+	T1."EBENE17" AS "Ebene17",
+	T1."EBENE18" AS "Ebene18",
+	T1."EBENE19" AS "Ebene19",
+	T1."EBENE20" AS "Ebene20",
+	T1."EBENE21" AS "Ebene21",
+	T1."EBENE22" AS "Ebene22",
+	T1."EBENE23" AS "Ebene23",
+	T1."EBENE24" AS "Ebene24",
+	T1."EBENE25" AS "Ebene25",
+	T1."EBENE26" AS "Ebene26",
+	T1."EBENE27" AS "Ebene27",
+	T1."EBENE28" AS "Ebene28",
+	T1."EBENE29" AS "Ebene29",
+	T1."EBENE30" AS "Ebene30",
+	T1."EBENE31" AS "Ebene31",
+	T1."EBENE32" AS "Ebene32",
+	T1."EBENE33" AS "Ebene33",
+	T1."EBENE34" AS "Ebene34",
+	T1."EBENE35" AS "Ebene35",
+	T1."EBENE36" AS "Ebene36",
+	T1."EBENE37" AS "Ebene37",
+	T1."EBENE38" AS "Ebene38",
+	T1."EBENE39" AS "Ebene39",
+	T1."EBENE40" AS "Ebene40",
+	T1."EBENE41" AS "Ebene41",
+	T1."EBENE42" AS "Ebene42",
+	T1."EBENE43" AS "Ebene43",
+	T1."EBENE44" AS "Ebene44",
+	T1."EBENE45" AS "Ebene45",
+	T1."EBENE46" AS "Ebene46",
+	T1."EBENE47" AS "Ebene47",
+	T1."EBENE48" AS "Ebene48",
+	T1."EBENE49" AS "Ebene49",
+	T1."EBENE50" AS "Ebene50",
+	T1."EBENE51" AS "Ebene51",
+	T1."EBENE52" AS "Ebene52",
+	T1."EBENE53" AS "Ebene53",
+	T1."EBENE54" AS "Ebene54",
+	T1."EBENE55" AS "Ebene55",
+	T1."EBENE56" AS "Ebene56",
+	T1."EBENE57" AS "Ebene57",
+	T1."EBENE58" AS "Ebene58",
+	T1."EBENE59" AS "Ebene59",
+	T1."EBENE60" AS "Ebene60",
+	T1."EBENE61" AS "Ebene61",
+	T1."EBENE62" AS "Ebene62",
+	T1."EBENE63" AS "Ebene63",
+	T1."EBENE64" AS "Ebene64",
+	T1."EBENE65" AS "Ebene65",
+	T1."EBENE66" AS "Ebene66",
+	T1."EBENE67" AS "Ebene67",
+	T1."EBENE68" AS "Ebene68",
+	T1."EBENE69" AS "Ebene69",
+	T1."EBENE70" AS "Ebene70",
+	T1."EBENE71" AS "Ebene71",
+	T1."EBENE72" AS "Ebene72",
+	T1."EBENE73" AS "Ebene73",
+	T1."EBENE74" AS "Ebene74",
+	T1."EBENE75" AS "Ebene75",
+	T1."EBENE76" AS "Ebene76",
+	T1."EBENE77" AS "Ebene77",
+	T1."EBENE78" AS "Ebene78",
+	T1."EBENE79" AS "Ebene79",
+	T1."EBENE80" AS "Ebene80",
+	T1."EBENE81" AS "Ebene81",
+	T1."EBENE82" AS "Ebene82",
+	T1."EBENE83" AS "Ebene83",
+	T1."EBENE84" AS "Ebene84",
+	T1."EBENE85" AS "Ebene85",
+	T1."EBENE86" AS "Ebene86",
+	T1."EBENE87" AS "Ebene87",
+	T1."EBENE88" AS "Ebene88",
+	T1."EBENE89" AS "Ebene89",
+	T1."EBENE90" AS "Ebene90",
+	(len(T1."KONTO_NR" + 'Z') - 1) AS "Anzahl Stellen_Konto_Nr",
+	T1."KONTO_ART" AS "Konto Art",
+	T1."KONTO_ART" AS "GuV_Bilanz",
+	T1."KONTO_NR" AS "Acct Nr",
+	T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" AS "Konto",
+	(left(T1."KONTO_NR", 1)) AS "Susa"
+FROM "Kontenrahmen.csv" T1
+WHERE (((left(T1."KONTO_NR", 1))) <> '9')

+ 194 - 69
System/LOCOSOFT/IQD/Belege/loc_belege.sql

@@ -1,69 +1,194 @@
-select T1."accounting_date" as "Accounting Date",
-	   T1."document_type" as "Document Type",
-	   (T1."document_number") as "Document Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."customer_number" as "Customer Number",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."document_date" as "Document Date",
-	   T1."posted_value" as "Posted Value",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_count" as "Posted Count",
-	   T1."branch_number" as "Branch Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."employee_number" as "Employee Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."journal_page" as "Journal Page",
-	   T1."journal_line" as "Journal Line",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."term_of_payment" as "Term Of Payment",
-	   CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END as "Posting Text",
-	   CASE WHEN (T1."vehicle_reference" IS NULL) THEN (' - ') ELSE (T1."vehicle_reference") END as "Vehicle Reference",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."vat_key" as "Vat Key",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   '0' + (((T1."skr51_branch"))) as "Betrieb",
-	   CASE WHEN (((CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) = 'Saldenübernahme CDK') or (T1."accounting_date" = convert(date, '2019-01-01'))) THEN (convert(date, '2019-12-01')) ELSE (T1."accounting_date") END as "Bookkeep Date",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, (CASE WHEN (((CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) = 'Saldenübernahme CDK') or (T1."accounting_date" = convert(date, '2019-01-01'))) THEN (convert(date, '2019-12-01')) ELSE (T1."accounting_date") END))))) <= 90) THEN ((convert(varchar(50), ((T1."document_number")))) + ' - ' + (CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) + '/' + (CASE WHEN (T1."vehicle_reference" IS NULL) THEN (' - ') ELSE (T1."vehicle_reference") END) + ' - ' + (left((((T1."employee_number")))))) ELSE ('Buchungen älter 90 Tage') END as "Text",
-	   '1' as "Mandant",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   (left((((T1."nominal_account_number"))),4)) + ' - ' + T2."account_description" as "Konto_mit_Bezeichnung",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   (left((((T1."nominal_account_number"))),1)) as "Susa",
-	   CASE WHEN (T1."skr51_cost_center" <> 0) THEN ((rtrim((((T1."nominal_account_number"))))) + '_' + (rtrim((((T1."skr51_cost_center")))))) ELSE ((((T1."nominal_account_number")))) END as "Acct Nr"
-from "dbo"."nominal_accounts" T2,
-	("dbo"."journal_accountings" T1 left outer join "dbo"."accounts_characteristics" T3 on (((((T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref") and (T3."skr51_branch" = T1."skr51_branch")) and (T3."skr51_make" = T1."skr51_make")) and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((T2."is_profit_loss_account" = 'J') and (not (CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) IN ('G&V-Abschlussbuchung')))
--- order by "Nominal Account Number" asc
+SELECT T1."accounting_date" AS "Accounting Date",
+	T1."document_type" AS "Document Type",
+	(T1."document_number") AS "Document Number",
+	T1."position_in_document" AS "Position In Document",
+	T1."customer_number" AS "Customer Number",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."is_balanced" AS "Is Balanced",
+	T1."clearing_number" AS "Clearing Number",
+	T1."document_date" AS "Document Date",
+	T1."posted_value" AS "Posted Value",
+	T1."debit_or_credit" AS "Debit Or Credit",
+	T1."posted_count" AS "Posted Count",
+	T1."branch_number" AS "Branch Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."nominal_contra_account" AS "Nominal Contra Account",
+	T1."contra_account_text" AS "Contra Account Text",
+	T1."account_form_page_number" AS "Account Form Page Number",
+	T1."account_form_page_line" AS "Account Form Page Line",
+	T1."serial_number_each_month" AS "Serial Number Each Month",
+	T1."employee_number" AS "Employee Number",
+	T1."invoice_date" AS "Invoice Date",
+	T1."invoice_number" AS "Invoice Number",
+	T1."dunning_level" AS "Dunning Level",
+	T1."last_dunning_date" AS "Last Dunning Date",
+	T1."journal_page" AS "Journal Page",
+	T1."journal_line" AS "Journal Line",
+	T1."cash_discount" AS "Cash Discount",
+	T1."term_of_payment" AS "Term Of Payment",
+	CASE 
+		WHEN (T1."posting_text" IS NULL)
+			THEN (' - ')
+		ELSE (T1."posting_text")
+		END AS "Posting Text",
+	CASE 
+		WHEN (T1."vehicle_reference" IS NULL)
+			THEN (' - ')
+		ELSE (T1."vehicle_reference")
+		END AS "Vehicle Reference",
+	T1."vat_id_number" AS "Vat Id Number",
+	T1."account_statement_number" AS "Account Statement Number",
+	T1."account_statement_page" AS "Account Statement Page",
+	T1."vat_key" AS "Vat Key",
+	T1."days_for_cash_discount" AS "Days For Cash Discount",
+	T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+	T1."skr51_branch" AS "Skr51 Branch",
+	T1."skr51_make" AS "Skr51 Make",
+	T1."skr51_cost_center" AS "Skr51 Cost Center",
+	T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+	T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+	T1."previously_used_account_no" AS "Previously Used Account No",
+	T1."free_form_accounting_text" AS "Free Form Accounting Text",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN ('Neuwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+			THEN ('Gebrauchtwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+			THEN ('Teile & Zubeh�r')
+		WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+			THEN ('Service')
+		WHEN (T1."skr51_cost_unit" = 0)
+			THEN ('Ohne Kostentr�ger')
+		ELSE NULL
+		END AS "Free Form Document Text",
+	T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+	T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+	'0' + (((T1."skr51_branch"))) AS "Betrieb",
+	CASE 
+		WHEN (
+				(
+					(
+						CASE 
+							WHEN (T1."posting_text" IS NULL)
+								THEN (' - ')
+							ELSE (T1."posting_text")
+							END
+						) = 'Saldenübernahme CDK'
+					)
+				OR (T1."accounting_date" = convert(DATE, '2019-01-01'))
+				)
+			THEN (convert(DATE, '2019-12-01'))
+		ELSE (T1."accounting_date")
+		END AS "Bookkeep Date",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+	CASE 
+		WHEN (
+				(
+					- 1 * datediff(day, (getdate()), (
+							convert(DATETIME, (
+									CASE 
+										WHEN (
+												(
+													(
+														CASE 
+															WHEN (T1."posting_text" IS NULL)
+																THEN (' - ')
+															ELSE (T1."posting_text")
+															END
+														) = 'Saldenübernahme CDK'
+													)
+												OR (T1."accounting_date" = convert(DATE, '2019-01-01'))
+												)
+											THEN (convert(DATE, '2019-12-01'))
+										ELSE (T1."accounting_date")
+										END
+									))
+							))
+					) <= 90
+				)
+			THEN (
+					(convert(VARCHAR(50), ((T1."document_number")))) + ' - ' + (
+						CASE 
+							WHEN (T1."posting_text" IS NULL)
+								THEN (' - ')
+							ELSE (T1."posting_text")
+							END
+						) + '/' + (
+						CASE 
+							WHEN (T1."vehicle_reference" IS NULL)
+								THEN (' - ')
+							ELSE (T1."vehicle_reference")
+							END
+						) + ' - ' + (left((((T1."employee_number")))))
+					)
+		ELSE ('Buchungen �lter 90 Tage')
+		END AS "Text",
+	'1' AS "Mandant",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_value" / 100 * - 1)
+		ELSE (T1."posted_value" / 100)
+		END AS "Betrag",
+	(left((((T1."nominal_account_number"))), 4)) + ' - ' + T2."account_description" AS "Konto_mit_Bezeichnung",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_count" / 100 * - 1)
+		ELSE (T1."posted_count" / 100)
+		END AS "Menge",
+	(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+	(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+	(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+	(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+	(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+		ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+		END AS "Kostentr�ger",
+	CASE 
+		WHEN (T2."is_profit_loss_account" = 'J')
+			THEN ('2')
+		ELSE ('1')
+		END AS "GuV_Bilanz",
+	(left((((T1."nominal_account_number"))), 1)) AS "Susa",
+	CASE 
+		WHEN (T1."skr51_cost_center" <> 0)
+			THEN ((rtrim((((T1."nominal_account_number"))))) + '_' + (rtrim((((T1."skr51_cost_center"))))))
+		ELSE ((((T1."nominal_account_number"))))
+		END AS "Acct Nr"
+FROM "dbo"."nominal_accounts" T2,
+	(
+		"dbo"."journal_accountings" T1 LEFT JOIN "dbo"."accounts_characteristics" T3 ON (
+				(
+					(
+						(
+							(T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+							AND (T3."skr51_branch" = T1."skr51_branch")
+							)
+						AND (T3."skr51_make" = T1."skr51_make")
+						)
+					AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+					)
+				AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+				)
+			AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+		)
+WHERE (
+		(T2."nominal_account_number" = T1."nominal_account_number")
+		AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+		)
+	AND (
+		(T2."is_profit_loss_account" = 'J')
+		AND (
+			NOT (
+				CASE 
+					WHEN (T1."posting_text" IS NULL)
+						THEN (' - ')
+					ELSE (T1."posting_text")
+					END
+				) IN ('G&V-Abschlussbuchung')
+			)
+		)
+	-- order by "Nominal Account Number" asc

+ 167 - 70
System/LOCOSOFT/IQD/Belege/loc_belege_bilanz.sql

@@ -1,70 +1,167 @@
-select T1."accounting_date" as "Accounting Date",
-	   T1."document_type" as "Document Type",
-	   T1."document_number" as "Document Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."customer_number" as "Customer Number",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."document_date" as "Document Date",
-	   T1."posted_value" as "Posted Value",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_count" as "Posted Count",
-	   T1."branch_number" as "Branch Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."employee_number" as "Employee Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."journal_page" as "Journal Page",
-	   T1."journal_line" as "Journal Line",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."term_of_payment" as "Term Of Payment",
-	   '-' as "Posting Text",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."vat_key" as "Vat Key",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   (left(T1."free_form_accounting_text",100)) as "Free Form Accounting Text",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."accounting_date" as "Bookkeep Date",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   '' as "Text",
-	   '1' as "Mandant",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   (rtrim((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END))) + ' - ' + T2."account_description" as "Konto_mit_Bezeichnung",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   (substring((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END), 1, 1)) as "Susa",
-	   CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END as "Acct Nr",
-	   (len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1) as "Stellen_Konto_Nr"
-from "dbo"."nominal_accounts" T2,
-	("dbo"."journal_accountings" T1 left outer join "dbo"."accounts_characteristics" T3 on (((((T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref") and (T3."skr51_branch" = T1."skr51_branch")) and (T3."skr51_make" = T1."skr51_make")) and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((T2."is_profit_loss_account" = 'N') and (not ((rtrim((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END))) + ' - ' + T2."account_description") LIKE '9%'))
--- order by "Nominal Account Number" asc
+SELECT T1."accounting_date" AS "Accounting Date",
+	T1."document_type" AS "Document Type",
+	T1."document_number" AS "Document Number",
+	T1."position_in_document" AS "Position In Document",
+	T1."customer_number" AS "Customer Number",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."is_balanced" AS "Is Balanced",
+	T1."clearing_number" AS "Clearing Number",
+	T1."document_date" AS "Document Date",
+	T1."posted_value" AS "Posted Value",
+	T1."debit_or_credit" AS "Debit Or Credit",
+	T1."posted_count" AS "Posted Count",
+	T1."branch_number" AS "Branch Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."nominal_contra_account" AS "Nominal Contra Account",
+	T1."contra_account_text" AS "Contra Account Text",
+	T1."account_form_page_number" AS "Account Form Page Number",
+	T1."account_form_page_line" AS "Account Form Page Line",
+	T1."serial_number_each_month" AS "Serial Number Each Month",
+	T1."employee_number" AS "Employee Number",
+	T1."invoice_date" AS "Invoice Date",
+	T1."invoice_number" AS "Invoice Number",
+	T1."dunning_level" AS "Dunning Level",
+	T1."last_dunning_date" AS "Last Dunning Date",
+	T1."journal_page" AS "Journal Page",
+	T1."journal_line" AS "Journal Line",
+	T1."cash_discount" AS "Cash Discount",
+	T1."term_of_payment" AS "Term Of Payment",
+	'-' AS "Posting Text",
+	T1."vehicle_reference" AS "Vehicle Reference",
+	T1."vat_id_number" AS "Vat Id Number",
+	T1."account_statement_number" AS "Account Statement Number",
+	T1."account_statement_page" AS "Account Statement Page",
+	T1."vat_key" AS "Vat Key",
+	T1."days_for_cash_discount" AS "Days For Cash Discount",
+	T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+	T1."skr51_branch" AS "Skr51 Branch",
+	T1."skr51_make" AS "Skr51 Make",
+	T1."skr51_cost_center" AS "Skr51 Cost Center",
+	T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+	T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+	T1."previously_used_account_no" AS "Previously Used Account No",
+	(left(T1."free_form_accounting_text", 100)) AS "Free Form Accounting Text",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN ('Neuwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+			THEN ('Gebrauchtwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+			THEN ('Teile & Zubeh�r')
+		WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+			THEN ('Service')
+		WHEN (T1."skr51_cost_unit" = 0)
+			THEN ('Ohne Kostentr�ger')
+		ELSE NULL
+		END AS "Free Form Document Text",
+	T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+	T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+	('0' + (((T1."skr51_branch")))) AS "Betrieb",
+	T1."accounting_date" AS "Bookkeep Date",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+	'' AS "Text",
+	'1' AS "Mandant",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_value" / 100 * - 1)
+		ELSE (T1."posted_value" / 100)
+		END AS "Betrag",
+	(
+		rtrim((
+				CASE 
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+						THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+						THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+						THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+					ELSE ((rtrim((((T1."nominal_account_number"))))))
+					END
+				))
+		) + ' - ' + T2."account_description" AS "Konto_mit_Bezeichnung",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_count" / 100 * - 1)
+		ELSE (T1."posted_count" / 100)
+		END AS "Menge",
+	(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+	(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+	(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+	(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+	(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+		ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+		END AS "Kostentr�ger",
+	CASE 
+		WHEN (T2."is_profit_loss_account" = 'J')
+			THEN ('2')
+		ELSE ('1')
+		END AS "GuV_Bilanz",
+	(
+		substring((
+				CASE 
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+						THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+						THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+						THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+					ELSE ((rtrim((((T1."nominal_account_number"))))))
+					END
+				), 1, 1)
+		) AS "Susa",
+	CASE 
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+			THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+			THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+			THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+		ELSE ((rtrim((((T1."nominal_account_number"))))))
+		END AS "Acct Nr",
+	(len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1) AS "Stellen_Konto_Nr"
+FROM "dbo"."nominal_accounts" T2,
+	(
+		"dbo"."journal_accountings" T1 LEFT JOIN "dbo"."accounts_characteristics" T3 ON (
+				(
+					(
+						(
+							(T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+							AND (T3."skr51_branch" = T1."skr51_branch")
+							)
+						AND (T3."skr51_make" = T1."skr51_make")
+						)
+					AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+					)
+				AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+				)
+			AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+		)
+WHERE (
+		(T2."nominal_account_number" = T1."nominal_account_number")
+		AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+		)
+	AND (
+		(T2."is_profit_loss_account" = 'N')
+		AND (
+			NOT (
+				(
+					rtrim((
+							CASE 
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+									THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+									THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+									THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+								ELSE ((rtrim((((T1."nominal_account_number"))))))
+								END
+							))
+					) + ' - ' + T2."account_description"
+				) LIKE '9%'
+			)
+		)
+	-- order by "Nominal Account Number" asc

+ 1198 - 406
System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK.sql

@@ -1,406 +1,1198 @@
-select "Accounting Date" as "Accounting Date",
-	   "Document Type" as "Document Type",
-	   "Document Number" as "Document Number",
-	   "Position In Document" as "Position In Document",
-	   (day((convert(datetime, (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))) - c142)) 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" as "Betrag",
-	   "Konto_mit_Bezeichnung" as "Konto_mit_Bezeichnung",
-	   "Menge_2" as "Menge_2",
-	   "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" as "Acct Nr",
-	   "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 ('FZG-Geschäft') ELSE ('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",
-	   SUM("Betrag") OVER (partition by "Vehicle Reference_berechnet") 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",
-	   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 ('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 ('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",
-	   "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",
-	   COUNT("Accounting Date") OVER (partition by "Vehicle Reference_berechnet") as "Menge_2",
-	   ("Menge_1" / COUNT("Accounting Date") OVER (partition by "Vehicle Reference_berechnet")) as "Menge",
-	   "Vehicle Reference_berechnet" as "Vehicle Reference_berechnet",
-	   "var. Kosten" as "var. Kosten",
-	   "Dealer Vehicle Type_Vehicles" as "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles" as "Dealer Vehicle Number_Vehicles"
-from 
-(select "Accounting Date",
-	   "Document Type",
-	   "Document Number",
-	   "Position In Document",
-	   "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_journal_accountings",
-	   "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",
-	   "Nom_Account_Is Profit Loss Account",
-	   "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Marke",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag",
-	   "Konto_mit_Bezeichnung",
-	   "Menge_2",
-	   "Stellen Cost Center",
-	   "KST",
-	   "Stellen Sales Channel",
-	   "Absatzkanal",
-	   "Stellen Cost Unit",
-	   "Kostenträger_mit_Null",
-	   "Kostenträger",
-	   "GuV_Bilanz",
-	   "Susa",
-	   "Nominal Account Number" as "Acct Nr",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   "FZG_ja_nein",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Invoice Date_Invoices",
-	   "Is Canceled_Invoices",
-	   "Vehicle Number_Invoices",
-	   "Invoice_Date_1",
-	   "Internal Number_Vehicles",
-	   "Vin_Vehicles",
-	   "Dealer Vehicle Type",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Make Number_Vehicles",
-	   "Description_Make",
-	   "Fabrikat",
-	   "Model Code_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Owner Number_Vehicles",
-	   "Holder Number_Vehicles",
-	   "First Name_Owner_Customer",
-	   "Family Name_Owner_Customer",
-	   "First Name_Holder_Customer",
-	   "Family Name_Holder_Customer",
-	   "Kunde",
-	   "Sales_Channel_FZG",
-	   "Rechtseinheit" as "Hauptbetrieb",
-	   "Betrieb" as "Standort_alt",
-	   "Salesman Number_Vehicles",
-	   "Employee Number_Employees_Salesman",
-	   "Name_Employees_Salesman",
-	   "Verkäufer",
-	   "Standort_FZG_Verkauf",
-	   "Standort",
-	   "FZG",
-	   "Konto_mit_Bezeichnung" as "Konto",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Body Paint Description" as "Farbe",
-	   "Erlös FZG",
-	   "Nachlass",
-	   "VAK FZG",
-	   "Erlös Zulass.",
-	   "VAK Zulass.",
-	   "Erlös Gar.",
-	   "VAK Gar.",
-	   "Erlös Nachr./Aufb.",
-	   "VAK Nachr./Aufb.",
-	   "VAK Fertigm.",
-	   "Erlös Überf.",
-	   "VAK Überf.",
-	   "Erlös Sonst._8900",
-	   "Erlös Sonst.",
-	   "VAK Sonst.",
-	   "VAK intern",
-	   "Erlös Prov.",
-	   "Boni/VK-Hilfen",
-	   "Erlös ges.",
-	   "VAK ges.",
-	   1 as "Menge_1",
-	   "Vehicle Reference_berechnet",
-	   "var. Kosten",
-	   "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles",
-	   c152 as c142,
-	   MAX("Vehicle Number_Invoices") OVER (partition by "Vehicle Reference_berechnet") as c143,
-	   (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 c144,
-	   (CASE WHEN ((SUM("FZG_ja_nein") OVER (partition by "Vehicle Reference_berechnet")) > 0) THEN ('FZG-Geschäft') ELSE ('Buchungen ohne FZG-Geschäft') END) as c145
-from 
-(select (left(T1."vehicle_reference",7)) + '_' + (right(T1."vehicle_reference",17)) as "Vehicle Reference_berechnet",
-	   (((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) as "Invoice Date_journal_accountings",
-	   T2."in_buy_invoice_no_date" as c152,
-	   T8."dealer_vehicle_number" as "Dealer Vehicle Number_Vehicles",
-	   T8."dealer_vehicle_type" as "Dealer Vehicle Type_Vehicles",
-	   CASE WHEN (T1."nominal_account_number" IN (4300,4560,4580,4530,4540,4590,4591,4690,4621,4622,4623,4500,4501,4502,4503,4504,5501)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "var. Kosten",
-	   (CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK ges.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös ges.",
-	   CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "Boni/VK-Hilfen",
-	   CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Prov.",
-	   CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK intern",
-	   (CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK Sonst.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös Sonst.",
-	   CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Sonst._8900",
-	   CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Fertigm.",
-	   CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK FZG",
-	   CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Nachlass",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös FZG",
-	   T8."body_paint_description" as "Body Paint Description",
-	   T8."body_paint_code" as "Body Paint Code",
-	   ((left((((T1."nominal_account_number"))),4)) + ' - ' + T4."account_description") as "Konto_mit_Bezeichnung",
-	   (left(((left(T1."vehicle_reference",7)) + '_' + (right(T1."vehicle_reference",17))),7)) + ' / ' + ((left((((T8."internal_number"))),5))) + ' - ' + T10."description" + ' - ' + (((left((((T8."holder_number"))),7)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END) + ' ' + T3."family_name")) as "FZG",
-	   CASE WHEN (T2."out_subsidiary" IS NULL) THEN ((('0' + (((T1."skr51_branch")))))) ELSE ('0' + (((T2."out_subsidiary")))) END as "Standort",
-	   T2."out_subsidiary" as "Standort_FZG_Verkauf",
-	   (left((((T8."salesman_number"))),4)) + ' - ' + T12."name" as "Verkäufer",
-	   T12."name" as "Name_Employees_Salesman",
-	   T12."employee_number" as "Employee Number_Employees_Salesman",
-	   T8."salesman_number" as "Salesman Number_Vehicles",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T7."is_canceled" <> '1')) THEN (T1."skr51_sales_channel") ELSE null END as "Sales_Channel_FZG",
-	   ((left((((T8."holder_number"))),7)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END) + ' ' + T3."family_name") as "Kunde",
-	   T3."family_name" as "Family Name_Holder_Customer",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END as "First Name_Holder_Customer",
-	   T11."family_name" as "Family Name_Owner_Customer",
-	   T11."first_name" as "First Name_Owner_Customer",
-	   T8."holder_number" as "Holder Number_Vehicles",
-	   T8."owner_number" as "Owner Number_Vehicles",
-	   (left((ucase(T10."description")),3)) as "Model",
-	   T10."description" as "Model_Detail",
-	   T10."model_code" as "Model Code_Models",
-	   CASE WHEN (((rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description") = '3 - Husqvarna') THEN ('Husqvarna') ELSE (T9."description") END as "Fabrikat",
-	   T9."description" as "Description_Make",
-	   T8."make_number" as "Make Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (left(T1."vehicle_reference",1)) as "Dealer Vehicle Type",
-	   T8."vin" as "Vin_Vehicles",
-	   T8."internal_number" as "Internal Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) THEN (((((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))))) ELSE null END as "Invoice_Date_1",
-	   T7."vehicle_number" as "Vehicle Number_Invoices",
-	   T7."is_canceled" as "Is Canceled_Invoices",
-	   T7."invoice_date" as "Invoice Date_Invoices",
-	   T7."invoice_number" as "Invoice Number_Invoices",
-	   T7."invoice_type" as "Invoice Type_Invoices",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END as "FZG_ja_nein",
-	   T6."document_type_description" as "Document Type Description",
-	   T6."document_type_in_journal" as "Document Type In Journal",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   (left((((T1."nominal_account_number"))),1)) as "Susa",
-	   CASE WHEN (T4."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name")) END as "Kostenträger",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T5."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T5."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge_2",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) <= 360) THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (rtrim((((T1."employee_number")))))) ELSE ('Buchungen älter 360 Tage') END as "Text",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description" as "Marke",
-	   T1."accounting_date" as "Accounting Date",
-	   T4."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T3."zip_code" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."position_in_document" as "Position In Document",
-	   T1."document_number" as "Document Number",
-	   T1."document_type" as "Document Type"
-from "nominal_accounts" T4,
-	(((((((((("journal_accountings" T1 left outer join "vehicles" T8 on (right(T1."vehicle_reference",17)) = T8."vin") left outer join "LOCOSOFT"."dbo"."dealer_vehicles" T2 on (T8."dealer_vehicle_type" = T2."dealer_vehicle_type") and (T8."dealer_vehicle_number" = T2."dealer_vehicle_number")) left outer join "customers_suppliers" T3 on T3."customer_number" = T8."holder_number") left outer join "accounts_characteristics" T5 on (((((T5."skr51_make" = T1."skr51_make") and (T5."skr51_cost_center" = T1."skr51_cost_center")) and (T5."skr51_sales_channel" = T1."skr51_sales_channel")) and (T5."skr51_cost_unit" = T1."skr51_cost_unit")) and (T5."skr51_branch" = T1."skr51_branch")) and (T5."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")) left outer join "document_types" T6 on T6."document_type_in_journal" = T1."document_type") left outer join "invoices" T7 on T7."invtype_invnr" = T1."invoice_number") left outer join "makes" T9 on T8."make_number" = T9."make_number") left outer join "models" T10 on (T8."make_number" = T10."make_number") and (T8."model_code" = T10."model_code")) left outer join "customers_suppliers" T11 on T11."customer_number" = T8."owner_number") left outer join "employees" T12 on (T8."salesman_number" <> 0) and (T8."salesman_number" = T12."salesman_number"))
-where ((T4."nominal_account_number" = T1."nominal_account_number") and (T4."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and (((T4."is_profit_loss_account" = 'J') and (T1."vehicle_reference" <> ' ')) and (T1."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,5001,5005,5006,5007,5008,5003,5004,4300,4560,4580,4530,4540,4590,4591,4690,4621,4622,4623,4500,4501,4502,4503,4504,5501,7220,7800,7850,7801,7802,7803,7804,7805,7806,7807,7820,7825,7827,7808,8030,7030,8870,8871,8872,8220,7220)))
-) D2
-) D1
-where (((((c143 = "Internal Number_Vehicles") or ("Internal Number_Vehicles" IS NULL)) or (c143 IS NULL)) and (c144 >= convert(date, '2017-01-01'))) and (c145 = 'FZG-Geschäft'))
--- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
+SELECT "Accounting Date" AS "Accounting Date",
+	"Document Type" AS "Document Type",
+	"Document Number" AS "Document Number",
+	"Position In Document" AS "Position In Document",
+	(
+		day((
+				convert(DATETIME, (
+						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
+						))
+				) - c142)
+		) 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" AS "Betrag",
+	"Konto_mit_Bezeichnung" AS "Konto_mit_Bezeichnung",
+	"Menge_2" AS "Menge_2",
+	"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" AS "Acct Nr",
+	"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 ('FZG-Gesch�ft')
+		ELSE ('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",
+	SUM("Betrag") OVER (PARTITION BY "Vehicle Reference_berechnet") 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",
+	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 ('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 ('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",
+	"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",
+	COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Menge_2",
+	("Menge_1" / COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet")) AS "Menge",
+	"Vehicle Reference_berechnet" AS "Vehicle Reference_berechnet",
+	"var. Kosten" AS "var. Kosten",
+	"Dealer Vehicle Type_Vehicles" AS "Dealer Vehicle Type_Vehicles",
+	"Dealer Vehicle Number_Vehicles" AS "Dealer Vehicle Number_Vehicles"
+FROM (
+	SELECT "Accounting Date",
+		"Document Type",
+		"Document Number",
+		"Position In Document",
+		"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_journal_accountings",
+		"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",
+		"Nom_Account_Is Profit Loss Account",
+		"Rechtseinheit",
+		"Betrieb",
+		"Accounting Date" AS "Bookkeep Date",
+		"Marke",
+		"Text",
+		'1' AS "Mandant",
+		"Betrag",
+		"Konto_mit_Bezeichnung",
+		"Menge_2",
+		"Stellen Cost Center",
+		"KST",
+		"Stellen Sales Channel",
+		"Absatzkanal",
+		"Stellen Cost Unit",
+		"Kostentr�ger_mit_Null",
+		"Kostentr�ger",
+		"GuV_Bilanz",
+		"Susa",
+		"Nominal Account Number" AS "Acct Nr",
+		"Document Type In Journal",
+		"Document Type Description",
+		"FZG_ja_nein",
+		"Invoice Type_Invoices",
+		"Invoice Number_Invoices",
+		"Invoice Date_Invoices",
+		"Is Canceled_Invoices",
+		"Vehicle Number_Invoices",
+		"Invoice_Date_1",
+		"Internal Number_Vehicles",
+		"Vin_Vehicles",
+		"Dealer Vehicle Type",
+		"Fahrzeugtyp",
+		"Fahrzeugart",
+		"Make Number_Vehicles",
+		"Description_Make",
+		"Fabrikat",
+		"Model Code_Models",
+		"Model_Detail",
+		"Model",
+		"Owner Number_Vehicles",
+		"Holder Number_Vehicles",
+		"First Name_Owner_Customer",
+		"Family Name_Owner_Customer",
+		"First Name_Holder_Customer",
+		"Family Name_Holder_Customer",
+		"Kunde",
+		"Sales_Channel_FZG",
+		"Rechtseinheit" AS "Hauptbetrieb",
+		"Betrieb" AS "Standort_alt",
+		"Salesman Number_Vehicles",
+		"Employee Number_Employees_Salesman",
+		"Name_Employees_Salesman",
+		"Verk�ufer",
+		"Standort_FZG_Verkauf",
+		"Standort",
+		"FZG",
+		"Konto_mit_Bezeichnung" AS "Konto",
+		"Body Paint Code",
+		"Body Paint Description",
+		"Body Paint Description" AS "Farbe",
+		"Erl�s FZG",
+		"Nachlass",
+		"VAK FZG",
+		"Erl�s Zulass.",
+		"VAK Zulass.",
+		"Erl�s Gar.",
+		"VAK Gar.",
+		"Erl�s Nachr./Aufb.",
+		"VAK Nachr./Aufb.",
+		"VAK Fertigm.",
+		"Erl�s �berf.",
+		"VAK �berf.",
+		"Erl�s Sonst._8900",
+		"Erl�s Sonst.",
+		"VAK Sonst.",
+		"VAK intern",
+		"Erl�s Prov.",
+		"Boni/VK-Hilfen",
+		"Erl�s ges.",
+		"VAK ges.",
+		1 AS "Menge_1",
+		"Vehicle Reference_berechnet",
+		"var. Kosten",
+		"Dealer Vehicle Type_Vehicles",
+		"Dealer Vehicle Number_Vehicles",
+		c152 AS c142,
+		MAX("Vehicle Number_Invoices") OVER (PARTITION BY "Vehicle Reference_berechnet") AS c143,
+		(
+			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 c144,
+		(
+			CASE 
+				WHEN ((SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet")) > 0)
+					THEN ('FZG-Gesch�ft')
+				ELSE ('Buchungen ohne FZG-Gesch�ft')
+				END
+			) AS c145
+	FROM (
+		SELECT (left(T1."vehicle_reference", 7)) + '_' + (right(T1."vehicle_reference", 17)) AS "Vehicle Reference_berechnet",
+			(((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) AS "Invoice Date_journal_accountings",
+			T2."in_buy_invoice_no_date" AS c152,
+			T8."dealer_vehicle_number" AS "Dealer Vehicle Number_Vehicles",
+			T8."dealer_vehicle_type" AS "Dealer Vehicle Type_Vehicles",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (4300, 4560, 4580, 4530, 4540, 4590, 4591, 4690, 4621, 4622, 4623, 4500, 4501, 4502, 4503, 4504, 5501))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "var. Kosten",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7240))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					)
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7800, 7850))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) AS "VAK ges.",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8900))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					)
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) AS "Erl�s ges.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7800, 7850))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "Boni/VK-Hilfen",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Prov.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK intern",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7240))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) AS "VAK Sonst.",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8900))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) AS "Erl�s Sonst.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8900))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Sonst._8900",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7030))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK �berf.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8030))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s �berf.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7240))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Fertigm.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7220))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Nachr./Aufb.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8220))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Nachr./Aufb.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7210))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Gar.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8210))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Gar.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7200))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Zulass.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8200))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Zulass.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK FZG",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Nachlass",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s FZG",
+			T8."body_paint_description" AS "Body Paint Description",
+			T8."body_paint_code" AS "Body Paint Code",
+			((left((((T1."nominal_account_number"))), 4)) + ' - ' + T4."account_description") AS "Konto_mit_Bezeichnung",
+			(left(((left(T1."vehicle_reference", 7)) + '_' + (right(T1."vehicle_reference", 17))), 7)) + ' / ' + ((left((((T8."internal_number"))), 5))) + ' - ' + T10."description" + ' - ' + (
+				(
+					(left((((T8."holder_number"))), 7)) + ' - ' + (
+						CASE 
+							WHEN (T3."first_name" IS NULL)
+								THEN ('')
+							ELSE (T3."first_name")
+							END
+						) + ' ' + T3."family_name"
+					)
+				) AS "FZG",
+			CASE 
+				WHEN (T2."out_subsidiary" IS NULL)
+					THEN ((('0' + (((T1."skr51_branch"))))))
+				ELSE ('0' + (((T2."out_subsidiary"))))
+				END AS "Standort",
+			T2."out_subsidiary" AS "Standort_FZG_Verkauf",
+			(left((((T8."salesman_number"))), 4)) + ' - ' + T12."name" AS "Verk�ufer",
+			T12."name" AS "Name_Employees_Salesman",
+			T12."employee_number" AS "Employee Number_Employees_Salesman",
+			T8."salesman_number" AS "Salesman Number_Vehicles",
+			('0' + (((T1."skr51_branch")))) AS "Betrieb",
+			T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+			CASE 
+				WHEN (
+						(
+							(
+								CASE 
+									WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+										THEN (1)
+									ELSE (0)
+									END
+								) = 1
+							)
+						AND (T7."is_canceled" <> '1')
+						)
+					THEN (T1."skr51_sales_channel")
+				ELSE NULL
+				END AS "Sales_Channel_FZG",
+			(
+				(left((((T8."holder_number"))), 7)) + ' - ' + (
+					CASE 
+						WHEN (T3."first_name" IS NULL)
+							THEN ('')
+						ELSE (T3."first_name")
+						END
+					) + ' ' + T3."family_name"
+				) AS "Kunde",
+			T3."family_name" AS "Family Name_Holder_Customer",
+			CASE 
+				WHEN (T3."first_name" IS NULL)
+					THEN ('')
+				ELSE (T3."first_name")
+				END AS "First Name_Holder_Customer",
+			T11."family_name" AS "Family Name_Owner_Customer",
+			T11."first_name" AS "First Name_Owner_Customer",
+			T8."holder_number" AS "Holder Number_Vehicles",
+			T8."owner_number" AS "Owner Number_Vehicles",
+			(left((ucase(T10."description")), 3)) AS "Model",
+			T10."description" AS "Model_Detail",
+			T10."model_code" AS "Model Code_Models",
+			CASE 
+				WHEN (((rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description") = '3 - Husqvarna')
+					THEN ('Husqvarna')
+				ELSE (T9."description")
+				END AS "Fabrikat",
+			T9."description" AS "Description_Make",
+			T8."make_number" AS "Make Number_Vehicles",
+			CASE 
+				WHEN (
+						(
+							CASE 
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+									THEN ('NW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+									THEN ('VFW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+									THEN ('GW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+									THEN ('TZ')
+								ELSE NULL
+								END
+							) IN ('VFW', 'TZ', 'NW')
+						)
+					THEN ('Neuwagen')
+				ELSE ('Gebrauchtwagen')
+				END AS "Fahrzeugart",
+			CASE 
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+					THEN ('NW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+					THEN ('VFW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+					THEN ('GW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+					THEN ('TZ')
+				ELSE NULL
+				END AS "Fahrzeugtyp",
+			(left(T1."vehicle_reference", 1)) AS "Dealer Vehicle Type",
+			T8."vin" AS "Vin_Vehicles",
+			T8."internal_number" AS "Internal Number_Vehicles",
+			CASE 
+				WHEN (
+						(
+							CASE 
+								WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+									THEN (1)
+								ELSE (0)
+								END
+							) = 1
+						)
+					THEN (((((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2))))))))
+				ELSE NULL
+				END AS "Invoice_Date_1",
+			T7."vehicle_number" AS "Vehicle Number_Invoices",
+			T7."is_canceled" AS "Is Canceled_Invoices",
+			T7."invoice_date" AS "Invoice Date_Invoices",
+			T7."invoice_number" AS "Invoice Number_Invoices",
+			T7."invoice_type" AS "Invoice Type_Invoices",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+					THEN (1)
+				ELSE (0)
+				END AS "FZG_ja_nein",
+			T6."document_type_description" AS "Document Type Description",
+			T6."document_type_in_journal" AS "Document Type In Journal",
+			T1."nominal_account_number" AS "Nominal Account Number",
+			(left((((T1."nominal_account_number"))), 1)) AS "Susa",
+			CASE 
+				WHEN (T4."is_profit_loss_account" = 'J')
+					THEN ('2')
+				ELSE ('1')
+				END AS "GuV_Bilanz",
+			CASE 
+				WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+					THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name")))
+				ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name"))
+				END AS "Kostentr�ger",
+			(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+			(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+			(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T5."skr51_sales_channel_name" AS "Absatzkanal",
+			(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+			(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T5."skr51_cost_center_name" AS "KST",
+			(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_count" / 100 * - 1)
+				ELSE (T1."posted_count" / 100)
+				END AS "Menge_2",
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_value" / 100 * - 1)
+				ELSE (T1."posted_value" / 100)
+				END AS "Betrag",
+			CASE 
+				WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) <= 360)
+					THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (rtrim((((T1."employee_number"))))))
+				ELSE ('Buchungen �lter 360 Tage')
+				END AS "Text",
+			(rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description" AS "Marke",
+			T1."accounting_date" AS "Accounting Date",
+			T4."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+			CASE 
+				WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+					THEN ('Neuwagen')
+				WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+					THEN ('Gebrauchtwagen')
+				WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+					THEN ('Teile & Zubeh�r')
+				WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+					THEN ('Service')
+				WHEN (T1."skr51_cost_unit" = 0)
+					THEN ('Ohne Kostentr�ger')
+				ELSE NULL
+				END AS "Free Form Document Text",
+			T3."zip_code" AS "Free Form Accounting Text",
+			T1."previously_used_account_no" AS "Previously Used Account No",
+			T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+			T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+			T1."skr51_cost_center" AS "Skr51 Cost Center",
+			T1."skr51_make" AS "Skr51 Make",
+			T1."skr51_branch" AS "Skr51 Branch",
+			T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+			T1."days_for_cash_discount" AS "Days For Cash Discount",
+			T1."vat_key" AS "Vat Key",
+			T1."account_statement_page" AS "Account Statement Page",
+			T1."account_statement_number" AS "Account Statement Number",
+			T1."vat_id_number" AS "Vat Id Number",
+			T1."vehicle_reference" AS "Vehicle Reference",
+			T1."posting_text" AS "Posting Text",
+			T1."term_of_payment" AS "Term Of Payment",
+			T1."cash_discount" AS "Cash Discount",
+			T1."journal_line" AS "Journal Line",
+			T1."journal_page" AS "Journal Page",
+			T1."last_dunning_date" AS "Last Dunning Date",
+			T1."dunning_level" AS "Dunning Level",
+			T1."invoice_number" AS "Invoice Number",
+			T1."employee_number" AS "Employee Number",
+			T1."serial_number_each_month" AS "Serial Number Each Month",
+			T1."account_form_page_line" AS "Account Form Page Line",
+			T1."account_form_page_number" AS "Account Form Page Number",
+			T1."contra_account_text" AS "Contra Account Text",
+			T1."nominal_contra_account" AS "Nominal Contra Account",
+			T1."customer_contra_account" AS "Customer Contra Account",
+			T1."branch_number" AS "Branch Number",
+			T1."posted_count" AS "Posted Count",
+			T1."debit_or_credit" AS "Debit Or Credit",
+			T1."posted_value" AS "Posted Value",
+			T1."document_date" AS "Document Date",
+			T1."clearing_number" AS "Clearing Number",
+			T1."is_balanced" AS "Is Balanced",
+			T1."position_in_document" AS "Position In Document",
+			T1."document_number" AS "Document Number",
+			T1."document_type" AS "Document Type"
+		FROM "nominal_accounts" T4,
+			(
+				(
+					(
+						(
+							(
+								(
+									(
+										(
+											(
+												(
+													"journal_accountings" T1 LEFT JOIN "vehicles" T8 ON (right(T1."vehicle_reference", 17)) = T8."vin"
+													) LEFT JOIN "LOCOSOFT"."dbo"."dealer_vehicles" T2 ON (T8."dealer_vehicle_type" = T2."dealer_vehicle_type")
+													AND (T8."dealer_vehicle_number" = T2."dealer_vehicle_number")
+												) LEFT JOIN "customers_suppliers" T3 ON T3."customer_number" = T8."holder_number"
+											) LEFT JOIN "accounts_characteristics" T5 ON (
+												(
+													(
+														(
+															(T5."skr51_make" = T1."skr51_make")
+															AND (T5."skr51_cost_center" = T1."skr51_cost_center")
+															)
+														AND (T5."skr51_sales_channel" = T1."skr51_sales_channel")
+														)
+													AND (T5."skr51_cost_unit" = T1."skr51_cost_unit")
+													)
+												AND (T5."skr51_branch" = T1."skr51_branch")
+												)
+											AND (T5."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+										) LEFT JOIN "document_types" T6 ON T6."document_type_in_journal" = T1."document_type"
+									) LEFT JOIN "invoices" T7 ON T7."invtype_invnr" = T1."invoice_number"
+								) LEFT JOIN "makes" T9 ON T8."make_number" = T9."make_number"
+							) LEFT JOIN "models" T10 ON (T8."make_number" = T10."make_number")
+							AND (T8."model_code" = T10."model_code")
+						) LEFT JOIN "customers_suppliers" T11 ON T11."customer_number" = T8."owner_number"
+					) LEFT JOIN "employees" T12 ON (T8."salesman_number" <> 0)
+					AND (T8."salesman_number" = T12."salesman_number")
+				)
+		WHERE (
+				(T4."nominal_account_number" = T1."nominal_account_number")
+				AND (T4."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+				)
+			AND (
+				(
+					(T4."is_profit_loss_account" = 'J')
+					AND (T1."vehicle_reference" <> ' ')
+					)
+				AND (T1."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, 5001, 5005, 5006, 5007, 5008, 5003, 5004, 4300, 4560, 4580, 4530, 4540, 4590, 4591, 4690, 4621, 4622, 4623, 4500, 4501, 4502, 4503, 4504, 5501, 7220, 7800, 7850, 7801, 7802, 7803, 7804, 7805, 7806, 7807, 7820, 7825, 7827, 7808, 8030, 7030, 8870, 8871, 8872, 8220, 7220))
+				)
+		) D2
+	) D1
+WHERE (
+		(
+			(
+				(
+					(c143 = "Internal Number_Vehicles")
+					OR ("Internal Number_Vehicles" IS NULL)
+					)
+				OR (c143 IS NULL)
+				)
+			AND (c144 >= convert(DATE, '2017-01-01'))
+			)
+		AND (c145 = 'FZG-Gesch�ft')
+		)
+	-- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc

+ 1321 - 545
System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK_Stk_FIBU.sql

@@ -1,545 +1,1321 @@
-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 ('FZG-Geschäft') ELSE ('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 ('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 ('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",
-	   '0' + (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 "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_journal_accountings",
-	   "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",
-	   "Nom_Account_Is Profit Loss Account",
-	   "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Marke",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag_ori",
-	   "Konto_mit_Bezeichnung",
-	   "Menge",
-	   "Stellen Cost Center",
-	   "KST",
-	   "Stellen Sales Channel",
-	   "Absatzkanal",
-	   "Stellen Cost Unit",
-	   "Kostenträger_mit_Null",
-	   "Kostenträger",
-	   "GuV_Bilanz",
-	   '3' as "Susa",
-	   "Acct Nr_ori",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   "FZG_ja_nein",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Invoice Date_Invoices",
-	   "Is Canceled_Invoices",
-	   "Vehicle Number_Invoices",
-	   "Invoice_Date_1",
-	   "Internal Number_Vehicles",
-	   "Vin_Vehicles",
-	   "Dealer Vehicle Type",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Make Number_Vehicles",
-	   "Description_Make",
-	   "Description_Make" as "Fabrikat",
-	   "Model Code_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Owner Number_Vehicles",
-	   "Holder Number_Vehicles",
-	   "First Name_Owner_Customer",
-	   "Family Name_Owner_Customer",
-	   "First Name_Holder_Customer",
-	   "Family Name_Holder_Customer",
-	   "Kunde",
-	   "Sales_Channel_FZG",
-	   "Rechtseinheit" as "Hauptbetrieb",
-	   "Betrieb" as "Standort_alt",
-	   "Salesman Number_Vehicles",
-	   "Employee Number_Employees_Salesman",
-	   "Name_Employees_Salesman",
-	   "Verkäufer",
-	   "Standort_FZG_Verkauf",
-	   "FZG",
-	   "Konto_mit_Bezeichnung" as "Konto",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Body Paint Description" as "Farbe",
-	   "Erlös FZG",
-	   "Nachlass",
-	   "VAK FZG",
-	   "Erlös Zulass.",
-	   "VAK Zulass.",
-	   "Erlös Gar.",
-	   "VAK Gar.",
-	   "Erlös Nachr./Aufb.",
-	   "VAK Nachr./Aufb.",
-	   "VAK Fertigm.",
-	   "Erlös Überf.",
-	   "VAK Überf.",
-	   "Erlös Sonst._8900",
-	   "Erlös Sonst.",
-	   "VAK Sonst.",
-	   "VAK intern",
-	   "Erlös Prov.",
-	   "Boni/VK-Hilfen",
-	   "Erlös ges.",
-	   "VAK ges.",
-	   1 as "Menge_1",
-	   "Vehicle Reference_berechnet",
-	   "Acct Nr",
-	   "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles",
-	   MAX("Vehicle Number_Invoices") OVER (partition by "Vehicle Reference_berechnet") as c142,
-	   (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 c143,
-	   (CASE WHEN ((SUM("FZG_ja_nein") OVER (partition by "Vehicle Reference_berechnet")) > 0) THEN ('FZG-Geschäft') ELSE ('Buchungen ohne FZG-Geschäft') END) as c144
-from 
-(select (left(T1."vehicle_reference",8)) + '_' + (right(T1."vehicle_reference",17)) as "Vehicle Reference_berechnet",
-	   (((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) as "Invoice Date_journal_accountings",
-	   T6."dealer_vehicle_number" as "Dealer Vehicle Number_Vehicles",
-	   T6."dealer_vehicle_type" as "Dealer Vehicle Type_Vehicles",
-	   (ltrim(((left((((T1."nominal_account_number"))),4)) + '_STK'))) as "Acct Nr",
-	   (CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK ges.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös ges.",
-	   CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "Boni/VK-Hilfen",
-	   CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Prov.",
-	   CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK intern",
-	   (CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK Sonst.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös Sonst.",
-	   CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Sonst._8900",
-	   CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Fertigm.",
-	   CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK FZG",
-	   CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Nachlass",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös FZG",
-	   T6."body_paint_description" as "Body Paint Description",
-	   T6."body_paint_code" as "Body Paint Code",
-	   ((left((((T1."nominal_account_number"))),4)) + ' - ' + T2."account_description") as "Konto_mit_Bezeichnung",
-	   (right(T6."vin",5)) + ' / ' + ((left((((T6."internal_number"))),5))) + ' - ' + T8."description" + ' - ' + (CASE WHEN (T10."first_name" <> ' ') THEN ((left((((T6."holder_number"))),6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((left((((T6."holder_number"))),6)) + ' - ' + T10."family_name") END) as "FZG",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_branch") ELSE null END as "Standort_FZG_Verkauf",
-	   (left((((T6."salesman_number"))),3)) + ' - ' + T11."name" as "Verkäufer",
-	   T11."name" as "Name_Employees_Salesman",
-	   T11."employee_number" as "Employee Number_Employees_Salesman",
-	   T6."salesman_number" as "Salesman Number_Vehicles",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_sales_channel") ELSE null END as "Sales_Channel_FZG",
-	   CASE WHEN (T10."first_name" <> ' ') THEN ((left((((T6."holder_number"))),6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((left((((T6."holder_number"))),6)) + ' - ' + T10."family_name") END as "Kunde",
-	   T10."family_name" as "Family Name_Holder_Customer",
-	   T10."first_name" as "First Name_Holder_Customer",
-	   T9."family_name" as "Family Name_Owner_Customer",
-	   T9."first_name" as "First Name_Owner_Customer",
-	   T6."holder_number" as "Holder Number_Vehicles",
-	   T6."owner_number" as "Owner Number_Vehicles",
-	   (left((ucase(T8."description")),3)) as "Model",
-	   T8."description" as "Model_Detail",
-	   T8."model_code" as "Model Code_Models",
-	   T7."description" as "Description_Make",
-	   T6."make_number" as "Make Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (left(T1."vehicle_reference",1)) as "Dealer Vehicle Type",
-	   T6."vin" as "Vin_Vehicles",
-	   T6."internal_number" as "Internal Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) THEN (((((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))))) ELSE null END as "Invoice_Date_1",
-	   T5."vehicle_number" as "Vehicle Number_Invoices",
-	   T5."is_canceled" as "Is Canceled_Invoices",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_type" as "Invoice Type_Invoices",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END as "FZG_ja_nein",
-	   T4."document_type_description" as "Document Type Description",
-	   T4."document_type_in_journal" as "Document Type In Journal",
-	   (left((((T1."nominal_account_number"))),4)) + '_STK' as "Acct Nr_ori",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag_ori",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) <= 360) THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (((T1."employee_number")))) ELSE ('Buchungen älter 360 Tage') END as "Text",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   T1."accounting_date" as "Accounting Date",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."customer_number" as "Customer Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."document_number" as "Document Number",
-	   T1."document_type" as "Document Type"
-from "nominal_accounts" T2,
-	((((((((("journal_accountings" T1 left outer join "accounts_characteristics" T3 on (((((T3."skr51_make" = T1."skr51_make") and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit")) and (T3."skr51_branch" = T1."skr51_branch")) and (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")) left outer join "document_types" T4 on T4."document_type_in_journal" = T1."document_type") left outer join "invoices" T5 on T5."invtype_invnr" = T1."invoice_number") left outer join "vehicles" T6 on (right(T1."vehicle_reference",17)) = T6."vin") left outer join "makes" T7 on T6."make_number" = T7."make_number") left outer join "models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code")) left outer join "customers_suppliers" T9 on T9."customer_number" = T6."owner_number") left outer join "customers_suppliers" T10 on T10."customer_number" = T6."holder_number") left outer join "employees" T11 on (T6."salesman_number" <> 0) and (T6."salesman_number" = T11."salesman_number"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((((T2."is_profit_loss_account" = 'J') and (T1."vehicle_reference" <> ' ')) and (T1."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 (T1."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 = 'FZG-Geschäft')) and (c143 = "Invoice_Date_1"))
-) D4
--- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
+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 ('FZG-Gesch�ft')
+			ELSE ('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 ('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 ('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",
+		'0' + (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 "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_journal_accountings",
+			"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",
+			"Nom_Account_Is Profit Loss Account",
+			"Rechtseinheit",
+			"Betrieb",
+			"Accounting Date" AS "Bookkeep Date",
+			"Marke",
+			"Text",
+			'1' AS "Mandant",
+			"Betrag_ori",
+			"Konto_mit_Bezeichnung",
+			"Menge",
+			"Stellen Cost Center",
+			"KST",
+			"Stellen Sales Channel",
+			"Absatzkanal",
+			"Stellen Cost Unit",
+			"Kostentr�ger_mit_Null",
+			"Kostentr�ger",
+			"GuV_Bilanz",
+			'3' AS "Susa",
+			"Acct Nr_ori",
+			"Document Type In Journal",
+			"Document Type Description",
+			"FZG_ja_nein",
+			"Invoice Type_Invoices",
+			"Invoice Number_Invoices",
+			"Invoice Date_Invoices",
+			"Is Canceled_Invoices",
+			"Vehicle Number_Invoices",
+			"Invoice_Date_1",
+			"Internal Number_Vehicles",
+			"Vin_Vehicles",
+			"Dealer Vehicle Type",
+			"Fahrzeugtyp",
+			"Fahrzeugart",
+			"Make Number_Vehicles",
+			"Description_Make",
+			"Description_Make" AS "Fabrikat",
+			"Model Code_Models",
+			"Model_Detail",
+			"Model",
+			"Owner Number_Vehicles",
+			"Holder Number_Vehicles",
+			"First Name_Owner_Customer",
+			"Family Name_Owner_Customer",
+			"First Name_Holder_Customer",
+			"Family Name_Holder_Customer",
+			"Kunde",
+			"Sales_Channel_FZG",
+			"Rechtseinheit" AS "Hauptbetrieb",
+			"Betrieb" AS "Standort_alt",
+			"Salesman Number_Vehicles",
+			"Employee Number_Employees_Salesman",
+			"Name_Employees_Salesman",
+			"Verk�ufer",
+			"Standort_FZG_Verkauf",
+			"FZG",
+			"Konto_mit_Bezeichnung" AS "Konto",
+			"Body Paint Code",
+			"Body Paint Description",
+			"Body Paint Description" AS "Farbe",
+			"Erl�s FZG",
+			"Nachlass",
+			"VAK FZG",
+			"Erl�s Zulass.",
+			"VAK Zulass.",
+			"Erl�s Gar.",
+			"VAK Gar.",
+			"Erl�s Nachr./Aufb.",
+			"VAK Nachr./Aufb.",
+			"VAK Fertigm.",
+			"Erl�s �berf.",
+			"VAK �berf.",
+			"Erl�s Sonst._8900",
+			"Erl�s Sonst.",
+			"VAK Sonst.",
+			"VAK intern",
+			"Erl�s Prov.",
+			"Boni/VK-Hilfen",
+			"Erl�s ges.",
+			"VAK ges.",
+			1 AS "Menge_1",
+			"Vehicle Reference_berechnet",
+			"Acct Nr",
+			"Dealer Vehicle Type_Vehicles",
+			"Dealer Vehicle Number_Vehicles",
+			MAX("Vehicle Number_Invoices") OVER (PARTITION BY "Vehicle Reference_berechnet") AS c142,
+			(
+				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 c143,
+			(
+				CASE 
+					WHEN ((SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet")) > 0)
+						THEN ('FZG-Gesch�ft')
+					ELSE ('Buchungen ohne FZG-Gesch�ft')
+					END
+				) AS c144
+		FROM (
+			SELECT (left(T1."vehicle_reference", 8)) + '_' + (right(T1."vehicle_reference", 17)) AS "Vehicle Reference_berechnet",
+				(((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) AS "Invoice Date_journal_accountings",
+				T6."dealer_vehicle_number" AS "Dealer Vehicle Number_Vehicles",
+				T6."dealer_vehicle_type" AS "Dealer Vehicle Type_Vehicles",
+				(ltrim(((left((((T1."nominal_account_number"))), 4)) + '_STK'))) AS "Acct Nr",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7240))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7800, 7850))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS "VAK ges.",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8900))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS "Erl�s ges.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7800, 7850))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "Boni/VK-Hilfen",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Prov.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK intern",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7240))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS "VAK Sonst.",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8900))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS "Erl�s Sonst.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8900))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Sonst._8900",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK �berf.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s �berf.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7240))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Fertigm.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Nachr./Aufb.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Nachr./Aufb.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Gar.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Gar.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Zulass.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Zulass.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK FZG",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Nachlass",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s FZG",
+				T6."body_paint_description" AS "Body Paint Description",
+				T6."body_paint_code" AS "Body Paint Code",
+				((left((((T1."nominal_account_number"))), 4)) + ' - ' + T2."account_description") AS "Konto_mit_Bezeichnung",
+				(right(T6."vin", 5)) + ' / ' + ((left((((T6."internal_number"))), 5))) + ' - ' + T8."description" + ' - ' + (
+					CASE 
+						WHEN (T10."first_name" <> ' ')
+							THEN ((left((((T6."holder_number"))), 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+						ELSE ((left((((T6."holder_number"))), 6)) + ' - ' + T10."family_name")
+						END
+					) AS "FZG",
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_branch")
+					ELSE NULL
+					END AS "Standort_FZG_Verkauf",
+				(left((((T6."salesman_number"))), 3)) + ' - ' + T11."name" AS "Verk�ufer",
+				T11."name" AS "Name_Employees_Salesman",
+				T11."employee_number" AS "Employee Number_Employees_Salesman",
+				T6."salesman_number" AS "Salesman Number_Vehicles",
+				('0' + (((T1."skr51_branch")))) AS "Betrieb",
+				T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_sales_channel")
+					ELSE NULL
+					END AS "Sales_Channel_FZG",
+				CASE 
+					WHEN (T10."first_name" <> ' ')
+						THEN ((left((((T6."holder_number"))), 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+					ELSE ((left((((T6."holder_number"))), 6)) + ' - ' + T10."family_name")
+					END AS "Kunde",
+				T10."family_name" AS "Family Name_Holder_Customer",
+				T10."first_name" AS "First Name_Holder_Customer",
+				T9."family_name" AS "Family Name_Owner_Customer",
+				T9."first_name" AS "First Name_Owner_Customer",
+				T6."holder_number" AS "Holder Number_Vehicles",
+				T6."owner_number" AS "Owner Number_Vehicles",
+				(left((ucase(T8."description")), 3)) AS "Model",
+				T8."description" AS "Model_Detail",
+				T8."model_code" AS "Model Code_Models",
+				T7."description" AS "Description_Make",
+				T6."make_number" AS "Make Number_Vehicles",
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+										THEN ('NW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+										THEN ('VFW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+										THEN ('GW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+										THEN ('TZ')
+									ELSE NULL
+									END
+								) IN ('VFW', 'TZ', 'NW')
+							)
+						THEN ('Neuwagen')
+					ELSE ('Gebrauchtwagen')
+					END AS "Fahrzeugart",
+				CASE 
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+						THEN ('NW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+						THEN ('VFW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+						THEN ('GW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+						THEN ('TZ')
+					ELSE NULL
+					END AS "Fahrzeugtyp",
+				(left(T1."vehicle_reference", 1)) AS "Dealer Vehicle Type",
+				T6."vin" AS "Vin_Vehicles",
+				T6."internal_number" AS "Internal Number_Vehicles",
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+										THEN (1)
+									ELSE (0)
+									END
+								) = 1
+							)
+						THEN (((((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2))))))))
+					ELSE NULL
+					END AS "Invoice_Date_1",
+				T5."vehicle_number" AS "Vehicle Number_Invoices",
+				T5."is_canceled" AS "Is Canceled_Invoices",
+				T5."invoice_date" AS "Invoice Date_Invoices",
+				T5."invoice_number" AS "Invoice Number_Invoices",
+				T5."invoice_type" AS "Invoice Type_Invoices",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+						THEN (1)
+					ELSE (0)
+					END AS "FZG_ja_nein",
+				T4."document_type_description" AS "Document Type Description",
+				T4."document_type_in_journal" AS "Document Type In Journal",
+				(left((((T1."nominal_account_number"))), 4)) + '_STK' AS "Acct Nr_ori",
+				CASE 
+					WHEN (T2."is_profit_loss_account" = 'J')
+						THEN ('2')
+					ELSE ('1')
+					END AS "GuV_Bilanz",
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+					ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+					END AS "Kostentr�ger",
+				(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+				(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+				(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+				(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+				(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+				(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_count" / 100 * - 1)
+					ELSE (T1."posted_count" / 100)
+					END AS "Menge",
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_value" / 100 * - 1)
+					ELSE (T1."posted_value" / 100)
+					END AS "Betrag_ori",
+				CASE 
+					WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) <= 360)
+						THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (((T1."employee_number"))))
+					ELSE ('Buchungen �lter 360 Tage')
+					END AS "Text",
+				(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+				T1."accounting_date" AS "Accounting Date",
+				T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN ('Neuwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+						THEN ('Gebrauchtwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+						THEN ('Teile & Zubeh�r')
+					WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+						THEN ('Service')
+					WHEN (T1."skr51_cost_unit" = 0)
+						THEN ('Ohne Kostentr�ger')
+					ELSE NULL
+					END AS "Free Form Document Text",
+				T1."free_form_accounting_text" AS "Free Form Accounting Text",
+				T1."previously_used_account_no" AS "Previously Used Account No",
+				T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+				T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+				T1."skr51_cost_center" AS "Skr51 Cost Center",
+				T1."skr51_make" AS "Skr51 Make",
+				T1."skr51_branch" AS "Skr51 Branch",
+				T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+				T1."days_for_cash_discount" AS "Days For Cash Discount",
+				T1."vat_key" AS "Vat Key",
+				T1."account_statement_page" AS "Account Statement Page",
+				T1."account_statement_number" AS "Account Statement Number",
+				T1."vat_id_number" AS "Vat Id Number",
+				T1."vehicle_reference" AS "Vehicle Reference",
+				T1."posting_text" AS "Posting Text",
+				T1."term_of_payment" AS "Term Of Payment",
+				T1."cash_discount" AS "Cash Discount",
+				T1."journal_line" AS "Journal Line",
+				T1."journal_page" AS "Journal Page",
+				T1."last_dunning_date" AS "Last Dunning Date",
+				T1."dunning_level" AS "Dunning Level",
+				T1."invoice_number" AS "Invoice Number",
+				T1."employee_number" AS "Employee Number",
+				T1."serial_number_each_month" AS "Serial Number Each Month",
+				T1."account_form_page_line" AS "Account Form Page Line",
+				T1."account_form_page_number" AS "Account Form Page Number",
+				T1."contra_account_text" AS "Contra Account Text",
+				T1."nominal_contra_account" AS "Nominal Contra Account",
+				T1."customer_contra_account" AS "Customer Contra Account",
+				T1."branch_number" AS "Branch Number",
+				T1."posted_count" AS "Posted Count",
+				T1."debit_or_credit" AS "Debit Or Credit",
+				T1."posted_value" AS "Posted Value",
+				T1."document_date" AS "Document Date",
+				T1."clearing_number" AS "Clearing Number",
+				T1."is_balanced" AS "Is Balanced",
+				T1."nominal_account_number" AS "Nominal Account Number",
+				T1."customer_number" AS "Customer Number",
+				T1."position_in_document" AS "Position In Document",
+				T1."document_number" AS "Document Number",
+				T1."document_type" AS "Document Type"
+			FROM "nominal_accounts" T2,
+				(
+					(
+						(
+							(
+								(
+									(
+										(
+											(
+												(
+													"journal_accountings" T1 LEFT JOIN "accounts_characteristics" T3 ON (
+															(
+																(
+																	(
+																		(T3."skr51_make" = T1."skr51_make")
+																		AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+																		)
+																	AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+																	)
+																AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+																)
+															AND (T3."skr51_branch" = T1."skr51_branch")
+															)
+														AND (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+													) LEFT JOIN "document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
+												) LEFT JOIN "invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
+											) LEFT JOIN "vehicles" T6 ON (right(T1."vehicle_reference", 17)) = T6."vin"
+										) LEFT JOIN "makes" T7 ON T6."make_number" = T7."make_number"
+									) LEFT JOIN "models" T8 ON (T6."make_number" = T8."make_number")
+									AND (T6."model_code" = T8."model_code")
+								) LEFT JOIN "customers_suppliers" T9 ON T9."customer_number" = T6."owner_number"
+							) LEFT JOIN "customers_suppliers" T10 ON T10."customer_number" = T6."holder_number"
+						) LEFT JOIN "employees" T11 ON (T6."salesman_number" <> 0)
+						AND (T6."salesman_number" = T11."salesman_number")
+					)
+			WHERE (
+					(T2."nominal_account_number" = T1."nominal_account_number")
+					AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+					)
+				AND (
+					(
+						(
+							(T2."is_profit_loss_account" = 'J')
+							AND (T1."vehicle_reference" <> ' ')
+							)
+						AND (T1."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 (T1."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 = 'FZG-Gesch�ft')
+				)
+			AND (c143 = "Invoice_Date_1")
+			)
+	) D4
+	-- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc

+ 1321 - 545
System/LOCOSOFT/IQD/NW/LOC_Belege_NW_GW_VK_Stk_FIBU_Vorlage.sql

@@ -1,545 +1,1321 @@
-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 ('FZG-Geschäft') ELSE ('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 ('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 ('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",
-	   '0' + (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 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 ('FZG-Geschäft') ELSE ('Buchungen ohne FZG-Geschäft') END) as c144
-from 
-(select (substring(T1."vehicle_reference", 1, 8)) + '_' + (right(T1."vehicle_reference",17)) as c149,
-	   (convert(datetime, (((substring(T1."invoice_date", 1, 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) as c150,
-	   T6."dealer_vehicle_number" as c151,
-	   T6."dealer_vehicle_type" as c152,
-	   (reverse(rtrim(reverse(((substring((convert(varchar(50), T1."nominal_account_number")), 1, 4)) + '_STK'))))) as c153,
-	   (CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as c154,
-	   (CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as c155,
-	   CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c156,
-	   CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c157,
-	   CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c158,
-	   (CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as c159,
-	   (CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as c160,
-	   CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c161,
-	   CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c162,
-	   CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c163,
-	   CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c164,
-	   CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c165,
-	   CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c166,
-	   CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c167,
-	   CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c168,
-	   CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c169,
-	   CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c170,
-	   CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as c171,
-	   CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c172,
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as c173,
-	   T6."body_paint_description" as c174,
-	   T6."body_paint_code" as c175,
-	   ((substring((convert(varchar(50), T1."nominal_account_number")), 1, 4)) + ' - ' + T2."account_description") as c176,
-	   (right(T6."vin",5)) + ' / ' + ((substring((convert(varchar(50), T6."internal_number")), 1, 5))) + ' - ' + T8."description" + ' - ' + (CASE WHEN (T10."first_name" <> ' ') THEN ((substring((convert(varchar(50), T6."holder_number")), 1, 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((substring((convert(varchar(50), T6."holder_number")), 1, 6)) + ' - ' + T10."family_name") END) as c177,
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_branch") ELSE null END as c178,
-	   (substring((convert(varchar(50), T6."salesman_number")), 1, 3)) + ' - ' + T11."name" as c179,
-	   T11."name" as c180,
-	   T11."employee_number" as c181,
-	   T6."salesman_number" as c182,
-	   ('0' + (convert(varchar(50), T1."skr51_branch"))) as c183,
-	   T1."subsidiary_to_company_ref" as c184,
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_sales_channel") ELSE null END as c185,
-	   CASE WHEN (T10."first_name" <> ' ') THEN ((substring((convert(varchar(50), T6."holder_number")), 1, 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((substring((convert(varchar(50), T6."holder_number")), 1, 6)) + ' - ' + T10."family_name") END as c186,
-	   T10."family_name" as c187,
-	   T10."first_name" as c188,
-	   T9."family_name" as c189,
-	   T9."first_name" as c190,
-	   T6."holder_number" as c191,
-	   T6."owner_number" as c192,
-	   (substring((upper(T8."description")), 1, 3)) as c193,
-	   T8."description" as c194,
-	   T8."model_code" as c195,
-	   T7."description" as c196,
-	   T6."make_number" as c197,
-	   CASE WHEN ((CASE WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('N')) THEN ('NW') WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('V')) THEN ('VFW') WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('G','D')) THEN ('GW') WHEN (((substring(T1."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(T1."vehicle_reference", 1, 1))) IN ('N')) THEN ('NW') WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('V')) THEN ('VFW') WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('G','D')) THEN ('GW') WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('T')) THEN ('TZ') ELSE null END as c199,
-	   (substring(T1."vehicle_reference", 1, 1)) as c200,
-	   T6."vin" as c201,
-	   T6."internal_number" as c202,
-	   CASE WHEN ((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) THEN (((convert(datetime, (((substring(T1."invoice_date", 1, 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))))) ELSE null END as c203,
-	   T5."vehicle_number" as c204,
-	   T5."is_canceled" as c205,
-	   T5."invoice_date" as c206,
-	   T5."invoice_number" as c207,
-	   T5."invoice_type" as c208,
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END as c209,
-	   T4."document_type_description" as c210,
-	   T4."document_type_in_journal" as c211,
-	   (substring((convert(varchar(50), T1."nominal_account_number")), 1, 4)) + '_STK' as c212,
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as c213,
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((convert(varchar(50), T1."skr51_make")))) + ' - ' + (rtrim((convert(varchar(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((convert(varchar(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name")) END as c214,
-	   (rtrim((convert(varchar(50), T1."skr51_make")))) + ' - ' + (rtrim((convert(varchar(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name") as c215,
-	   (len((convert(varchar(50), T1."skr51_cost_unit")))) as c216,
-	   (rtrim((convert(varchar(50), T1."skr51_sales_channel")))) + ' - ' + T3."skr51_sales_channel_name" as c217,
-	   (len((convert(varchar(50), T1."skr51_sales_channel")))) as c218,
-	   (rtrim((convert(varchar(50), T1."skr51_cost_center")))) + ' - ' + T3."skr51_cost_center_name" as c219,
-	   (len((convert(varchar(50), T1."skr51_cost_center")))) as c220,
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as c221,
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as c222,
-	   CASE WHEN ((day((getdate()) - (convert(datetime, T1."accounting_date")))) <= 360) THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (convert(varchar(50), T1."employee_number"))) ELSE ('Buchungen älter 360 Tage') END as c223,
-	   (rtrim((convert(varchar(50), T1."skr51_make")))) + ' - ' + T3."skr51_make_description" as c224,
-	   T1."accounting_date" as c225,
-	   T2."is_profit_loss_account" as c226,
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as c227,
-	   T1."free_form_accounting_text" as c228,
-	   T1."previously_used_account_no" as c229,
-	   T1."skr51_cost_unit" as c230,
-	   T1."skr51_sales_channel" as c231,
-	   T1."skr51_cost_center" as c232,
-	   T1."skr51_make" as c233,
-	   T1."skr51_branch" as c234,
-	   T1."day_of_actual_accounting" as c235,
-	   T1."days_for_cash_discount" as c236,
-	   T1."vat_key" as c237,
-	   T1."account_statement_page" as c238,
-	   T1."account_statement_number" as c239,
-	   T1."vat_id_number" as c240,
-	   T1."vehicle_reference" as c241,
-	   T1."posting_text" as c242,
-	   T1."term_of_payment" as c243,
-	   T1."cash_discount" as c244,
-	   T1."journal_line" as c245,
-	   T1."journal_page" as c246,
-	   T1."last_dunning_date" as c247,
-	   T1."dunning_level" as c248,
-	   T1."invoice_number" as c249,
-	   T1."employee_number" as c250,
-	   T1."serial_number_each_month" as c251,
-	   T1."account_form_page_line" as c252,
-	   T1."account_form_page_number" as c253,
-	   T1."contra_account_text" as c254,
-	   T1."nominal_contra_account" as c255,
-	   T1."customer_contra_account" as c256,
-	   T1."branch_number" as c257,
-	   T1."posted_count" as c258,
-	   T1."debit_or_credit" as c259,
-	   T1."posted_value" as c260,
-	   T1."document_date" as c261,
-	   T1."clearing_number" as c262,
-	   T1."is_balanced" as c263,
-	   T1."nominal_account_number" as c264,
-	   T1."customer_number" as c265,
-	   T1."position_in_document" as c266,
-	   T1."document_number" as c267,
-	   T1."document_type" as c268
-from "nominal_accounts" T2,
-	((((((((("journal_accountings" T1 left outer join "accounts_characteristics" T3 on (((((T3."skr51_make" = T1."skr51_make") and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit")) and (T3."skr51_branch" = T1."skr51_branch")) and (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")) left outer join "document_types" T4 on T4."document_type_in_journal" = T1."document_type") left outer join "invoices" T5 on T5."invtype_invnr" = T1."invoice_number") left outer join "vehicles" T6 on (right(T1."vehicle_reference",17)) = T6."vin") left outer join "makes" T7 on T6."make_number" = T7."make_number") left outer join "models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code")) left outer join "customers_suppliers" T9 on T9."customer_number" = T6."owner_number") left outer join "customers_suppliers" T10 on T10."customer_number" = T6."holder_number") left outer join "employees" T11 on (T6."salesman_number" <> 0) and (T6."salesman_number" = T11."salesman_number"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((((T2."is_profit_loss_account" = 'J') and (T1."vehicle_reference" <> ' ')) and (T1."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 (T1."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 = 'FZG-Geschäft')) and (c143 = "Invoice_Date_1"))
-) D4
--- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
+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 ('FZG-Geschäft')
+			ELSE ('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 ('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 ('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",
+		'0' + (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 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 ('FZG-Geschäft')
+					ELSE ('Buchungen ohne FZG-Geschäft')
+					END
+				) AS c144
+		FROM (
+			SELECT (substring(T1."vehicle_reference", 1, 8)) + '_' + (right(T1."vehicle_reference", 17)) AS c149,
+				(convert(DATETIME, (((substring(T1."invoice_date", 1, 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) AS c150,
+				T6."dealer_vehicle_number" AS c151,
+				T6."dealer_vehicle_type" AS c152,
+				(reverse(rtrim(reverse(((substring((convert(VARCHAR(50), T1."nominal_account_number")), 1, 4)) + '_STK'))))) AS c153,
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7240))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7800, 7850))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS c154,
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8900))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS c155,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7800, 7850))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c156,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c157,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c158,
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7240))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS c159,
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8900))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS c160,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8900))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c161,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c162,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c163,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7240))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c164,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c165,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c166,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c167,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c168,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c169,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c170,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS c171,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c172,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS c173,
+				T6."body_paint_description" AS c174,
+				T6."body_paint_code" AS c175,
+				((substring((convert(VARCHAR(50), T1."nominal_account_number")), 1, 4)) + ' - ' + T2."account_description") AS c176,
+				(right(T6."vin", 5)) + ' / ' + ((substring((convert(VARCHAR(50), T6."internal_number")), 1, 5))) + ' - ' + T8."description" + ' - ' + (
+					CASE 
+						WHEN (T10."first_name" <> ' ')
+							THEN ((substring((convert(VARCHAR(50), T6."holder_number")), 1, 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+						ELSE ((substring((convert(VARCHAR(50), T6."holder_number")), 1, 6)) + ' - ' + T10."family_name")
+						END
+					) AS c177,
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_branch")
+					ELSE NULL
+					END AS c178,
+				(substring((convert(VARCHAR(50), T6."salesman_number")), 1, 3)) + ' - ' + T11."name" AS c179,
+				T11."name" AS c180,
+				T11."employee_number" AS c181,
+				T6."salesman_number" AS c182,
+				('0' + (convert(VARCHAR(50), T1."skr51_branch"))) AS c183,
+				T1."subsidiary_to_company_ref" AS c184,
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_sales_channel")
+					ELSE NULL
+					END AS c185,
+				CASE 
+					WHEN (T10."first_name" <> ' ')
+						THEN ((substring((convert(VARCHAR(50), T6."holder_number")), 1, 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+					ELSE ((substring((convert(VARCHAR(50), T6."holder_number")), 1, 6)) + ' - ' + T10."family_name")
+					END AS c186,
+				T10."family_name" AS c187,
+				T10."first_name" AS c188,
+				T9."family_name" AS c189,
+				T9."first_name" AS c190,
+				T6."holder_number" AS c191,
+				T6."owner_number" AS c192,
+				(substring((upper(T8."description")), 1, 3)) AS c193,
+				T8."description" AS c194,
+				T8."model_code" AS c195,
+				T7."description" AS c196,
+				T6."make_number" AS c197,
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('N'))
+										THEN ('NW')
+									WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('V'))
+										THEN ('VFW')
+									WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('G', 'D'))
+										THEN ('GW')
+									WHEN (((substring(T1."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(T1."vehicle_reference", 1, 1))) IN ('N'))
+						THEN ('NW')
+					WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('V'))
+						THEN ('VFW')
+					WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('G', 'D'))
+						THEN ('GW')
+					WHEN (((substring(T1."vehicle_reference", 1, 1))) IN ('T'))
+						THEN ('TZ')
+					ELSE NULL
+					END AS c199,
+				(substring(T1."vehicle_reference", 1, 1)) AS c200,
+				T6."vin" AS c201,
+				T6."internal_number" AS c202,
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+										THEN (1)
+									ELSE (0)
+									END
+								) = 1
+							)
+						THEN (((convert(DATETIME, (((substring(T1."invoice_date", 1, 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2))))))))
+					ELSE NULL
+					END AS c203,
+				T5."vehicle_number" AS c204,
+				T5."is_canceled" AS c205,
+				T5."invoice_date" AS c206,
+				T5."invoice_number" AS c207,
+				T5."invoice_type" AS c208,
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+						THEN (1)
+					ELSE (0)
+					END AS c209,
+				T4."document_type_description" AS c210,
+				T4."document_type_in_journal" AS c211,
+				(substring((convert(VARCHAR(50), T1."nominal_account_number")), 1, 4)) + '_STK' AS c212,
+				CASE 
+					WHEN (T2."is_profit_loss_account" = 'J')
+						THEN ('2')
+					ELSE ('1')
+					END AS c213,
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN (((rtrim((convert(VARCHAR(50), T1."skr51_make")))) + ' - ' + (rtrim((convert(VARCHAR(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name")))
+					ELSE ((rtrim((convert(VARCHAR(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name"))
+					END AS c214,
+				(rtrim((convert(VARCHAR(50), T1."skr51_make")))) + ' - ' + (rtrim((convert(VARCHAR(50), T1."skr51_cost_unit")))) + ' - ' + (T3."skr51_cost_unit_name") AS c215,
+				(len((convert(VARCHAR(50), T1."skr51_cost_unit")))) AS c216,
+				(rtrim((convert(VARCHAR(50), T1."skr51_sales_channel")))) + ' - ' + T3."skr51_sales_channel_name" AS c217,
+				(len((convert(VARCHAR(50), T1."skr51_sales_channel")))) AS c218,
+				(rtrim((convert(VARCHAR(50), T1."skr51_cost_center")))) + ' - ' + T3."skr51_cost_center_name" AS c219,
+				(len((convert(VARCHAR(50), T1."skr51_cost_center")))) AS c220,
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_count" / 100 * - 1)
+					ELSE (T1."posted_count" / 100)
+					END AS c221,
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_value" / 100 * - 1)
+					ELSE (T1."posted_value" / 100)
+					END AS c222,
+				CASE 
+					WHEN ((day((getdate()) - (convert(DATETIME, T1."accounting_date")))) <= 360)
+						THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (convert(VARCHAR(50), T1."employee_number")))
+					ELSE ('Buchungen älter 360 Tage')
+					END AS c223,
+				(rtrim((convert(VARCHAR(50), T1."skr51_make")))) + ' - ' + T3."skr51_make_description" AS c224,
+				T1."accounting_date" AS c225,
+				T2."is_profit_loss_account" AS c226,
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN ('Neuwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+						THEN ('Gebrauchtwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+						THEN ('Teile & Zubehör')
+					WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+						THEN ('Service')
+					WHEN (T1."skr51_cost_unit" = 0)
+						THEN ('Ohne Kostenträger')
+					ELSE NULL
+					END AS c227,
+				T1."free_form_accounting_text" AS c228,
+				T1."previously_used_account_no" AS c229,
+				T1."skr51_cost_unit" AS c230,
+				T1."skr51_sales_channel" AS c231,
+				T1."skr51_cost_center" AS c232,
+				T1."skr51_make" AS c233,
+				T1."skr51_branch" AS c234,
+				T1."day_of_actual_accounting" AS c235,
+				T1."days_for_cash_discount" AS c236,
+				T1."vat_key" AS c237,
+				T1."account_statement_page" AS c238,
+				T1."account_statement_number" AS c239,
+				T1."vat_id_number" AS c240,
+				T1."vehicle_reference" AS c241,
+				T1."posting_text" AS c242,
+				T1."term_of_payment" AS c243,
+				T1."cash_discount" AS c244,
+				T1."journal_line" AS c245,
+				T1."journal_page" AS c246,
+				T1."last_dunning_date" AS c247,
+				T1."dunning_level" AS c248,
+				T1."invoice_number" AS c249,
+				T1."employee_number" AS c250,
+				T1."serial_number_each_month" AS c251,
+				T1."account_form_page_line" AS c252,
+				T1."account_form_page_number" AS c253,
+				T1."contra_account_text" AS c254,
+				T1."nominal_contra_account" AS c255,
+				T1."customer_contra_account" AS c256,
+				T1."branch_number" AS c257,
+				T1."posted_count" AS c258,
+				T1."debit_or_credit" AS c259,
+				T1."posted_value" AS c260,
+				T1."document_date" AS c261,
+				T1."clearing_number" AS c262,
+				T1."is_balanced" AS c263,
+				T1."nominal_account_number" AS c264,
+				T1."customer_number" AS c265,
+				T1."position_in_document" AS c266,
+				T1."document_number" AS c267,
+				T1."document_type" AS c268
+			FROM "nominal_accounts" T2,
+				(
+					(
+						(
+							(
+								(
+									(
+										(
+											(
+												(
+													"journal_accountings" T1 LEFT JOIN "accounts_characteristics" T3 ON (
+															(
+																(
+																	(
+																		(T3."skr51_make" = T1."skr51_make")
+																		AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+																		)
+																	AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+																	)
+																AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+																)
+															AND (T3."skr51_branch" = T1."skr51_branch")
+															)
+														AND (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+													) LEFT JOIN "document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
+												) LEFT JOIN "invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
+											) LEFT JOIN "vehicles" T6 ON (right(T1."vehicle_reference", 17)) = T6."vin"
+										) LEFT JOIN "makes" T7 ON T6."make_number" = T7."make_number"
+									) LEFT JOIN "models" T8 ON (T6."make_number" = T8."make_number")
+									AND (T6."model_code" = T8."model_code")
+								) LEFT JOIN "customers_suppliers" T9 ON T9."customer_number" = T6."owner_number"
+							) LEFT JOIN "customers_suppliers" T10 ON T10."customer_number" = T6."holder_number"
+						) LEFT JOIN "employees" T11 ON (T6."salesman_number" <> 0)
+						AND (T6."salesman_number" = T11."salesman_number")
+					)
+			WHERE (
+					(T2."nominal_account_number" = T1."nominal_account_number")
+					AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+					)
+				AND (
+					(
+						(
+							(T2."is_profit_loss_account" = 'J')
+							AND (T1."vehicle_reference" <> ' ')
+							)
+						AND (T1."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 (T1."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 = 'FZG-Geschäft')
+				)
+			AND (c143 = "Invoice_Date_1")
+			)
+	) D4
+	-- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc

+ 471 - 252
System/LOCOSOFT/IQD/NW/NW_GW_BE_auf_NW_GW_Bestand_Cat.sql

@@ -1,252 +1,471 @@
-select convert(date, '1900-01-01') as "Accounting Date",
-	   '' as "Document Type",
-	   0 as "Document Number_2",
-	   0 as "Position In Document",
-	   0 as "Customer Number",
-	   0 as "Nominal Account Number",
-	   '' as "Is Balanced",
-	   0 as "Clearing Number",
-	   convert(date, '1900-01-01') as "Document Date",
-	   0 as "Posted Value",
-	   '' as "Debit Or Credit",
-	   0 as "Posted Count",
-	   0 as "Branch Number",
-	   0 as "Customer Contra Account",
-	   0 as "Nominal Contra Account",
-	   '' as "Contra Account Text",
-	   0 as "Account Form Page Number",
-	   0 as "Account Form Page Line",
-	   0 as "Serial Number Each Month",
-	   0 as "Employee Number",
-	   convert(date, '1900-01-01') as "Invoice Date",
-	   '' as "Invoice Number",
-	   '' as "Dunning Level",
-	   convert(date, '1900-01-01') as "Last Dunning Date",
-	   0 as "Journal Page",
-	   0 as "Journal Line",
-	   0 as "Cash Discount",
-	   0 as "Term Of Payment",
-	   '' as "Posting Text",
-	   '' as "Vehicle Reference",
-	   '' as "Vat Id Number",
-	   0 as "Account Statement Number",
-	   0 as "Account Statement Page",
-	   '' as "Vat Key",
-	   0 as "Days For Cash Discount",
-	   convert(date, '1900-01-01') as "Day Of Actual Accounting",
-	   0 as "Skr51 Branch",
-	   0 as "Skr51 Make",
-	   0 as "Skr51 Cost Center",
-	   0 as "Skr51 Sales Channel",
-	   0 as "Skr51 Cost Unit",
-	   '' as "Previously Used Account No",
-	   "Free Form Accounting Text",
-	   "Free Form Document Text",
-	   '' as "Nom_Account_Is Profit Loss Account",
-	   '1' as "Rechtseinheit",
-	   '' as "Betrieb",
-	   convert(date, '1900-01-01') as "Bookkeep Date",
-	   '' as "Marke",
-	   '' as "Text",
-	   '1' as "Mandant",
-	   0 as "Betrag",
-	   '' as "Konto_mit_Bezeichnung",
-	   0 as "Menge_FIBU",
-	   0 as "Stellen Cost Center",
-	   '' as "KST",
-	   0 as "Stellen Sales Channel",
-	   '' as "Absatzkanal",
-	   0 as "Stellen Cost Unit",
-	   '' as "Kostenträger_mit_Null",
-	   '' as "Kostenträger",
-	   '' as "GuV_Bilanz",
-	   '' as "Susa",
-	   '' as "Acct Nr",
-	   0 as "Stellen_Konto_Nr",
-	   SUM(0) OVER (partition by "Vehicle_Reference_berechnet") as "Summe_Betrag",
-	   "Internal Number_ori",
-	   "Vin",
-	   "License Plate",
-	   "License Plate Country",
-	   "License Plate Season",
-	   "Make Number",
-	   "Free Form Make Text",
-	   "Model Code",
-	   "Free Form Model Text",
-	   "Is Roadworthy",
-	   "Is Customer Vehicle",
-	   "Dealer Vehicle Type",
-	   "Dealer Vehicle Number",
-	   "First Registration Date",
-	   "Readmission Date",
-	   "Next Service Date",
-	   "Next Service Km",
-	   "Next Service Miles",
-	   "Production Year",
-	   "Owner Number",
-	   "Holder Number",
-	   "Previous Owner Number",
-	   "Previous Owner Counter",
-	   "Last Holder Change Date",
-	   "German Kba Hsn",
-	   "German Kba Tsn",
-	   "Austria Nat Code",
-	   "Is Prefer Km",
-	   "Mileage Km",
-	   "Mileage Miles",
-	   "Odometer Reading Date",
-	   "Engine Number",
-	   "Gear Number",
-	   "Unloaded Weight",
-	   "Gross Vehicle Weight",
-	   "Power Kw",
-	   "Cubic Capacity",
-	   "Is All Accidents Repaired",
-	   "Accidents Counter",
-	   "Has Tyre Pressure Sensor",
-	   "Carkey Number",
-	   "Internal Source Flag",
-	   "Emission Code",
-	   "First Sold Country",
-	   "First Sold Dealer Code",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Is Body Paint Metallic",
-	   "Interior Paint Code",
-	   "Interior Paint Description",
-	   "Trim Code",
-	   "Trim Description",
-	   "Fine Dust Label",
-	   "Internal Assignment",
-	   "Ricambi Free Input",
-	   "Document Number",
-	   "Salesman Number",
-	   "Sale Date",
-	   "Next Emission Test Date",
-	   "Next General Inspection Date",
-	   "Next Rust Inspection Date",
-	   "Next Exceptional Inspection Da",
-	   "Last Change Date",
-	   "Last Change Employee No",
-	   "Created Date",
-	   "Created Employee No",
-	   "Last Change Subsidiary",
-	   MIN(convert(date, '1900-01-01')) OVER (partition by "Vehicle_Reference_berechnet") as "Minimum_Accounting_Date",
-	   "Hauptbetrieb",
-	   "Standort",
-	   "Description_Makes",
-	   "Description_Makes" as "Fabrikat",
-	   "Description_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Body Paint Description" as "Farbe",
-	   "Customer Number_Vorbesitzer",
-	   "First Name_Vorbesitzer",
-	   "Family Name_Vorbesitzer",
-	   "Vorbesitzer",
-	   "First Registration Date" as "Zulassungsdatum",
-	   1 as "Menge_1",
-	   COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet") as "Menge_2",
-	   1 / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet")) as "Menge",
-	   "Einsatz",
-	   CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END as "Standtage_Berechnung",
-	   (c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet")) as "Standtage",
-	   CASE WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 0 AND 30) THEN ('0 - 30 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 31 AND 60) THEN ('31 - 60 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 61 AND 90) THEN ('61 - 90 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 91 AND 180) THEN ('91 - 180 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 181 AND 360) THEN ('181 - 360 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) > 360) THEN ('> 360 Tage') ELSE null END as "Standtagestaffel",
-	   (getdate()) as "Heute",
-	   "FZG",
-	   "Vehicle_Reference_berechnet",
-	   "Internal Number_ori" as "Internal Number"
-from 
-(select ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) as "Vehicle_Reference_berechnet",
-	   T2."internal_number" as "Internal Number_ori",
-	   ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) + ' - ' + (substring((CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END), 1, 170)) as "FZG",
-	   -1 * datediff(day, (getdate()), T1."in_buy_invoice_no_date") as c169,
-	   T1."in_buy_invoice_no_date" as c170,
-	   -1 * datediff(day, (getdate()), T1."in_arrival_date") as c171,
-	   T1."calc_basic_charge" + T1."calc_accessory" + T1."calc_extra_expenses" + T1."calc_usage_value_encr_external" + T1."calc_usage_value_encr_internal" + T1."calc_usage_value_encr_other" + T1."calc_total_writedown" + T1."calc_commission_for_arranging" + T1."calc_cost_internal_invoices" + T1."calc_cost_other" + T1."calc_returns_workshop" + (T1."calc_sales_aid" + T1."calc_sales_aid_finish" + T1."calc_sales_aid_bonus") * -1 as "Einsatz",
-	   T2."first_registration_date" as "First Registration Date",
-	   (left((((T5."customer_number"))),7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" as "Vorbesitzer",
-	   T5."family_name" as "Family Name_Vorbesitzer",
-	   T5."first_name" as "First Name_Vorbesitzer",
-	   T5."customer_number" as "Customer Number_Vorbesitzer",
-	   T2."body_paint_description" as "Body Paint Description",
-	   CASE WHEN ((CASE WHEN ((left(T2."dealer_vehicle_type",1)) IN ('N')) THEN ('NW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('V')) THEN ('VFW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('D')) THEN ('GW diff.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('G')) THEN ('GW regel.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN ((left(T2."dealer_vehicle_type",1)) IN ('N')) THEN ('NW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('V')) THEN ('VFW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('D')) THEN ('GW diff.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('G')) THEN ('GW regel.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (substring((upper((CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END))), 1, 3)) as "Model",
-	   CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END as "Model_Detail",
-	   CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END as "Description_Models",
-	   T3."description" as "Description_Makes",
-	   '0' + (((T1."in_subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."in_subsidiary")))) = '015                                                                                                                                                                                                                                                            ') THEN ('15') ELSE (T1."client_db") END as "Hauptbetrieb",
-	   T2."last_change_subsidiary" as "Last Change Subsidiary",
-	   T2."created_employee_no" as "Created Employee No",
-	   T2."created_date" as "Created Date",
-	   T2."last_change_employee_no" as "Last Change Employee No",
-	   T2."last_change_date" as "Last Change Date",
-	   T2."next_exceptional_inspection_da" as "Next Exceptional Inspection Da",
-	   T2."next_rust_inspection_date" as "Next Rust Inspection Date",
-	   T2."next_general_inspection_date" as "Next General Inspection Date",
-	   T2."next_emission_test_date" as "Next Emission Test Date",
-	   T2."sale_date" as "Sale Date",
-	   T2."salesman_number" as "Salesman Number",
-	   T2."document_number" as "Document Number",
-	   T2."ricambi_free_input" as "Ricambi Free Input",
-	   T2."internal_assignment" as "Internal Assignment",
-	   T2."fine_dust_label" as "Fine Dust Label",
-	   T2."trim_description" as "Trim Description",
-	   T2."trim_code" as "Trim Code",
-	   T2."interior_paint_description" as "Interior Paint Description",
-	   T2."interior_paint_code" as "Interior Paint Code",
-	   T2."is_body_paint_metallic" as "Is Body Paint Metallic",
-	   T2."body_paint_code" as "Body Paint Code",
-	   T2."first_sold_dealer_code" as "First Sold Dealer Code",
-	   T2."first_sold_country" as "First Sold Country",
-	   T2."emission_code" as "Emission Code",
-	   T2."internal_source_flag" as "Internal Source Flag",
-	   T2."carkey_number" as "Carkey Number",
-	   T2."has_tyre_pressure_sensor" as "Has Tyre Pressure Sensor",
-	   T2."accidents_counter" as "Accidents Counter",
-	   T2."is_all_accidents_repaired" as "Is All Accidents Repaired",
-	   T2."cubic_capacity" as "Cubic Capacity",
-	   T2."power_kw" as "Power Kw",
-	   T2."gross_vehicle_weight" as "Gross Vehicle Weight",
-	   T2."unloaded_weight" as "Unloaded Weight",
-	   T2."gear_number" as "Gear Number",
-	   T2."engine_number" as "Engine Number",
-	   T2."odometer_reading_date" as "Odometer Reading Date",
-	   T2."mileage_miles" as "Mileage Miles",
-	   T2."mileage_km" as "Mileage Km",
-	   T2."is_prefer_km" as "Is Prefer Km",
-	   T2."austria_nat_code" as "Austria Nat Code",
-	   T2."german_kba_tsn" as "German Kba Tsn",
-	   T2."german_kba_hsn" as "German Kba Hsn",
-	   T2."last_holder_change_date" as "Last Holder Change Date",
-	   T2."previous_owner_counter" as "Previous Owner Counter",
-	   T2."previous_owner_number" as "Previous Owner Number",
-	   T2."holder_number" as "Holder Number",
-	   T2."owner_number" as "Owner Number",
-	   T2."production_year" as "Production Year",
-	   T2."next_service_miles" as "Next Service Miles",
-	   T2."next_service_km" as "Next Service Km",
-	   T2."next_service_date" as "Next Service Date",
-	   T2."readmission_date" as "Readmission Date",
-	   T2."dealer_vehicle_number" as "Dealer Vehicle Number",
-	   T2."dealer_vehicle_type" as "Dealer Vehicle Type",
-	   T2."is_customer_vehicle" as "Is Customer Vehicle",
-	   T2."is_roadworthy" as "Is Roadworthy",
-	   T2."free_form_model_text" as "Free Form Model Text",
-	   T2."model_code" as "Model Code",
-	   T2."free_form_make_text" as "Free Form Make Text",
-	   T2."make_number" as "Make Number",
-	   T2."license_plate_season" as "License Plate Season",
-	   T2."license_plate_country" as "License Plate Country",
-	   T2."license_plate" as "License Plate",
-	   T2."vin" as "Vin",
-	   (rtrim((((T1."dealer_vehicle_number"))))) as "Free Form Document Text",
-	   T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))) as "Free Form Accounting Text"
-from (((("dbo"."vehicles" T2 left outer join "dbo"."makes" T3 on T2."make_number" = T3."make_number") left outer join "dbo"."models" T4 on (T2."make_number" = T4."make_number") and (T2."model_code" = T4."model_code")) left outer join "dbo"."customers_suppliers" T5 on T2."previous_owner_number" = T5."customer_number") left outer join "LOCOSOFT"."dbo"."dealer_vehicles" T1 on (T1."dealer_vehicle_type" = T2."dealer_vehicle_type") and (T1."dealer_vehicle_number" = T2."dealer_vehicle_number"))
-where ((T1."in_arrival_date" IS NOT NULL) and (T1."out_invoice_number" IS NULL))
--- order by "Vehicle_Reference_berechnet" asc
-) D1
+SELECT convert(DATE, '1900-01-01') AS "Accounting Date",
+	'' AS "Document Type",
+	0 AS "Document Number_2",
+	0 AS "Position In Document",
+	0 AS "Customer Number",
+	0 AS "Nominal Account Number",
+	'' AS "Is Balanced",
+	0 AS "Clearing Number",
+	convert(DATE, '1900-01-01') AS "Document Date",
+	0 AS "Posted Value",
+	'' AS "Debit Or Credit",
+	0 AS "Posted Count",
+	0 AS "Branch Number",
+	0 AS "Customer Contra Account",
+	0 AS "Nominal Contra Account",
+	'' AS "Contra Account Text",
+	0 AS "Account Form Page Number",
+	0 AS "Account Form Page Line",
+	0 AS "Serial Number Each Month",
+	0 AS "Employee Number",
+	convert(DATE, '1900-01-01') AS "Invoice Date",
+	'' AS "Invoice Number",
+	'' AS "Dunning Level",
+	convert(DATE, '1900-01-01') AS "Last Dunning Date",
+	0 AS "Journal Page",
+	0 AS "Journal Line",
+	0 AS "Cash Discount",
+	0 AS "Term Of Payment",
+	'' AS "Posting Text",
+	'' AS "Vehicle Reference",
+	'' AS "Vat Id Number",
+	0 AS "Account Statement Number",
+	0 AS "Account Statement Page",
+	'' AS "Vat Key",
+	0 AS "Days For Cash Discount",
+	convert(DATE, '1900-01-01') AS "Day Of Actual Accounting",
+	0 AS "Skr51 Branch",
+	0 AS "Skr51 Make",
+	0 AS "Skr51 Cost Center",
+	0 AS "Skr51 Sales Channel",
+	0 AS "Skr51 Cost Unit",
+	'' AS "Previously Used Account No",
+	"Free Form Accounting Text",
+	"Free Form Document Text",
+	'' AS "Nom_Account_Is Profit Loss Account",
+	'1' AS "Rechtseinheit",
+	'' AS "Betrieb",
+	convert(DATE, '1900-01-01') AS "Bookkeep Date",
+	'' AS "Marke",
+	'' AS "Text",
+	'1' AS "Mandant",
+	0 AS "Betrag",
+	'' AS "Konto_mit_Bezeichnung",
+	0 AS "Menge_FIBU",
+	0 AS "Stellen Cost Center",
+	'' AS "KST",
+	0 AS "Stellen Sales Channel",
+	'' AS "Absatzkanal",
+	0 AS "Stellen Cost Unit",
+	'' AS "Kostentr�ger_mit_Null",
+	'' AS "Kostentr�ger",
+	'' AS "GuV_Bilanz",
+	'' AS "Susa",
+	'' AS "Acct Nr",
+	0 AS "Stellen_Konto_Nr",
+	SUM(0) OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Summe_Betrag",
+	"Internal Number_ori",
+	"Vin",
+	"License Plate",
+	"License Plate Country",
+	"License Plate Season",
+	"Make Number",
+	"Free Form Make Text",
+	"Model Code",
+	"Free Form Model Text",
+	"Is Roadworthy",
+	"Is Customer Vehicle",
+	"Dealer Vehicle Type",
+	"Dealer Vehicle Number",
+	"First Registration Date",
+	"Readmission Date",
+	"Next Service Date",
+	"Next Service Km",
+	"Next Service Miles",
+	"Production Year",
+	"Owner Number",
+	"Holder Number",
+	"Previous Owner Number",
+	"Previous Owner Counter",
+	"Last Holder Change Date",
+	"German Kba Hsn",
+	"German Kba Tsn",
+	"Austria Nat Code",
+	"Is Prefer Km",
+	"Mileage Km",
+	"Mileage Miles",
+	"Odometer Reading Date",
+	"Engine Number",
+	"Gear Number",
+	"Unloaded Weight",
+	"Gross Vehicle Weight",
+	"Power Kw",
+	"Cubic Capacity",
+	"Is All Accidents Repaired",
+	"Accidents Counter",
+	"Has Tyre Pressure Sensor",
+	"Carkey Number",
+	"Internal Source Flag",
+	"Emission Code",
+	"First Sold Country",
+	"First Sold Dealer Code",
+	"Body Paint Code",
+	"Body Paint Description",
+	"Is Body Paint Metallic",
+	"Interior Paint Code",
+	"Interior Paint Description",
+	"Trim Code",
+	"Trim Description",
+	"Fine Dust Label",
+	"Internal Assignment",
+	"Ricambi Free Input",
+	"Document Number",
+	"Salesman Number",
+	"Sale Date",
+	"Next Emission Test Date",
+	"Next General Inspection Date",
+	"Next Rust Inspection Date",
+	"Next Exceptional Inspection Da",
+	"Last Change Date",
+	"Last Change Employee No",
+	"Created Date",
+	"Created Employee No",
+	"Last Change Subsidiary",
+	MIN(convert(DATE, '1900-01-01')) OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Minimum_Accounting_Date",
+	"Hauptbetrieb",
+	"Standort",
+	"Description_Makes",
+	"Description_Makes" AS "Fabrikat",
+	"Description_Models",
+	"Model_Detail",
+	"Model",
+	"Fahrzeugtyp",
+	"Fahrzeugart",
+	"Body Paint Description" AS "Farbe",
+	"Customer Number_Vorbesitzer",
+	"First Name_Vorbesitzer",
+	"Family Name_Vorbesitzer",
+	"Vorbesitzer",
+	"First Registration Date" AS "Zulassungsdatum",
+	1 AS "Menge_1",
+	COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Menge_2",
+	1 / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")) AS "Menge",
+	"Einsatz",
+	CASE 
+		WHEN (c170 IS NULL)
+			THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+		ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+		END AS "Standtage_Berechnung",
+	(c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")) AS "Standtage",
+	CASE 
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 0 AND 30
+				)
+			THEN ('0 - 30 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 31 AND 60
+				)
+			THEN ('31 - 60 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 61 AND 90
+				)
+			THEN ('61 - 90 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 91 AND 180
+				)
+			THEN ('91 - 180 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 181 AND 360
+				)
+			THEN ('181 - 360 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) > 360
+				)
+			THEN ('> 360 Tage')
+		ELSE NULL
+		END AS "Standtagestaffel",
+	(getdate()) AS "Heute",
+	"FZG",
+	"Vehicle_Reference_berechnet",
+	"Internal Number_ori" AS "Internal Number"
+FROM (
+	SELECT ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) AS "Vehicle_Reference_berechnet",
+		T2."internal_number" AS "Internal Number_ori",
+		((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) + ' - ' + (
+			substring((
+					CASE 
+						WHEN (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									) IS NULL
+								)
+							THEN ('keine Angabe')
+						WHEN (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									) LIKE '???%'
+								)
+							THEN ('???')
+						ELSE (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									)
+								)
+						END
+					), 1, 170)
+			) AS "FZG",
+		- 1 * datediff(day, (getdate()), T1."in_buy_invoice_no_date") AS c169,
+		T1."in_buy_invoice_no_date" AS c170,
+		- 1 * datediff(day, (getdate()), T1."in_arrival_date") AS c171,
+		T1."calc_basic_charge" + T1."calc_accessory" + T1."calc_extra_expenses" + T1."calc_usage_value_encr_external" + T1."calc_usage_value_encr_internal" + T1."calc_usage_value_encr_other" + T1."calc_total_writedown" + T1."calc_commission_for_arranging" + T1."calc_cost_internal_invoices" + T1."calc_cost_other" + T1."calc_returns_workshop" + (T1."calc_sales_aid" + T1."calc_sales_aid_finish" + T1."calc_sales_aid_bonus") * - 1 AS "Einsatz",
+		T2."first_registration_date" AS "First Registration Date",
+		(left((((T5."customer_number"))), 7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" AS "Vorbesitzer",
+		T5."family_name" AS "Family Name_Vorbesitzer",
+		T5."first_name" AS "First Name_Vorbesitzer",
+		T5."customer_number" AS "Customer Number_Vorbesitzer",
+		T2."body_paint_description" AS "Body Paint Description",
+		CASE 
+			WHEN (
+					(
+						CASE 
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('N'))
+								THEN ('NW')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('V'))
+								THEN ('VFW')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('D'))
+								THEN ('GW diff.')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('G'))
+								THEN ('GW regel.')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('T'))
+								THEN ('TZ')
+							ELSE NULL
+							END
+						) IN ('VFW', 'TZ', 'NW')
+					)
+				THEN ('Neuwagen')
+			ELSE ('Gebrauchtwagen')
+			END AS "Fahrzeugart",
+		CASE 
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('N'))
+				THEN ('NW')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('V'))
+				THEN ('VFW')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('D'))
+				THEN ('GW diff.')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('G'))
+				THEN ('GW regel.')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('T'))
+				THEN ('TZ')
+			ELSE NULL
+			END AS "Fahrzeugtyp",
+		(
+			substring((
+					upper((
+							CASE 
+								WHEN (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											) IS NULL
+										)
+									THEN ('keine Angabe')
+								WHEN (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											) LIKE '???%'
+										)
+									THEN ('???')
+								ELSE (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											)
+										)
+								END
+							))
+					), 1, 3)
+			) AS "Model",
+		CASE 
+			WHEN (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						) IS NULL
+					)
+				THEN ('keine Angabe')
+			WHEN (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						) LIKE '???%'
+					)
+				THEN ('???')
+			ELSE (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						)
+					)
+			END AS "Model_Detail",
+		CASE 
+			WHEN (T4."description" = 'DIVERSE')
+				THEN (T2."free_form_model_text")
+			ELSE (T4."description")
+			END AS "Description_Models",
+		T3."description" AS "Description_Makes",
+		'0' + (((T1."in_subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."in_subsidiary")))) = '015                                                                                                                                                                                                                                                            ')
+				THEN ('15')
+			ELSE (T1."client_db")
+			END AS "Hauptbetrieb",
+		T2."last_change_subsidiary" AS "Last Change Subsidiary",
+		T2."created_employee_no" AS "Created Employee No",
+		T2."created_date" AS "Created Date",
+		T2."last_change_employee_no" AS "Last Change Employee No",
+		T2."last_change_date" AS "Last Change Date",
+		T2."next_exceptional_inspection_da" AS "Next Exceptional Inspection Da",
+		T2."next_rust_inspection_date" AS "Next Rust Inspection Date",
+		T2."next_general_inspection_date" AS "Next General Inspection Date",
+		T2."next_emission_test_date" AS "Next Emission Test Date",
+		T2."sale_date" AS "Sale Date",
+		T2."salesman_number" AS "Salesman Number",
+		T2."document_number" AS "Document Number",
+		T2."ricambi_free_input" AS "Ricambi Free Input",
+		T2."internal_assignment" AS "Internal Assignment",
+		T2."fine_dust_label" AS "Fine Dust Label",
+		T2."trim_description" AS "Trim Description",
+		T2."trim_code" AS "Trim Code",
+		T2."interior_paint_description" AS "Interior Paint Description",
+		T2."interior_paint_code" AS "Interior Paint Code",
+		T2."is_body_paint_metallic" AS "Is Body Paint Metallic",
+		T2."body_paint_code" AS "Body Paint Code",
+		T2."first_sold_dealer_code" AS "First Sold Dealer Code",
+		T2."first_sold_country" AS "First Sold Country",
+		T2."emission_code" AS "Emission Code",
+		T2."internal_source_flag" AS "Internal Source Flag",
+		T2."carkey_number" AS "Carkey Number",
+		T2."has_tyre_pressure_sensor" AS "Has Tyre Pressure Sensor",
+		T2."accidents_counter" AS "Accidents Counter",
+		T2."is_all_accidents_repaired" AS "Is All Accidents Repaired",
+		T2."cubic_capacity" AS "Cubic Capacity",
+		T2."power_kw" AS "Power Kw",
+		T2."gross_vehicle_weight" AS "Gross Vehicle Weight",
+		T2."unloaded_weight" AS "Unloaded Weight",
+		T2."gear_number" AS "Gear Number",
+		T2."engine_number" AS "Engine Number",
+		T2."odometer_reading_date" AS "Odometer Reading Date",
+		T2."mileage_miles" AS "Mileage Miles",
+		T2."mileage_km" AS "Mileage Km",
+		T2."is_prefer_km" AS "Is Prefer Km",
+		T2."austria_nat_code" AS "Austria Nat Code",
+		T2."german_kba_tsn" AS "German Kba Tsn",
+		T2."german_kba_hsn" AS "German Kba Hsn",
+		T2."last_holder_change_date" AS "Last Holder Change Date",
+		T2."previous_owner_counter" AS "Previous Owner Counter",
+		T2."previous_owner_number" AS "Previous Owner Number",
+		T2."holder_number" AS "Holder Number",
+		T2."owner_number" AS "Owner Number",
+		T2."production_year" AS "Production Year",
+		T2."next_service_miles" AS "Next Service Miles",
+		T2."next_service_km" AS "Next Service Km",
+		T2."next_service_date" AS "Next Service Date",
+		T2."readmission_date" AS "Readmission Date",
+		T2."dealer_vehicle_number" AS "Dealer Vehicle Number",
+		T2."dealer_vehicle_type" AS "Dealer Vehicle Type",
+		T2."is_customer_vehicle" AS "Is Customer Vehicle",
+		T2."is_roadworthy" AS "Is Roadworthy",
+		T2."free_form_model_text" AS "Free Form Model Text",
+		T2."model_code" AS "Model Code",
+		T2."free_form_make_text" AS "Free Form Make Text",
+		T2."make_number" AS "Make Number",
+		T2."license_plate_season" AS "License Plate Season",
+		T2."license_plate_country" AS "License Plate Country",
+		T2."license_plate" AS "License Plate",
+		T2."vin" AS "Vin",
+		(rtrim((((T1."dealer_vehicle_number"))))) AS "Free Form Document Text",
+		T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))) AS "Free Form Accounting Text"
+	FROM (
+		(
+			(
+				(
+					"dbo"."vehicles" T2 LEFT JOIN "dbo"."makes" T3 ON T2."make_number" = T3."make_number"
+					) LEFT JOIN "dbo"."models" T4 ON (T2."make_number" = T4."make_number")
+					AND (T2."model_code" = T4."model_code")
+				) LEFT JOIN "dbo"."customers_suppliers" T5 ON T2."previous_owner_number" = T5."customer_number"
+			) LEFT JOIN "LOCOSOFT"."dbo"."dealer_vehicles" T1 ON (T1."dealer_vehicle_type" = T2."dealer_vehicle_type")
+			AND (T1."dealer_vehicle_number" = T2."dealer_vehicle_number")
+		)
+	WHERE (
+			(T1."in_arrival_date" IS NOT NULL)
+			AND (T1."out_invoice_number" IS NULL)
+			)
+		-- order by "Vehicle_Reference_berechnet" asc
+	) D1

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 111
System/LOCOSOFT/IQD/OP/Kreditorenbewegungen.sql


+ 19 - 13
System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_8520.sql

@@ -1,13 +1,19 @@
-select distinct T1."document_type" as "Document Type",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   '1' as "Rechtseinheit",
-	   '0' + (((T1."skr51_branch"))) as "Betrieb",
-	   '1' as "Mandant",
-	   '1' as "Hauptbetrieb",
-	   ('0' + (((T1."skr51_branch")))) as "Standort"
-from "journal_accountings" T1
-where (((T1."nominal_account_number" IN (8520)) and (T1."document_type" = 'R')) and (T1."invoice_date" >= convert(date, '2016-01-01')))
--- order by "Invoice Number" asc,"Nominal Account Number" asc
+SELECT DISTINCT T1."document_type" AS "Document Type",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."invoice_number" AS "Invoice Number",
+	T1."skr51_branch" AS "Skr51 Branch",
+	'1' AS "Rechtseinheit",
+	'0' + (((T1."skr51_branch"))) AS "Betrieb",
+	'1' AS "Mandant",
+	'1' AS "Hauptbetrieb",
+	('0' + (((T1."skr51_branch")))) AS "Standort"
+FROM "journal_accountings" T1
+WHERE (
+		(
+			(T1."nominal_account_number" IN (8520))
+			AND (T1."document_type" = 'R')
+			)
+		AND (T1."invoice_date" >= convert(DATE, '2016-01-01'))
+		)
+	-- order by "Invoice Number" asc,"Nominal Account Number" asc

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 190
System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_Deb_Saldo.sql


+ 306 - 160
System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_nur_8520.sql

@@ -1,160 +1,306 @@
-select "Accounting Date",
-	   "Document Type",
-	   "Document Number",
-	   "Position In Document",
-	   "Nominal Account Number",
-	   "Customer 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",
-	   "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",
-	   '1' as "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag",
-	   "Menge",
-	   "Nominal Account Number" as "Acct Nr",
-	   "Customer Number_suctomer_supplier",
-	   "Is Supplier",
-	   "First Name",
-	   "Family Name",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   SUM("Betrag") OVER (partition by "Customer Number_suctomer_supplier") as "Debitorsaldo_",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Subsidiary_Invoices",
-	   "Invoice Date_Invoices",
-	   "Hauptbetrieb",
-	   "Standort_ori",
-	   "Sel Name",
-	   "Betrag" as "OP-Saldo",
-	   "Kostenstelle",
-	   "Tage",
-	   "Staffel",
-	   "Kunde",
-	   "Beleg",
-	   "Employee Number_Employees_journal_accountings",
-	   "Name_Employees_journal_accountings",
-	   "Anzahl_Stellen_Invoice_Number",
-	   "Forderungsart_aus_erste_Stelle_Inv_Number",
-	   "Forderungsart",
-	   "Standort_aus_2_Stelle_Invoice_Number",
-	   "Betrieb" as "Standort",
-	   "Standort_ims_8520"
-from 
-(select T3."customer_number" as "Customer Number_suctomer_supplier",
-	   T6."Standort" as "Standort_ims_8520",
-	   ('0' + (((T1."subsidiary_to_company_ref")))) as "Betrieb",
-	   CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((substring(T1."invoice_number", 2, 1)) BETWEEN '1' AND '4')) THEN ('0' + (substring(T1."invoice_number", 2, 1))) ELSE null END as "Standort_aus_2_Stelle_Invoice_Number",
-	   CASE WHEN (T1."dunning_level" = 'Gesperrt') THEN ('Versicherung') ELSE ((CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('2','3','4'))) THEN ('Service') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('5'))) THEN ('Teile') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('6'))) THEN ('Garantie') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('7'))) THEN ('Neuwagen') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('8'))) THEN ('Gebrauchtwagen') WHEN (T1."document_type" = 'R') THEN ('Rent') ELSE ('nicht zuzuordnen') END)) END as "Forderungsart",
-	   CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('2','3','4'))) THEN ('Service') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('5'))) THEN ('Teile') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('6'))) THEN ('Garantie') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('7'))) THEN ('Neuwagen') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('8'))) THEN ('Gebrauchtwagen') WHEN (T1."document_type" = 'R') THEN ('Rent') ELSE ('nicht zuzuordnen') END as "Forderungsart_aus_erste_Stelle_Inv_Number",
-	   (len(T1."invoice_number" + 'Z') - 1) as "Anzahl_Stellen_Invoice_Number",
-	   T2."name" as "Name_Employees_journal_accountings",
-	   T2."employee_number" as "Employee Number_Employees_journal_accountings",
-	   CASE WHEN (T1."posting_text" IS NOT NULL) THEN ((substring((convert(varchar(50), ((T1."document_number")))), 1, 7)) + ' - ' + T1."posting_text" + ' - ' + (convert(varchar(50), year(T1."document_date")) + '-' + convert(varchar(50), month(T1."document_date")) + '-' + convert(varchar(50), day(T1."document_date")))) ELSE ((convert(varchar(50), year(T1."document_date")) + '-' + convert(varchar(50), month(T1."document_date")) + '-' + convert(varchar(50), day(T1."document_date")))) END as "Beleg",
-	   CASE WHEN (T3."first_name" <> ' ') THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") END as "Kunde",
-	   CASE WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 0 AND 14) THEN ('< 2 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 15 AND 28) THEN ('2 - 4 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 29 AND 42) THEN ('4 - 6 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 43 AND 84) THEN ('6 - 12 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) > 84) THEN ('> 12 Wochen') ELSE null END as "Staffel",
-	   (-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) as "Tage",
-	   CASE WHEN (T5."invoice_type" IN (2,4,6,3)) THEN ('3') WHEN (T5."invoice_type" IN (5)) THEN ('6') WHEN (T5."invoice_type" IN (7)) THEN ('1') WHEN (T5."invoice_type" IN (8)) THEN ('2') WHEN (T4."document_type_in_journal" = 'R') THEN ('7') ELSE null END as "Kostenstelle",
-	   (CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) as "Betrag",
-	   (left((((T2."employee_number"))),3)) + ' - ' + T2."name" as "Sel Name",
-	   CASE WHEN (T5."subsidiary" IS NOT NULL) THEN ('0' + (left((((T5."subsidiary"))),1))) ELSE ('nicht zuzuordnen') END as "Standort_ori",
-	   CASE WHEN ((('0' + (((T1."subsidiary_to_company_ref"))))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."subsidiary" as "Subsidiary_Invoices",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_type" as "Invoice Type_Invoices",
-	   T4."document_type_description" as "Document Type Description",
-	   T4."document_type_in_journal" as "Document Type In Journal",
-	   T3."family_name" as "Family Name",
-	   T3."first_name" as "First Name",
-	   T3."is_supplier" as "Is Supplier",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   ((rtrim((convert(varchar(50), ((T1."document_number")))))) + ' - ' + T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + ((left((((T2."employee_number"))))) + ' - ' + T2."name")) as "Text",
-	   T1."accounting_date" as "Accounting Date",
-	   T1."free_form_document_text" as "Free Form Document Text",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."customer_number" as "Customer Number",
-	   T1."position_in_document" as "Position In Document",
-	   (T1."document_number") as "Document Number",
-	   T1."document_type" as "Document Type"
-from "ims"."OP_aus_LOC_Belege_8520" T6,
-	(((("journal_accountings" T1 left outer join "LOCOSOFT"."dbo"."employees" T2 on T1."employee_number" = T2."employee_number") left outer join "LOCOSOFT"."dbo"."customers_suppliers" T3 on T3."customer_number" = T1."customer_number") left outer join "document_types" T4 on T4."document_type_in_journal" = T1."document_type") left outer join "invoices" T5 on T5."invtype_invnr" = T1."invoice_number")
-where ((T1."invoice_number" = T6."invoice_number") and (T1."customer_number" = T6."customer_contra_account"))
-and (((((T1."nominal_account_number" IN (1400,1409,1451,1408,1525)) and (T1."clearing_number" = 0)) and (T1."customer_number" IS NOT NULL)) and (not T4."document_type_in_journal" IN ('A','E','U','V','L'))) and (T3."customer_number" IS NOT NULL))
-) D1
--- order by "Customer Number" asc,"Document Type In Journal" asc,"Nominal Account Number" asc
+SELECT "Accounting Date",
+	"Document Type",
+	"Document Number",
+	"Position In Document",
+	"Nominal Account Number",
+	"Customer 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",
+	"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",
+	'1' AS "Rechtseinheit",
+	"Betrieb",
+	"Accounting Date" AS "Bookkeep Date",
+	"Text",
+	'1' AS "Mandant",
+	"Betrag",
+	"Menge",
+	"Nominal Account Number" AS "Acct Nr",
+	"Customer Number_suctomer_supplier",
+	"Is Supplier",
+	"First Name",
+	"Family Name",
+	"Document Type In Journal",
+	"Document Type Description",
+	SUM("Betrag") OVER (PARTITION BY "Customer Number_suctomer_supplier") AS "Debitorsaldo_",
+	"Invoice Type_Invoices",
+	"Invoice Number_Invoices",
+	"Subsidiary_Invoices",
+	"Invoice Date_Invoices",
+	"Hauptbetrieb",
+	"Standort_ori",
+	"Sel Name",
+	"Betrag" AS "OP-Saldo",
+	"Kostenstelle",
+	"Tage",
+	"Staffel",
+	"Kunde",
+	"Beleg",
+	"Employee Number_Employees_journal_accountings",
+	"Name_Employees_journal_accountings",
+	"Anzahl_Stellen_Invoice_Number",
+	"Forderungsart_aus_erste_Stelle_Inv_Number",
+	"Forderungsart",
+	"Standort_aus_2_Stelle_Invoice_Number",
+	"Betrieb" AS "Standort",
+	"Standort_ims_8520"
+FROM (
+	SELECT T3."customer_number" AS "Customer Number_suctomer_supplier",
+		T6."Standort" AS "Standort_ims_8520",
+		('0' + (((T1."subsidiary_to_company_ref")))) AS "Betrieb",
+		CASE 
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((substring(T1."invoice_number", 2, 1)) BETWEEN '1' AND '4')
+					)
+				THEN ('0' + (substring(T1."invoice_number", 2, 1)))
+			ELSE NULL
+			END AS "Standort_aus_2_Stelle_Invoice_Number",
+		CASE 
+			WHEN (T1."dunning_level" = 'Gesperrt')
+				THEN ('Versicherung')
+			ELSE (
+					(
+						CASE 
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('2', '3', '4'))
+									)
+								THEN ('Service')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('5'))
+									)
+								THEN ('Teile')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('6'))
+									)
+								THEN ('Garantie')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('7'))
+									)
+								THEN ('Neuwagen')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('8'))
+									)
+								THEN ('Gebrauchtwagen')
+							WHEN (T1."document_type" = 'R')
+								THEN ('Rent')
+							ELSE ('nicht zuzuordnen')
+							END
+						)
+					)
+			END AS "Forderungsart",
+		CASE 
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('2', '3', '4'))
+					)
+				THEN ('Service')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('5'))
+					)
+				THEN ('Teile')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('6'))
+					)
+				THEN ('Garantie')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('7'))
+					)
+				THEN ('Neuwagen')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('8'))
+					)
+				THEN ('Gebrauchtwagen')
+			WHEN (T1."document_type" = 'R')
+				THEN ('Rent')
+			ELSE ('nicht zuzuordnen')
+			END AS "Forderungsart_aus_erste_Stelle_Inv_Number",
+		(len(T1."invoice_number" + 'Z') - 1) AS "Anzahl_Stellen_Invoice_Number",
+		T2."name" AS "Name_Employees_journal_accountings",
+		T2."employee_number" AS "Employee Number_Employees_journal_accountings",
+		CASE 
+			WHEN (T1."posting_text" IS NOT NULL)
+				THEN ((substring((convert(VARCHAR(50), ((T1."document_number")))), 1, 7)) + ' - ' + T1."posting_text" + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date"))))
+			ELSE ((convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date"))))
+			END AS "Beleg",
+		CASE 
+			WHEN (T3."first_name" <> ' ')
+				THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+			ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+			END AS "Kunde",
+		CASE 
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 0 AND 14)
+				THEN ('< 2 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 15 AND 28)
+				THEN ('2 - 4 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 29 AND 42)
+				THEN ('4 - 6 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 43 AND 84)
+				THEN ('6 - 12 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) > 84)
+				THEN ('> 12 Wochen')
+			ELSE NULL
+			END AS "Staffel",
+		(- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) AS "Tage",
+		CASE 
+			WHEN (T5."invoice_type" IN (2, 4, 6, 3))
+				THEN ('3')
+			WHEN (T5."invoice_type" IN (5))
+				THEN ('6')
+			WHEN (T5."invoice_type" IN (7))
+				THEN ('1')
+			WHEN (T5."invoice_type" IN (8))
+				THEN ('2')
+			WHEN (T4."document_type_in_journal" = 'R')
+				THEN ('7')
+			ELSE NULL
+			END AS "Kostenstelle",
+		(
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_value" / 100 * - 1)
+				ELSE (T1."posted_value" / 100)
+				END
+			) AS "Betrag",
+		(left((((T2."employee_number"))), 3)) + ' - ' + T2."name" AS "Sel Name",
+		CASE 
+			WHEN (T5."subsidiary" IS NOT NULL)
+				THEN ('0' + (left((((T5."subsidiary"))), 1)))
+			ELSE ('nicht zuzuordnen')
+			END AS "Standort_ori",
+		CASE 
+			WHEN ((('0' + (((T1."subsidiary_to_company_ref"))))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		T5."invoice_date" AS "Invoice Date_Invoices",
+		T5."subsidiary" AS "Subsidiary_Invoices",
+		T5."invoice_number" AS "Invoice Number_Invoices",
+		T5."invoice_type" AS "Invoice Type_Invoices",
+		T4."document_type_description" AS "Document Type Description",
+		T4."document_type_in_journal" AS "Document Type In Journal",
+		T3."family_name" AS "Family Name",
+		T3."first_name" AS "First Name",
+		T3."is_supplier" AS "Is Supplier",
+		T1."nominal_account_number" AS "Nominal Account Number",
+		CASE 
+			WHEN (T1."debit_or_credit" = 'H')
+				THEN (T1."posted_count" / 100 * - 1)
+			ELSE (T1."posted_count" / 100)
+			END AS "Menge",
+		((rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + ((left((((T2."employee_number"))))) + ' - ' + T2."name")) AS "Text",
+		T1."accounting_date" AS "Accounting Date",
+		T1."free_form_document_text" AS "Free Form Document Text",
+		T1."free_form_accounting_text" AS "Free Form Accounting Text",
+		T1."previously_used_account_no" AS "Previously Used Account No",
+		T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+		T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+		T1."skr51_cost_center" AS "Skr51 Cost Center",
+		T1."skr51_make" AS "Skr51 Make",
+		T1."skr51_branch" AS "Skr51 Branch",
+		T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+		T1."days_for_cash_discount" AS "Days For Cash Discount",
+		T1."vat_key" AS "Vat Key",
+		T1."account_statement_page" AS "Account Statement Page",
+		T1."account_statement_number" AS "Account Statement Number",
+		T1."vat_id_number" AS "Vat Id Number",
+		T1."vehicle_reference" AS "Vehicle Reference",
+		T1."posting_text" AS "Posting Text",
+		T1."term_of_payment" AS "Term Of Payment",
+		T1."cash_discount" AS "Cash Discount",
+		T1."journal_line" AS "Journal Line",
+		T1."journal_page" AS "Journal Page",
+		T1."last_dunning_date" AS "Last Dunning Date",
+		T1."dunning_level" AS "Dunning Level",
+		T1."invoice_number" AS "Invoice Number",
+		T1."invoice_date" AS "Invoice Date",
+		T1."employee_number" AS "Employee Number",
+		T1."serial_number_each_month" AS "Serial Number Each Month",
+		T1."account_form_page_line" AS "Account Form Page Line",
+		T1."account_form_page_number" AS "Account Form Page Number",
+		T1."contra_account_text" AS "Contra Account Text",
+		T1."nominal_contra_account" AS "Nominal Contra Account",
+		T1."customer_contra_account" AS "Customer Contra Account",
+		T1."branch_number" AS "Branch Number",
+		T1."posted_count" AS "Posted Count",
+		T1."debit_or_credit" AS "Debit Or Credit",
+		T1."posted_value" AS "Posted Value",
+		T1."document_date" AS "Document Date",
+		T1."clearing_number" AS "Clearing Number",
+		T1."is_balanced" AS "Is Balanced",
+		T1."customer_number" AS "Customer Number",
+		T1."position_in_document" AS "Position In Document",
+		(T1."document_number") AS "Document Number",
+		T1."document_type" AS "Document Type"
+	FROM "ims"."OP_aus_LOC_Belege_8520" T6,
+		(
+			(
+				(
+					(
+						"journal_accountings" T1 LEFT JOIN "LOCOSOFT"."dbo"."employees" T2 ON T1."employee_number" = T2."employee_number"
+						) LEFT JOIN "LOCOSOFT"."dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."customer_number"
+					) LEFT JOIN "document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
+				) LEFT JOIN "invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
+			)
+	WHERE (
+			(T1."invoice_number" = T6."invoice_number")
+			AND (T1."customer_number" = T6."customer_contra_account")
+			)
+		AND (
+			(
+				(
+					(
+						(T1."nominal_account_number" IN (1400, 1409, 1451, 1408, 1525))
+						AND (T1."clearing_number" = 0)
+						)
+					AND (T1."customer_number" IS NOT NULL)
+					)
+				AND (NOT T4."document_type_in_journal" IN ('A', 'E', 'U', 'V', 'L'))
+				)
+			AND (T3."customer_number" IS NOT NULL)
+			)
+	) D1
+	-- order by "Customer Number" asc,"Document Type In Journal" asc,"Nominal Account Number" asc

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 190
System/LOCOSOFT/IQD/OP/OP_aus_LOC_Belege_ohne_8520.sql


+ 184 - 69
System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_ben_AW_final.sql

@@ -1,69 +1,184 @@
-select "Invoice Type",
-	   "Invoice Number",
-	   "Subsidiary",
-	   "Invoice Date",
-	   "Service Date",
-	   "Is Canceled",
-	   "Vehicle Number",
-	   "Invoice_Type_Invoice_Number",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Serviceberater",
-	   "Umsatzart",
-	   "Fabrikat",
-	   "Fabrikat" as "Description_Makes",
-	   "Description_Models",
-	   "Model",
-	   "Fahrzeug",
-	   "Kostenstelle",
-	   "Marke",
-	   'Service' as "Auftragsart",
-	   "Geschäftsart",
-	   "Kunde",
-	   "Time Units",
-	   "Time Units_Zahl",
-	   10 as "AW/Std.",
-	   "verk. Std._",
-	   "Order Number",
-	   "Order Number_Rg_Ausg",
-	   "Kundenart",
-	   "Summe Duration Minutes",
-	   "Order Number_ben_AW",
-	   1 as "Anzahl_Datensätze_1",
-	   COUNT("Order Number_ben_AW") OVER (partition by "Order Number") as "Anzahl_Datensätze_2",
-	   (c38) / (COUNT("Order Number_ben_AW") OVER (partition by "Order Number")) as "ben. Std."
-from 
-(select (CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END) as "Order Number",
-	   T9."Summe_Duration_Minutes" / 60 as c38,
-	   T9."order_number" as "Order Number_ben_AW",
-	   T9."Summe_Duration_Minutes" as "Summe Duration Minutes",
-	   (left((((T8."employee_number"))),4)) + ' - ' + T8."name" as "Kundenart",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."invoice_date")))) <= 4) THEN ((CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END)) ELSE null END as "Order Number_Rg_Ausg",
-	   ((convert(float, T7."time_units"))) / 10 as "verk. Std._",
-	   (convert(float, T7."time_units")) as "Time Units_Zahl",
-	   T7."time_units" as "Time Units",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END as "Kunde",
-	   T3."zip_code" as "Geschäftsart",
-	   CASE WHEN (T4."description" = 'Ford') THEN ('1') WHEN (T4."description" = 'Nissan') THEN ('3') WHEN (T4."description" = 'Suzuki') THEN ('2') ELSE ('9') END as "Marke",
-	   CASE WHEN ((left((((T6."type"))),1)) IN ('1','6')) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN (T6."type" IN (40,41,44,45,46,47,48,49,88,89,50,90)) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN ((left((((T6."type"))),1)) IN ('2')) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN (T6."type" IN (42,92)) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN ((left((((T6."type"))),1)) IN ('3')) THEN ('45 - After Sales Kundendienst eigene Lackiererei') WHEN (T6."type" IN (43,93)) THEN ('45 - After Sales Kundendienst eigene Lackiererei') ELSE null END as "Kostenstelle",
-	   (left((left((((T1."vehicle_number"))),5)) + ' - ' + T5."description",100)) as "Fahrzeug",
-	   (left((ucase(T5."description")),3)) as "Model",
-	   T5."description" as "Description_Models",
-	   T4."description" as "Fabrikat",
-	   CASE WHEN (T1."invoice_type" = 6) THEN ('GWL') WHEN ((T1."invoice_type" = 4) or (T3."customer_number" = 100001)) THEN ('intern') ELSE ('extern') END as "Umsatzart",
-	   T2."Serviceberater" as "Serviceberater",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   (left((((T1."invoice_type"))),1)) + '_' + (left((((T1."invoice_number"))),9)) as "Invoice_Type_Invoice_Number",
-	   T1."vehicle_number" as "Vehicle Number",
-	   T1."is_canceled" as "Is Canceled",
-	   T1."service_date" as "Service Date",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."subsidiary" as "Subsidiary",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."invoice_type" as "Invoice Type"
-from "ims"."ben_AW_Order_Number" T9,
-	(((((((("dbo"."invoices" T1 left outer join "dbo"."customers_suppliers" T3 on T3."customer_number" = T1."paying_customer") left outer join "dbo"."vehicles" T10 on T1."vehicle_number" = T10."internal_number") left outer join "dbo"."makes" T4 on T10."make_number" = T4."make_number") left outer join "dbo"."models" T5 on (T10."make_number" = T5."make_number") and (T10."model_code" = T5."model_code")) left outer join "dbo"."labours" T7 on (T7."invoice_number" = T1."invoice_number") and (T7."invoice_type" = T1."invoice_type")) left outer join "dbo"."charge_type_descriptions" T6 on T6."type" = T7."charge_type") left outer join "dbo"."employees" T8 on T1."creating_employee" = T8."employee_number") left outer join "ims"."Serviceberater_Rechnung" T2 on T1."invtype_invnr" = T2."invtype_invnr")
-where (T7."order_number" = T9."order_number")
-and ((((T1."invoice_type" BETWEEN 2 AND 6) and (T1."is_canceled" <> 1)) and (T1."invoice_date" >= convert(date, '2017-01-01'))) and ((((convert(float, T7."time_units"))) <> 0) and (((convert(float, T7."time_units"))) IS NOT NULL)))
-) D1
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+SELECT "Invoice Type",
+	"Invoice Number",
+	"Subsidiary",
+	"Invoice Date",
+	"Service Date",
+	"Is Canceled",
+	"Vehicle Number",
+	"Invoice_Type_Invoice_Number",
+	'1' AS "Hauptbetrieb",
+	"Standort",
+	"Serviceberater",
+	"Umsatzart",
+	"Fabrikat",
+	"Fabrikat" AS "Description_Makes",
+	"Description_Models",
+	"Model",
+	"Fahrzeug",
+	"Kostenstelle",
+	"Marke",
+	'Service' AS "Auftragsart",
+	"Gesch�ftsart",
+	"Kunde",
+	"Time Units",
+	"Time Units_Zahl",
+	10 AS "AW/Std.",
+	"verk. Std._",
+	"Order Number",
+	"Order Number_Rg_Ausg",
+	"Kundenart",
+	"Summe Duration Minutes",
+	"Order Number_ben_AW",
+	1 AS "Anzahl_Datens�tze_1",
+	COUNT("Order Number_ben_AW") OVER (PARTITION BY "Order Number") AS "Anzahl_Datens�tze_2",
+	(c38) / (COUNT("Order Number_ben_AW") OVER (PARTITION BY "Order Number")) AS "ben. Std."
+FROM (
+	SELECT (
+			CASE 
+				WHEN (T2."Serviceberater" IS NOT NULL)
+					THEN (
+							(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+								CASE 
+									WHEN (T3."first_name" IS NULL)
+										THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+									ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+									END
+								)
+							)
+				ELSE (
+						(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+							CASE 
+								WHEN (T3."first_name" IS NULL)
+									THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+								ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+								END
+							)
+						)
+				END
+			) AS "Order Number",
+		T9."Summe_Duration_Minutes" / 60 AS c38,
+		T9."order_number" AS "Order Number_ben_AW",
+		T9."Summe_Duration_Minutes" AS "Summe Duration Minutes",
+		(left((((T8."employee_number"))), 4)) + ' - ' + T8."name" AS "Kundenart",
+		CASE 
+			WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."invoice_date")))) <= 4)
+				THEN (
+						(
+							CASE 
+								WHEN (T2."Serviceberater" IS NOT NULL)
+									THEN (
+											(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+												CASE 
+													WHEN (T3."first_name" IS NULL)
+														THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+													ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+													END
+												)
+											)
+								ELSE (
+										(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+											CASE 
+												WHEN (T3."first_name" IS NULL)
+													THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+												ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+												END
+											)
+										)
+								END
+							)
+						)
+			ELSE NULL
+			END AS "Order Number_Rg_Ausg",
+		((convert(FLOAT, T7."time_units"))) / 10 AS "verk. Std._",
+		(convert(FLOAT, T7."time_units")) AS "Time Units_Zahl",
+		T7."time_units" AS "Time Units",
+		CASE 
+			WHEN (T3."first_name" IS NULL)
+				THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+			ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+			END AS "Kunde",
+		T3."zip_code" AS "Gesch�ftsart",
+		CASE 
+			WHEN (T4."description" = 'Ford')
+				THEN ('1')
+			WHEN (T4."description" = 'Nissan')
+				THEN ('3')
+			WHEN (T4."description" = 'Suzuki')
+				THEN ('2')
+			ELSE ('9')
+			END AS "Marke",
+		CASE 
+			WHEN ((left((((T6."type"))), 1)) IN ('1', '6'))
+				THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+			WHEN (T6."type" IN (40, 41, 44, 45, 46, 47, 48, 49, 88, 89, 50, 90))
+				THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+			WHEN ((left((((T6."type"))), 1)) IN ('2'))
+				THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+			WHEN (T6."type" IN (42, 92))
+				THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+			WHEN ((left((((T6."type"))), 1)) IN ('3'))
+				THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+			WHEN (T6."type" IN (43, 93))
+				THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+			ELSE NULL
+			END AS "Kostenstelle",
+		(left((left((((T1."vehicle_number"))), 5)) + ' - ' + T5."description", 100)) AS "Fahrzeug",
+		(left((ucase(T5."description")), 3)) AS "Model",
+		T5."description" AS "Description_Models",
+		T4."description" AS "Fabrikat",
+		CASE 
+			WHEN (T1."invoice_type" = 6)
+				THEN ('GWL')
+			WHEN (
+					(T1."invoice_type" = 4)
+					OR (T3."customer_number" = 100001)
+					)
+				THEN ('intern')
+			ELSE ('extern')
+			END AS "Umsatzart",
+		T2."Serviceberater" AS "Serviceberater",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		(left((((T1."invoice_type"))), 1)) + '_' + (left((((T1."invoice_number"))), 9)) AS "Invoice_Type_Invoice_Number",
+		T1."vehicle_number" AS "Vehicle Number",
+		T1."is_canceled" AS "Is Canceled",
+		T1."service_date" AS "Service Date",
+		T1."invoice_date" AS "Invoice Date",
+		T1."subsidiary" AS "Subsidiary",
+		T1."invoice_number" AS "Invoice Number",
+		T1."invoice_type" AS "Invoice Type"
+	FROM "ims"."ben_AW_Order_Number" T9,
+		(
+			(
+				(
+					(
+						(
+							(
+								(
+									(
+										"dbo"."invoices" T1 LEFT JOIN "dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."paying_customer"
+										) LEFT JOIN "dbo"."vehicles" T10 ON T1."vehicle_number" = T10."internal_number"
+									) LEFT JOIN "dbo"."makes" T4 ON T10."make_number" = T4."make_number"
+								) LEFT JOIN "dbo"."models" T5 ON (T10."make_number" = T5."make_number")
+								AND (T10."model_code" = T5."model_code")
+							) LEFT JOIN "dbo"."labours" T7 ON (T7."invoice_number" = T1."invoice_number")
+							AND (T7."invoice_type" = T1."invoice_type")
+						) LEFT JOIN "dbo"."charge_type_descriptions" T6 ON T6."type" = T7."charge_type"
+					) LEFT JOIN "dbo"."employees" T8 ON T1."creating_employee" = T8."employee_number"
+				) LEFT JOIN "ims"."Serviceberater_Rechnung" T2 ON T1."invtype_invnr" = T2."invtype_invnr"
+			)
+	WHERE (T7."order_number" = T9."order_number")
+		AND (
+			(
+				(
+					(T1."invoice_type" BETWEEN 2 AND 6)
+					AND (T1."is_canceled" <> 1)
+					)
+				AND (T1."invoice_date" >= convert(DATE, '2017-01-01'))
+				)
+			AND (
+				(((convert(FLOAT, T7."time_units"))) <> 0)
+				AND (((convert(FLOAT, T7."time_units"))) IS NOT NULL)
+				)
+			)
+	) D1
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc

+ 418 - 255
System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_neu.sql

@@ -1,255 +1,418 @@
-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",
-	   '1' as "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",
-	   '0' + (((T3."branch_number"))) as "Standort",
-	   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 outer join "dbo"."invoice_types" T2 on T2."type" = T1."invoice_type") left outer join "dbo"."journal_accountings" T3 on T3."invoice_number" = T1."invtype_invnr") left outer join "dbo"."customers_suppliers" T4 on T4."customer_number" = T1."paying_customer") left outer join "dbo"."employees" T5 on T1."creating_employee" = T5."employee_number") left outer 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 outer join "dbo"."vehicles" T7 on T1."vehicle_number" = T7."internal_number") left outer join "dbo"."makes" T8 on T7."make_number" = T8."make_number") left outer join "dbo"."models" T9 on (T7."make_number" = T9."make_number") and (T7."model_code" = T9."model_code")) left outer 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
+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",
+	'1' AS "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",
+		'0' + (((T3."branch_number"))) AS "Standort",
+		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

+ 148 - 32
System/LOCOSOFT/IQD/Serv_Teile/Aftersales_Rechnungen_verk_AW_final.sql

@@ -1,32 +1,148 @@
-select T1."invoice_type" as "Invoice Type",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."subsidiary" as "Subsidiary",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."service_date" as "Service Date",
-	   T1."is_canceled" as "Is Canceled",
-	   T1."vehicle_number" as "Vehicle Number",
-	   (left((((T1."invoice_type"))),1)) + '_' + (left((((T1."invoice_number"))),9)) as "Invoice_Type_Invoice_Number",
-	   '1' as "Hauptbetrieb",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   T2."Serviceberater" as "Serviceberater",
-	   CASE WHEN (T1."invoice_type" = 6) THEN ('GWL') WHEN ((T1."invoice_type" = 4) or (T3."customer_number" = 100001)) THEN ('intern') ELSE ('extern') END as "Umsatzart",
-	   T4."description" as "Fabrikat",
-	   T4."description" as "Description_Makes",
-	   T5."description" as "Description_Models",
-	   (left((ucase(T5."description")),3)) as "Model",
-	   (left((left((((T1."vehicle_number"))),5)) + ' - ' + T5."description",100)) as "Fahrzeug",
-	   CASE WHEN ((left((((T6."type"))),1)) IN ('1','6')) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN (T6."type" IN (40,41,44,45,46,47,48,49,88,89,50,90)) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN ((left((((T6."type"))),1)) IN ('2')) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN (T6."type" IN (42,92)) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN ((left((((T6."type"))),1)) IN ('3')) THEN ('45 - After Sales Kundendienst eigene Lackiererei') WHEN (T6."type" IN (43,93)) THEN ('45 - After Sales Kundendienst eigene Lackiererei') ELSE null END as "Kostenstelle",
-	   CASE WHEN (T4."description" = 'Ford') THEN ('1') WHEN (T4."description" = 'Nissan') THEN ('3') WHEN (T4."description" = 'Suzuki') THEN ('2') ELSE ('9') END as "Marke",
-	   'Service' as "Auftragsart",
-	   T3."zip_code" as "Geschäftsart",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END as "Kunde",
-	   T7."time_units" as "Time Units",
-	   (convert(float, T7."time_units")) as "Time Units_Zahl",
-	   10 as "AW/Std.",
-	   ((convert(float, T7."time_units"))) / 10 as "verk. Std.",
-	   CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END as "Order Number",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."invoice_date")))) <= 4) THEN ((CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END)) ELSE null END as "Order Number_Rg_Ausg",
-	   (left((((T8."employee_number"))),4)) + ' - ' + T8."name" as "Kundenart"
-from (((((((("dbo"."invoices" T1 left outer join "dbo"."customers_suppliers" T3 on T3."customer_number" = T1."paying_customer") left outer join "dbo"."vehicles" T9 on T1."vehicle_number" = T9."internal_number") left outer join "dbo"."makes" T4 on T9."make_number" = T4."make_number") left outer join "dbo"."models" T5 on (T9."make_number" = T5."make_number") and (T9."model_code" = T5."model_code")) left outer join "dbo"."labours" T7 on (T7."invoice_number" = T1."invoice_number") and (T7."invoice_type" = T1."invoice_type")) left outer join "dbo"."charge_type_descriptions" T6 on T6."type" = T7."charge_type") left outer join "dbo"."employees" T8 on T1."creating_employee" = T8."employee_number") left outer join "ims"."Serviceberater_Rechnung" T2 on T1."invtype_invnr" = T2."invtype_invnr")
-where (((((T1."invoice_type" BETWEEN 2 AND 6) and (T1."is_canceled" <> 1)) and (T1."invoice_date" >= convert(date, '2017-01-01'))) and ((((convert(float, T7."time_units"))) <> 0) and (((convert(float, T7."time_units"))) IS NOT NULL))) and (T7."mechanic_no" IS NOT NULL))
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+SELECT T1."invoice_type" AS "Invoice Type",
+	T1."invoice_number" AS "Invoice Number",
+	T1."subsidiary" AS "Subsidiary",
+	T1."invoice_date" AS "Invoice Date",
+	T1."service_date" AS "Service Date",
+	T1."is_canceled" AS "Is Canceled",
+	T1."vehicle_number" AS "Vehicle Number",
+	(left((((T1."invoice_type"))), 1)) + '_' + (left((((T1."invoice_number"))), 9)) AS "Invoice_Type_Invoice_Number",
+	'1' AS "Hauptbetrieb",
+	'0' + (((T1."subsidiary"))) AS "Standort",
+	T2."Serviceberater" AS "Serviceberater",
+	CASE 
+		WHEN (T1."invoice_type" = 6)
+			THEN ('GWL')
+		WHEN (
+				(T1."invoice_type" = 4)
+				OR (T3."customer_number" = 100001)
+				)
+			THEN ('intern')
+		ELSE ('extern')
+		END AS "Umsatzart",
+	T4."description" AS "Fabrikat",
+	T4."description" AS "Description_Makes",
+	T5."description" AS "Description_Models",
+	(left((ucase(T5."description")), 3)) AS "Model",
+	(left((left((((T1."vehicle_number"))), 5)) + ' - ' + T5."description", 100)) AS "Fahrzeug",
+	CASE 
+		WHEN ((left((((T6."type"))), 1)) IN ('1', '6'))
+			THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+		WHEN (T6."type" IN (40, 41, 44, 45, 46, 47, 48, 49, 88, 89, 50, 90))
+			THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+		WHEN ((left((((T6."type"))), 1)) IN ('2'))
+			THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+		WHEN (T6."type" IN (42, 92))
+			THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+		WHEN ((left((((T6."type"))), 1)) IN ('3'))
+			THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+		WHEN (T6."type" IN (43, 93))
+			THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+		ELSE NULL
+		END AS "Kostenstelle",
+	CASE 
+		WHEN (T4."description" = 'Ford')
+			THEN ('1')
+		WHEN (T4."description" = 'Nissan')
+			THEN ('3')
+		WHEN (T4."description" = 'Suzuki')
+			THEN ('2')
+		ELSE ('9')
+		END AS "Marke",
+	'Service' AS "Auftragsart",
+	T3."zip_code" AS "Gesch�ftsart",
+	CASE 
+		WHEN (T3."first_name" IS NULL)
+			THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+		ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+		END AS "Kunde",
+	T7."time_units" AS "Time Units",
+	(convert(FLOAT, T7."time_units")) AS "Time Units_Zahl",
+	10 AS "AW/Std.",
+	((convert(FLOAT, T7."time_units"))) / 10 AS "verk. Std.",
+	CASE 
+		WHEN (T2."Serviceberater" IS NOT NULL)
+			THEN (
+					(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+						CASE 
+							WHEN (T3."first_name" IS NULL)
+								THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+							ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+							END
+						)
+					)
+		ELSE (
+				(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+					CASE 
+						WHEN (T3."first_name" IS NULL)
+							THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+						ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+						END
+					)
+				)
+		END AS "Order Number",
+	CASE 
+		WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."invoice_date")))) <= 4)
+			THEN (
+					(
+						CASE 
+							WHEN (T2."Serviceberater" IS NOT NULL)
+								THEN (
+										(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+											CASE 
+												WHEN (T3."first_name" IS NULL)
+													THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+												ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+												END
+											)
+										)
+							ELSE (
+									(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+										CASE 
+											WHEN (T3."first_name" IS NULL)
+												THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+											ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+											END
+										)
+									)
+							END
+						)
+					)
+		ELSE NULL
+		END AS "Order Number_Rg_Ausg",
+	(left((((T8."employee_number"))), 4)) + ' - ' + T8."name" AS "Kundenart"
+FROM (
+	(
+		(
+			(
+				(
+					(
+						(
+							(
+								"dbo"."invoices" T1 LEFT JOIN "dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."paying_customer"
+								) LEFT JOIN "dbo"."vehicles" T9 ON T1."vehicle_number" = T9."internal_number"
+							) LEFT JOIN "dbo"."makes" T4 ON T9."make_number" = T4."make_number"
+						) LEFT JOIN "dbo"."models" T5 ON (T9."make_number" = T5."make_number")
+						AND (T9."model_code" = T5."model_code")
+					) LEFT JOIN "dbo"."labours" T7 ON (T7."invoice_number" = T1."invoice_number")
+					AND (T7."invoice_type" = T1."invoice_type")
+				) LEFT JOIN "dbo"."charge_type_descriptions" T6 ON T6."type" = T7."charge_type"
+			) LEFT JOIN "dbo"."employees" T8 ON T1."creating_employee" = T8."employee_number"
+		) LEFT JOIN "ims"."Serviceberater_Rechnung" T2 ON T1."invtype_invnr" = T2."invtype_invnr"
+	)
+WHERE (
+		(
+			(
+				(
+					(T1."invoice_type" BETWEEN 2 AND 6)
+					AND (T1."is_canceled" <> 1)
+					)
+				AND (T1."invoice_date" >= convert(DATE, '2017-01-01'))
+				)
+			AND (
+				(((convert(FLOAT, T7."time_units"))) <> 0)
+				AND (((convert(FLOAT, T7."time_units"))) IS NOT NULL)
+				)
+			)
+		AND (T7."mechanic_no" IS NOT NULL)
+		)
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc

+ 13 - 7
System/LOCOSOFT/IQD/Serv_Teile/Imvoice_No_Order_No.sql

@@ -1,7 +1,13 @@
-select distinct T1."order_number" as "Order Number",
-	   T1."invoice_number" as "Invoice Number_ohne_type",
-	   T2."invoice_type" as "Invoice Type",
-	   (left((((T2."invoice_type"))),1)) + (left((((T1."invoice_number"))),6)) as "Invoice Number"
-from ("dbo"."invoices" T2 left outer join "dbo"."labours" T1 on (T1."invoice_number" = T2."invoice_number") and (T1."invoice_type" = T2."invoice_type"))
-where ((((left((((T2."invoice_type"))),1)) + (left((((T1."invoice_number"))),6))) <> '') and (T2."invoice_date" >= convert(date, '2019-01-01')))
--- order by "Order Number" asc
+SELECT DISTINCT T1."order_number" AS "Order Number",
+	T1."invoice_number" AS "Invoice Number_ohne_type",
+	T2."invoice_type" AS "Invoice Type",
+	(left((((T2."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 6)) AS "Invoice Number"
+FROM (
+	"dbo"."invoices" T2 LEFT JOIN "dbo"."labours" T1 ON (T1."invoice_number" = T2."invoice_number")
+		AND (T1."invoice_type" = T2."invoice_type")
+	)
+WHERE (
+		(((left((((T2."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 6))) <> '')
+		AND (T2."invoice_date" >= convert(DATE, '2019-01-01'))
+		)
+	-- order by "Order Number" asc

+ 18 - 11
System/LOCOSOFT/IQD/Serv_Teile/Serviceberater_Rechnung.sql

@@ -1,11 +1,18 @@
-select distinct T1."invoice_type" as "Invoice Type",
-	   T1."invoice_number" as "Invoice Number",
-	   T2."order_taking_employee_no" as "Order Taking Employee No",
-	   T2."order_delivery_employee_no" as "Order Delivery Employee No",
-	   T1."invtype_invnr" as "Invoice_Type_Invoice_Number",
-	   T3."employee_number" as "Employee Number_Employees",
-	   T3."name" as "Name_Employees",
-	   (left((((T3."employee_number"))),4)) + ' - ' + T3."name" as "Serviceberater"
-from (("LOCOSOFT"."dbo"."order_positions" T2 left outer join "dbo"."invoices" T1 on T2."order_number" = T1."order_number") left outer join "LOCOSOFT"."dbo"."employees" T3 on T2."order_taking_employee_no" = T3."employee_number")
-where ((T1."invoice_type" BETWEEN 2 AND 6) and (T2."order_date" >= convert(datetime, '2019-01-01 00:00:00.000')))
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+SELECT DISTINCT T1."invoice_type" AS "Invoice Type",
+	T1."invoice_number" AS "Invoice Number",
+	T2."order_taking_employee_no" AS "Order Taking Employee No",
+	T2."order_delivery_employee_no" AS "Order Delivery Employee No",
+	T1."invtype_invnr" AS "Invoice_Type_Invoice_Number",
+	T3."employee_number" AS "Employee Number_Employees",
+	T3."name" AS "Name_Employees",
+	(left((((T3."employee_number"))), 4)) + ' - ' + T3."name" AS "Serviceberater"
+FROM (
+	(
+		"LOCOSOFT"."dbo"."order_positions" T2 LEFT JOIN "dbo"."invoices" T1 ON T2."order_number" = T1."order_number"
+		) LEFT JOIN "LOCOSOFT"."dbo"."employees" T3 ON T2."order_taking_employee_no" = T3."employee_number"
+	)
+WHERE (
+		(T1."invoice_type" BETWEEN 2 AND 6)
+		AND (T2."order_date" >= convert(DATETIME, '2019-01-01 00:00:00.000'))
+		)
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc

+ 6 - 6
System/LOCOSOFT/IQD/Serv_Teile/ben_AW_Order_Number.sql

@@ -1,6 +1,6 @@
-select T1."order_number" as "Order Number",
-	   SUM(T1."duration_minutes") as "Summe_Duration_Minutes"
-from "dbo"."times" T1
-where (T1."order_number" <> 0)
-group by (T1."order_number")
--- order by "Order Number" asc
+SELECT T1."order_number" AS "Order Number",
+	SUM(T1."duration_minutes") AS "Summe_Duration_Minutes"
+FROM "dbo"."times" T1
+WHERE (T1."order_number" <> 0)
+GROUP BY (T1."order_number")
+	-- order by "Order Number" asc

+ 142 - 93
System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_DG_Anz_Tage.sql

@@ -1,93 +1,142 @@
-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",
-	   "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",
-	   1 / (COUNT("Subsidiary") OVER (partition by "Order Number_ori")) as "DG",
-	   "Anzahl Tage_1",
-	   ("Anzahl Tage_1") / (COUNT("Subsidiary") OVER (partition by "Order Number_ori")) as "Anzahl Tage"
-from 
-(select T1."order_number" as "Order Number_ori",
-	   (-1 * datediff(day, (getdate()), T1."order_date")) as "Anzahl Tage_1",
-	   (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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."labour_types" T3 on T3."code" = T2."labour_type") left outer join "dbo"."employees" T4 on T1."order_taking_employee_no" = T4."employee_number") left outer join "dbo"."customers_suppliers" T5 on T1."order_customer" = T5."customer_number") left outer join "dbo"."vehicles" T6 on T1."vehicle_number" = T6."internal_number") left outer join "dbo"."makes" T7 on T6."make_number" = T7."make_number") left outer join "dbo"."models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code"))
-where (((T2."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2020-01-01 00:00:00.000'))) and (T2."labour_type" <> 'F'))
--- order by "Order Number_ori" asc
-) D1
+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",
+	"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",
+	1 / (COUNT("Subsidiary") OVER (PARTITION BY "Order Number_ori")) AS "DG",
+	"Anzahl Tage_1",
+	("Anzahl Tage_1") / (COUNT("Subsidiary") OVER (PARTITION BY "Order Number_ori")) AS "Anzahl Tage"
+FROM (
+	SELECT T1."order_number" AS "Order Number_ori",
+		(- 1 * datediff(day, (getdate()), T1."order_date")) AS "Anzahl Tage_1",
+		(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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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")
+		)
+	WHERE (
+			(
+				(T2."invoice_number" IS NULL)
+				AND (T1."order_date" >= convert(DATETIME, '2020-01-01 00:00:00.000'))
+				)
+			AND (T2."labour_type" <> 'F')
+			)
+		-- order by "Order Number_ori" asc
+	) D1

+ 150 - 100
System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Teile.sql

@@ -1,100 +1,150 @@
-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",
-	   "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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."parts" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."vehicles" T5 on T1."vehicle_number" = T5."internal_number") left outer join "dbo"."makes" T6 on T5."make_number" = T6."make_number") left outer 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
+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",
+	"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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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

+ 158 - 104
System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Ums_ben_AW.sql

@@ -1,104 +1,158 @@
-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",
-	   "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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."labour_types" T3 on T3."code" = T2."labour_type") left outer join "dbo"."employees" T4 on T1."order_taking_employee_no" = T4."employee_number") left outer join "dbo"."customers_suppliers" T5 on T1."order_customer" = T5."customer_number") left outer join "dbo"."vehicles" T6 on T1."vehicle_number" = T6."internal_number") left outer join "dbo"."makes" T7 on T6."make_number" = T7."make_number") left outer join "dbo"."models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code")) left outer 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
+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",
+	"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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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

+ 74 - 18
System/LOCOSOFT/IQD/Serv_Teile/offene_Auftraege_Ums_ben_AW_fuer_Monteure.sql

@@ -1,18 +1,74 @@
-select distinct "Order Number_ori",
-	   "Order Number",
-	   "verk. Std. aus o.A."
-from 
-(select T1."order_number" as c4,
-	   SUM((convert(float, T5."time_units")) / 10) as "verk. Std. aus o.A."
-from (((("dbo"."order_positions" T1 left outer join "dbo"."vehicles" T2 on T1."vehicle_number" = T2."internal_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."labours" T5 on T1."order_number" = T5."order_number")
-where ((((T5."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2017-01-01 00:00:00.000'))) and (T5."net_price_in_order" IS NOT NULL)) and (T5."labour_type" <> 'F'))
-group by T1."order_number"
-) D2,
-	
-(select distinct T1."order_number" as "Order Number_ori",
-	   CASE WHEN (T2."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"))) + ' - ' + T2."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"
-from (((("dbo"."order_positions" T1 left outer join "dbo"."vehicles" T2 on T1."vehicle_number" = T2."internal_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."labours" T5 on T1."order_number" = T5."order_number")
-where ((((T5."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2017-01-01 00:00:00.000'))) and (T5."net_price_in_order" IS NOT NULL)) and (T5."labour_type" <> 'F'))
-) D1
-where (("Order Number_ori" = c4) or (("Order Number_ori" IS NULL) and (c4 IS NULL)))
--- order by "Order Number_ori" asc
+SELECT DISTINCT "Order Number_ori",
+	"Order Number",
+	"verk. Std. aus o.A."
+FROM (
+	SELECT T1."order_number" AS c4,
+		SUM((convert(FLOAT, T5."time_units")) / 10) AS "verk. Std. aus o.A."
+	FROM (
+		(
+			(
+				(
+					"dbo"."order_positions" T1 LEFT JOIN "dbo"."vehicles" T2 ON T1."vehicle_number" = T2."internal_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"."labours" T5 ON T1."order_number" = T5."order_number"
+		)
+	WHERE (
+			(
+				(
+					(T5."invoice_number" IS NULL)
+					AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+					)
+				AND (T5."net_price_in_order" IS NOT NULL)
+				)
+			AND (T5."labour_type" <> 'F')
+			)
+	GROUP BY T1."order_number"
+	) D2,
+	(
+		SELECT DISTINCT T1."order_number" AS "Order Number_ori",
+			CASE 
+				WHEN (T2."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"))) + ' - ' + T2."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"
+		FROM (
+			(
+				(
+					(
+						"dbo"."order_positions" T1 LEFT JOIN "dbo"."vehicles" T2 ON T1."vehicle_number" = T2."internal_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"."labours" T5 ON T1."order_number" = T5."order_number"
+			)
+		WHERE (
+				(
+					(
+						(T5."invoice_number" IS NULL)
+						AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+						)
+					AND (T5."net_price_in_order" IS NOT NULL)
+					)
+				AND (T5."labour_type" <> 'F')
+				)
+		) D1
+WHERE (
+		("Order Number_ori" = c4)
+		OR (
+			("Order Number_ori" IS NULL)
+			AND (c4 IS NULL)
+			)
+		)
+	-- order by "Order Number_ori" asc

+ 125 - 117
System/LOCOSOFT/IQD/Zeit/Zeit_Abwesenheit_neu.sql

@@ -1,117 +1,125 @@
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Absence_Cal" as "Employee Number_Absence_Cal",
-	   "Date_Absence_Cal" as "Date_Absence_Cal",
-	   "Type_Absence_Cal" as "Type_Absence_Cal",
-	   "Is Payed_Absence_Cal" as "Is Payed_Absence_Cal",
-	   "Day Contingent_Absence_Cal" as "Day Contingent_Absence_Cal",
-	   "Reason_Absence_Cal" as "Reason_Absence_Cal",
-	   "Booking Flag_Absence_Cal" as "Booking Flag_Absence_Cal",
-	   "Id_Absence_Reas" as "Id_Absence_Reas",
-	   "Description_Absence_Reas" as "Description_Absence_Reas",
-	   "Is Annual Vacation_Absence_Reas" as "Is Annual Vacation_Absence_Reas",
-	   "Type_Absence_Types" as "Type_Absence_Types",
-	   "Description_Absence_Types" as "Description_Absence_Types",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Dummy_Arbeitszeit_Tag" as "Dummy_Arbeitszeit_Tag",
-	   "Abwesenheit" as "Abwesenheit",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std." as "Ges. Std.",
-	   "Datum" as "Datum",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date_Worktimes",
-	   "Arbeitszeit_Tag" as "Arbeitszeit_Tag"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Absence_Cal",
-	   "Date_Absence_Cal",
-	   "Type_Absence_Cal",
-	   "Is Payed_Absence_Cal",
-	   "Day Contingent_Absence_Cal",
-	   "Reason_Absence_Cal",
-	   "Booking Flag_Absence_Cal",
-	   "Id_Absence_Reas",
-	   "Description_Absence_Reas",
-	   "Is Annual Vacation_Absence_Reas",
-	   "Type_Absence_Types",
-	   "Description_Absence_Types",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   8 as "Dummy_Arbeitszeit_Tag",
-	   "Abwesenheit",
-	   'Abwesenheit' as "Zeitkategorie",
-	   "Zeitkategorie2",
-	   "Abwesenheit" as "Ges. Std.",
-	   "Date_Absence_Cal" as "Datum",
-	   '' as "Order Number",
-	   "Wochentag_Datum",
-	   "Validity Date_Worktimes",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Work Duration_Worktimes" as "Arbeitszeit_Tag",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c39
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   (datepart(weekday, T2."date")) as "Wochentag_Datum",
-	   T2."date" as "Date_Absence_Cal",
-	   (T2."day_contingent" * T5."work_duration") as "Abwesenheit",
-	   T3."id" + ' - ' + T3."description" as "Zeitkategorie2",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T4."description" as "Description_Absence_Types",
-	   T4."type" as "Type_Absence_Types",
-	   T3."is_annual_vacation" as "Is Annual Vacation_Absence_Reas",
-	   T3."description" as "Description_Absence_Reas",
-	   T3."id" as "Id_Absence_Reas",
-	   T2."booking_flag" as "Booking Flag_Absence_Cal",
-	   T2."reason" as "Reason_Absence_Cal",
-	   T2."day_contingent" as "Day Contingent_Absence_Cal",
-	   T2."is_payed" as "Is Payed_Absence_Cal",
-	   T2."type" as "Type_Absence_Cal",
-	   T2."employee_number" as "Employee Number_Absence_Cal",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	(("dbo"."absence_calendar" T2 left outer join "dbo"."absence_reasons" T3 on T2."reason" = T3."id") left outer join "dbo"."absence_types" T4 on T2."type" = T4."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((datepart(weekday, T2."date"))) = T5."dayofweek") and (((T2."date")) <= (getdate())))
-) D2
-) D1
-where ("Validity Date_Worktimes" = c39)
--- order by "Employee Number_Employees" asc
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Absence_Cal" AS "Employee Number_Absence_Cal",
+	"Date_Absence_Cal" AS "Date_Absence_Cal",
+	"Type_Absence_Cal" AS "Type_Absence_Cal",
+	"Is Payed_Absence_Cal" AS "Is Payed_Absence_Cal",
+	"Day Contingent_Absence_Cal" AS "Day Contingent_Absence_Cal",
+	"Reason_Absence_Cal" AS "Reason_Absence_Cal",
+	"Booking Flag_Absence_Cal" AS "Booking Flag_Absence_Cal",
+	"Id_Absence_Reas" AS "Id_Absence_Reas",
+	"Description_Absence_Reas" AS "Description_Absence_Reas",
+	"Is Annual Vacation_Absence_Reas" AS "Is Annual Vacation_Absence_Reas",
+	"Type_Absence_Types" AS "Type_Absence_Types",
+	"Description_Absence_Types" AS "Description_Absence_Types",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Dummy_Arbeitszeit_Tag" AS "Dummy_Arbeitszeit_Tag",
+	"Abwesenheit" AS "Abwesenheit",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std." AS "Ges. Std.",
+	"Datum" AS "Datum",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date_Worktimes",
+	"Arbeitszeit_Tag" AS "Arbeitszeit_Tag"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Absence_Cal",
+		"Date_Absence_Cal",
+		"Type_Absence_Cal",
+		"Is Payed_Absence_Cal",
+		"Day Contingent_Absence_Cal",
+		"Reason_Absence_Cal",
+		"Booking Flag_Absence_Cal",
+		"Id_Absence_Reas",
+		"Description_Absence_Reas",
+		"Is Annual Vacation_Absence_Reas",
+		"Type_Absence_Types",
+		"Description_Absence_Types",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		8 AS "Dummy_Arbeitszeit_Tag",
+		"Abwesenheit",
+		'Abwesenheit' AS "Zeitkategorie",
+		"Zeitkategorie2",
+		"Abwesenheit" AS "Ges. Std.",
+		"Date_Absence_Cal" AS "Datum",
+		'' AS "Order Number",
+		"Wochentag_Datum",
+		"Validity Date_Worktimes",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Work Duration_Worktimes" AS "Arbeitszeit_Tag",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c39
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			(datepart(weekday, T2."date")) AS "Wochentag_Datum",
+			T2."date" AS "Date_Absence_Cal",
+			(T2."day_contingent" * T5."work_duration") AS "Abwesenheit",
+			T3."id" + ' - ' + T3."description" AS "Zeitkategorie2",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T4."description" AS "Description_Absence_Types",
+			T4."type" AS "Type_Absence_Types",
+			T3."is_annual_vacation" AS "Is Annual Vacation_Absence_Reas",
+			T3."description" AS "Description_Absence_Reas",
+			T3."id" AS "Id_Absence_Reas",
+			T2."booking_flag" AS "Booking Flag_Absence_Cal",
+			T2."reason" AS "Reason_Absence_Cal",
+			T2."day_contingent" AS "Day Contingent_Absence_Cal",
+			T2."is_payed" AS "Is Payed_Absence_Cal",
+			T2."type" AS "Type_Absence_Cal",
+			T2."employee_number" AS "Employee Number_Absence_Cal",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				(
+					"dbo"."absence_calendar" T2 LEFT JOIN "dbo"."absence_reasons" T3 ON T2."reason" = T3."id"
+					) LEFT JOIN "dbo"."absence_types" T4 ON T2."type" = T4."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(((datepart(weekday, T2."date"))) = T5."dayofweek")
+				AND (((T2."date")) <= (getdate()))
+				)
+		) D2
+	) D1
+WHERE ("Validity Date_Worktimes" = c39)
+	-- order by "Employee Number_Employees" asc

+ 210 - 168
System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_Pausen.sql

@@ -1,168 +1,210 @@
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Times" as "Employee Number_Times",
-	   "Order Number_Times" as "Order Number_Times",
-	   "Start Time_Times" as "Start Time_Times",
-	   "Type_Times" as "Type_Times",
-	   "Order Positions_Times" as "Order Positions_Times",
-	   "End Time_Times" as "End Time_Times",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "Type_Time_Types" as "Type_Time_Types",
-	   "Description_Time_Types" as "Description_Time_Types",
-	   "Datum" as "Datum",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Anwesenheit_" as "Anwesenheit_",
-	   "produktiv_" as "produktiv_",
-	   "unproduktiv_" as "unproduktiv_",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std._" as "Ges. Std._",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Is Flextime_Employees" as "Is Flextime_Employees",
-	   "Break Time Registration_Employees" as "Break Time Registration_Employees",
-	   "Is Latest Record_break_times" as "Is Latest Record_break_times",
-	   "Dayofweek_break_times" as "Dayofweek_break_times",
-	   "Break Start_break_times" as "Break Start_break_times",
-	   "Break End_break_times" as "Break End_break_times",
-	   "Pause" as "Pause",
-	   "Start_Times_Stunde" as "Start_Times_Stunde",
-	   "Start_Times_Minute" as "Start_Times_Minute",
-	   "End_Times_Stunde" as "End_Times_Stunde",
-	   "End_Times_Minute" as "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal" as "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal" as "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal" as "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal" as "End_Times_Dezimal",
-	   "Pausenzeit_Stunde" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date",
-	   "Validity Date_Break_times" as "Validity Date_Break_times",
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as "Max_validity_date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6" as "Pausenzeit_Anw_mehr_als_6",
-	   "Anwesenheit" as "Anwesenheit"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Times",
-	   "Order Number_Times",
-	   "Start Time_Times",
-	   "Type_Times",
-	   "Order Positions_Times",
-	   "End Time_Times",
-	   "Duration Minutes_Times",
-	   "Type_Time_Types",
-	   "Description_Time_Types",
-	   "Start Time_Times" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   "Anwesenheit_",
-	   "produktiv_",
-	   "Anwesenheit_" as "unproduktiv_",
-	   "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Ges. Std._",
-	   "Order Number_Times" as "Order Number",
-	   "Wochentag_Datum",
-	   "Is Flextime_Employees",
-	   "Break Time Registration_Employees",
-	   "Is Latest Record_break_times",
-	   "Dayofweek_break_times",
-	   "Break Start_break_times",
-	   "Break End_break_times",
-	   "Pause",
-	   "Start_Times_Stunde",
-	   "Start_Times_Minute",
-	   "End_Times_Stunde",
-	   "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal",
-	   "Pause" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Validity Date_Worktimes",
-	   "Validity Date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6",
-	   "Pausenzeit_Anw_mehr_als_6" as "Anwesenheit",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c56,
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as c57
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   (CASE WHEN ((CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END) >= 6.1) THEN ((T4."break_end" - T4."break_start") * -1) ELSE (0) END) as "Pausenzeit_Anw_mehr_als_6",
-	   T4."validity_date" as "Validity Date_Break_times",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T4."break_end" - T4."break_start" as "Pause",
-	   ((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) as "End_Times_Dezimal",
-	   ((od_minute(T2."end_time"))) / 60 as "End_Times_Minute_Dezimal",
-	   ((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) as "Start_Times_Dezimal",
-	   ((od_minute(T2."start_time"))) / 60 as "Start_Times_Minute_Dezimal",
-	   (od_minute(T2."end_time")) as "End_Times_Minute",
-	   (od_hour(T2."end_time")) as "End_Times_Stunde",
-	   (od_minute(T2."start_time")) as "Start_Times_Minute",
-	   (od_hour(T2."start_time")) as "Start_Times_Stunde",
-	   T4."break_end" as "Break End_break_times",
-	   T4."break_start" as "Break Start_break_times",
-	   T4."dayofweek" as "Dayofweek_break_times",
-	   T4."is_latest_record" as "Is Latest Record_break_times",
-	   T1."break_time_registration" as "Break Time Registration_Employees",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T2."order_number" as "Order Number_Times",
-	   T2."duration_minutes" / 60 as "Ges. Std._",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END as "Anwesenheit_",
-	   CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END as "produktiv_",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T2."start_time" as "Start Time_Times",
-	   T3."description" as "Description_Time_Types",
-	   T3."type" as "Type_Time_Types",
-	   T2."duration_minutes" as "Duration Minutes_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."order_positions" as "Order Positions_Times",
-	   T2."type" as "Type_Times",
-	   T2."employee_number" as "Employee Number_Times",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_breaktimes" T4,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	("dbo"."times" T2 left outer join "dbo"."time_types" T3 on T2."type" = T3."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T4."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((T2."type" = 1) and (((datepart(weekday, T2."start_time"))) = T4."dayofweek")) and (((datepart(weekday, T2."start_time"))) = T5."dayofweek")) and (T2."start_time" >= convert(datetime, '2017-01-01 00:00:00.000')))
-) D2
-) D1
-where (("Validity Date_Worktimes" = c56) and ("Validity Date_Break_times" = c57))
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Times" AS "Employee Number_Times",
+	"Order Number_Times" AS "Order Number_Times",
+	"Start Time_Times" AS "Start Time_Times",
+	"Type_Times" AS "Type_Times",
+	"Order Positions_Times" AS "Order Positions_Times",
+	"End Time_Times" AS "End Time_Times",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"Type_Time_Types" AS "Type_Time_Types",
+	"Description_Time_Types" AS "Description_Time_Types",
+	"Datum" AS "Datum",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Anwesenheit_" AS "Anwesenheit_",
+	"produktiv_" AS "produktiv_",
+	"unproduktiv_" AS "unproduktiv_",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std._" AS "Ges. Std._",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Is Flextime_Employees" AS "Is Flextime_Employees",
+	"Break Time Registration_Employees" AS "Break Time Registration_Employees",
+	"Is Latest Record_break_times" AS "Is Latest Record_break_times",
+	"Dayofweek_break_times" AS "Dayofweek_break_times",
+	"Break Start_break_times" AS "Break Start_break_times",
+	"Break End_break_times" AS "Break End_break_times",
+	"Pause" AS "Pause",
+	"Start_Times_Stunde" AS "Start_Times_Stunde",
+	"Start_Times_Minute" AS "Start_Times_Minute",
+	"End_Times_Stunde" AS "End_Times_Stunde",
+	"End_Times_Minute" AS "End_Times_Minute",
+	"Start_Times_Minute_Dezimal" AS "Start_Times_Minute_Dezimal",
+	"Start_Times_Dezimal" AS "Start_Times_Dezimal",
+	"End_Times_Minute_Dezimal" AS "End_Times_Minute_Dezimal",
+	"End_Times_Dezimal" AS "End_Times_Dezimal",
+	"Pausenzeit_Stunde" AS "Pausenzeit_Stunde",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date",
+	"Validity Date_Break_times" AS "Validity Date_Break_times",
+	MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS "Max_validity_date_Break_times",
+	"Pausenzeit_Anw_mehr_als_6" AS "Pausenzeit_Anw_mehr_als_6",
+	"Anwesenheit" AS "Anwesenheit"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Times",
+		"Order Number_Times",
+		"Start Time_Times",
+		"Type_Times",
+		"Order Positions_Times",
+		"End Time_Times",
+		"Duration Minutes_Times",
+		"Type_Time_Types",
+		"Description_Time_Types",
+		"Start Time_Times" AS "Datum",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		"Anwesenheit_",
+		"produktiv_",
+		"Anwesenheit_" AS "unproduktiv_",
+		"Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Ges. Std._",
+		"Order Number_Times" AS "Order Number",
+		"Wochentag_Datum",
+		"Is Flextime_Employees",
+		"Break Time Registration_Employees",
+		"Is Latest Record_break_times",
+		"Dayofweek_break_times",
+		"Break Start_break_times",
+		"Break End_break_times",
+		"Pause",
+		"Start_Times_Stunde",
+		"Start_Times_Minute",
+		"End_Times_Stunde",
+		"End_Times_Minute",
+		"Start_Times_Minute_Dezimal",
+		"Start_Times_Dezimal",
+		"End_Times_Minute_Dezimal",
+		"End_Times_Dezimal",
+		"Pause" AS "Pausenzeit_Stunde",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Validity Date_Worktimes",
+		"Validity Date_Break_times",
+		"Pausenzeit_Anw_mehr_als_6",
+		"Pausenzeit_Anw_mehr_als_6" AS "Anwesenheit",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c56,
+		MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS c57
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			(
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (T3."type" = 1)
+										THEN (T2."duration_minutes" / 60)
+									ELSE (0)
+									END
+								) >= 6.1
+							)
+						THEN ((T4."break_end" - T4."break_start") * - 1)
+					ELSE (0)
+					END
+				) AS "Pausenzeit_Anw_mehr_als_6",
+			T4."validity_date" AS "Validity Date_Break_times",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T4."break_end" - T4."break_start" AS "Pause",
+			((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) AS "End_Times_Dezimal",
+			((od_minute(T2."end_time"))) / 60 AS "End_Times_Minute_Dezimal",
+			((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) AS "Start_Times_Dezimal",
+			((od_minute(T2."start_time"))) / 60 AS "Start_Times_Minute_Dezimal",
+			(od_minute(T2."end_time")) AS "End_Times_Minute",
+			(od_hour(T2."end_time")) AS "End_Times_Stunde",
+			(od_minute(T2."start_time")) AS "Start_Times_Minute",
+			(od_hour(T2."start_time")) AS "Start_Times_Stunde",
+			T4."break_end" AS "Break End_break_times",
+			T4."break_start" AS "Break Start_break_times",
+			T4."dayofweek" AS "Dayofweek_break_times",
+			T4."is_latest_record" AS "Is Latest Record_break_times",
+			T1."break_time_registration" AS "Break Time Registration_Employees",
+			T1."is_flextime" AS "Is Flextime_Employees",
+			(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+			T2."order_number" AS "Order Number_Times",
+			T2."duration_minutes" / 60 AS "Ges. Std._",
+			CASE 
+				WHEN (T2."type" = 2)
+					THEN ('produktiv')
+				ELSE ('unproduktiv')
+				END AS "Zeitkategorie",
+			CASE 
+				WHEN (T3."type" = 1)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "Anwesenheit_",
+			CASE 
+				WHEN (T3."type" = 2)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "produktiv_",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T2."start_time" AS "Start Time_Times",
+			T3."description" AS "Description_Time_Types",
+			T3."type" AS "Type_Time_Types",
+			T2."duration_minutes" AS "Duration Minutes_Times",
+			T2."end_time" AS "End Time_Times",
+			T2."order_positions" AS "Order Positions_Times",
+			T2."type" AS "Type_Times",
+			T2."employee_number" AS "Employee Number_Times",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_breaktimes" T4,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T3 ON T2."type" = T3."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T4."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(
+					(
+						(T2."type" = 1)
+						AND (((datepart(weekday, T2."start_time"))) = T4."dayofweek")
+						)
+					AND (((datepart(weekday, T2."start_time"))) = T5."dayofweek")
+					)
+				AND (T2."start_time" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+				)
+		) D2
+	) D1
+WHERE (
+		("Validity Date_Worktimes" = c56)
+		AND ("Validity Date_Break_times" = c57)
+		)
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc

+ 214 - 171
System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_Pausen_produktiv.sql

@@ -1,171 +1,214 @@
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Times" as "Employee Number_Times",
-	   "Order Number_Times" as "Order Number_Times",
-	   "Start Time_Times" as "Start Time_Times",
-	   "Type_Times" as "Type_Times",
-	   "Order Positions_Times" as "Order Positions_Times",
-	   "End Time_Times" as "End Time_Times",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "Type_Time_Types" as "Type_Time_Types",
-	   "Description_Time_Types" as "Description_Time_Types",
-	   "Datum" as "Datum",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Anwesenheit_" as "Anwesenheit_",
-	   "produktiv_" as "produktiv_",
-	   "unproduktiv_" as "unproduktiv_",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std._" as "Ges. Std._",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Is Flextime_Employees" as "Is Flextime_Employees",
-	   "Break Time Registration_Employees" as "Break Time Registration_Employees",
-	   "Is Latest Record_break_times" as "Is Latest Record_break_times",
-	   "Dayofweek_break_times" as "Dayofweek_break_times",
-	   "Break Start_break_times" as "Break Start_break_times",
-	   "Break End_break_times" as "Break End_break_times",
-	   "Pause" as "Pause",
-	   "Start_Times_Stunde" as "Start_Times_Stunde",
-	   "Start_Times_Minute" as "Start_Times_Minute",
-	   "End_Times_Stunde" as "End_Times_Stunde",
-	   "End_Times_Minute" as "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal" as "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal" as "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal" as "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal" as "End_Times_Dezimal",
-	   "Pausenzeit_Stunde" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date",
-	   "Validity Date_Break_times" as "Validity Date_Break_times",
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as "Max_validity_date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6_" as "Pausenzeit_Anw_mehr_als_6_",
-	   "Anwesenheit" as "Anwesenheit",
-	   "Pausenzeit_während_produktiv" as "Pausenzeit_während_produktiv"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Times",
-	   "Order Number_Times",
-	   "Start Time_Times",
-	   "Type_Times",
-	   "Order Positions_Times",
-	   "End Time_Times",
-	   "Duration Minutes_Times",
-	   "Type_Time_Types",
-	   "Description_Time_Types",
-	   "Start Time_Times" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   "Anwesenheit_",
-	   "produktiv_",
-	   "Anwesenheit_" as "unproduktiv_",
-	   "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Ges. Std._",
-	   "Order Number_Times" as "Order Number",
-	   "Wochentag_Datum",
-	   "Is Flextime_Employees",
-	   "Break Time Registration_Employees",
-	   "Is Latest Record_break_times",
-	   "Dayofweek_break_times",
-	   "Break Start_break_times",
-	   "Break End_break_times",
-	   "Pause",
-	   "Start_Times_Stunde",
-	   "Start_Times_Minute",
-	   "End_Times_Stunde",
-	   "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal",
-	   "Pause" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Validity Date_Worktimes",
-	   "Validity Date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6_",
-	   "Pausenzeit_Anw_mehr_als_6_" as "Anwesenheit",
-	   "Pausenzeit_während_produktiv",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c57,
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as c58
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   CASE WHEN (((CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END) > 0) and (T4."break_start" BETWEEN (((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60)) AND (((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60)))) THEN ((((T4."break_end" - T4."break_start") * -1))) ELSE (0) END as "Pausenzeit_während_produktiv",
-	   (((T4."break_end" - T4."break_start") * -1)) as "Pausenzeit_Anw_mehr_als_6_",
-	   T4."validity_date" as "Validity Date_Break_times",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T4."break_end" - T4."break_start" as "Pause",
-	   ((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) as "End_Times_Dezimal",
-	   ((od_minute(T2."end_time"))) / 60 as "End_Times_Minute_Dezimal",
-	   ((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) as "Start_Times_Dezimal",
-	   ((od_minute(T2."start_time"))) / 60 as "Start_Times_Minute_Dezimal",
-	   (od_minute(T2."end_time")) as "End_Times_Minute",
-	   (od_hour(T2."end_time")) as "End_Times_Stunde",
-	   (od_minute(T2."start_time")) as "Start_Times_Minute",
-	   (od_hour(T2."start_time")) as "Start_Times_Stunde",
-	   T4."break_end" as "Break End_break_times",
-	   T4."break_start" as "Break Start_break_times",
-	   T4."dayofweek" as "Dayofweek_break_times",
-	   T4."is_latest_record" as "Is Latest Record_break_times",
-	   T1."break_time_registration" as "Break Time Registration_Employees",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T2."order_number" as "Order Number_Times",
-	   T2."duration_minutes" / 60 as "Ges. Std._",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END as "Anwesenheit_",
-	   CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END as "produktiv_",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T2."start_time" as "Start Time_Times",
-	   T3."description" as "Description_Time_Types",
-	   T3."type" as "Type_Time_Types",
-	   T2."duration_minutes" as "Duration Minutes_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."order_positions" as "Order Positions_Times",
-	   T2."type" as "Type_Times",
-	   T2."employee_number" as "Employee Number_Times",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_breaktimes" T4,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	("dbo"."times" T2 left outer join "dbo"."time_types" T3 on T2."type" = T3."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T4."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((T2."type" = 2) and (((datepart(weekday, T2."start_time"))) = T4."dayofweek")) and (((datepart(weekday, T2."start_time"))) = T5."dayofweek")) and (T2."start_time" >= convert(datetime, '2017-01-01 00:00:00.000')))
-) D2
-) D1
-where (("Validity Date_Worktimes" = c57) and ("Validity Date_Break_times" = c58))
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Times" AS "Employee Number_Times",
+	"Order Number_Times" AS "Order Number_Times",
+	"Start Time_Times" AS "Start Time_Times",
+	"Type_Times" AS "Type_Times",
+	"Order Positions_Times" AS "Order Positions_Times",
+	"End Time_Times" AS "End Time_Times",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"Type_Time_Types" AS "Type_Time_Types",
+	"Description_Time_Types" AS "Description_Time_Types",
+	"Datum" AS "Datum",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Anwesenheit_" AS "Anwesenheit_",
+	"produktiv_" AS "produktiv_",
+	"unproduktiv_" AS "unproduktiv_",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std._" AS "Ges. Std._",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Is Flextime_Employees" AS "Is Flextime_Employees",
+	"Break Time Registration_Employees" AS "Break Time Registration_Employees",
+	"Is Latest Record_break_times" AS "Is Latest Record_break_times",
+	"Dayofweek_break_times" AS "Dayofweek_break_times",
+	"Break Start_break_times" AS "Break Start_break_times",
+	"Break End_break_times" AS "Break End_break_times",
+	"Pause" AS "Pause",
+	"Start_Times_Stunde" AS "Start_Times_Stunde",
+	"Start_Times_Minute" AS "Start_Times_Minute",
+	"End_Times_Stunde" AS "End_Times_Stunde",
+	"End_Times_Minute" AS "End_Times_Minute",
+	"Start_Times_Minute_Dezimal" AS "Start_Times_Minute_Dezimal",
+	"Start_Times_Dezimal" AS "Start_Times_Dezimal",
+	"End_Times_Minute_Dezimal" AS "End_Times_Minute_Dezimal",
+	"End_Times_Dezimal" AS "End_Times_Dezimal",
+	"Pausenzeit_Stunde" AS "Pausenzeit_Stunde",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date",
+	"Validity Date_Break_times" AS "Validity Date_Break_times",
+	MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS "Max_validity_date_Break_times",
+	"Pausenzeit_Anw_mehr_als_6_" AS "Pausenzeit_Anw_mehr_als_6_",
+	"Anwesenheit" AS "Anwesenheit",
+	"Pausenzeit_w�hrend_produktiv" AS "Pausenzeit_w�hrend_produktiv"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Times",
+		"Order Number_Times",
+		"Start Time_Times",
+		"Type_Times",
+		"Order Positions_Times",
+		"End Time_Times",
+		"Duration Minutes_Times",
+		"Type_Time_Types",
+		"Description_Time_Types",
+		"Start Time_Times" AS "Datum",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		"Anwesenheit_",
+		"produktiv_",
+		"Anwesenheit_" AS "unproduktiv_",
+		"Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Ges. Std._",
+		"Order Number_Times" AS "Order Number",
+		"Wochentag_Datum",
+		"Is Flextime_Employees",
+		"Break Time Registration_Employees",
+		"Is Latest Record_break_times",
+		"Dayofweek_break_times",
+		"Break Start_break_times",
+		"Break End_break_times",
+		"Pause",
+		"Start_Times_Stunde",
+		"Start_Times_Minute",
+		"End_Times_Stunde",
+		"End_Times_Minute",
+		"Start_Times_Minute_Dezimal",
+		"Start_Times_Dezimal",
+		"End_Times_Minute_Dezimal",
+		"End_Times_Dezimal",
+		"Pause" AS "Pausenzeit_Stunde",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Validity Date_Worktimes",
+		"Validity Date_Break_times",
+		"Pausenzeit_Anw_mehr_als_6_",
+		"Pausenzeit_Anw_mehr_als_6_" AS "Anwesenheit",
+		"Pausenzeit_w�hrend_produktiv",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c57,
+		MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS c58
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			CASE 
+				WHEN (
+						(
+							(
+								CASE 
+									WHEN (T3."type" = 2)
+										THEN (T2."duration_minutes" / 60)
+									ELSE (0)
+									END
+								) > 0
+							)
+						AND (T4."break_start" BETWEEN (((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60)) AND (((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60)))
+						)
+					THEN ((((T4."break_end" - T4."break_start") * - 1)))
+				ELSE (0)
+				END AS "Pausenzeit_w�hrend_produktiv",
+			(((T4."break_end" - T4."break_start") * - 1)) AS "Pausenzeit_Anw_mehr_als_6_",
+			T4."validity_date" AS "Validity Date_Break_times",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T4."break_end" - T4."break_start" AS "Pause",
+			((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) AS "End_Times_Dezimal",
+			((od_minute(T2."end_time"))) / 60 AS "End_Times_Minute_Dezimal",
+			((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) AS "Start_Times_Dezimal",
+			((od_minute(T2."start_time"))) / 60 AS "Start_Times_Minute_Dezimal",
+			(od_minute(T2."end_time")) AS "End_Times_Minute",
+			(od_hour(T2."end_time")) AS "End_Times_Stunde",
+			(od_minute(T2."start_time")) AS "Start_Times_Minute",
+			(od_hour(T2."start_time")) AS "Start_Times_Stunde",
+			T4."break_end" AS "Break End_break_times",
+			T4."break_start" AS "Break Start_break_times",
+			T4."dayofweek" AS "Dayofweek_break_times",
+			T4."is_latest_record" AS "Is Latest Record_break_times",
+			T1."break_time_registration" AS "Break Time Registration_Employees",
+			T1."is_flextime" AS "Is Flextime_Employees",
+			(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+			T2."order_number" AS "Order Number_Times",
+			T2."duration_minutes" / 60 AS "Ges. Std._",
+			CASE 
+				WHEN (T2."type" = 2)
+					THEN ('produktiv')
+				ELSE ('unproduktiv')
+				END AS "Zeitkategorie",
+			CASE 
+				WHEN (T3."type" = 1)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "Anwesenheit_",
+			CASE 
+				WHEN (T3."type" = 2)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "produktiv_",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T2."start_time" AS "Start Time_Times",
+			T3."description" AS "Description_Time_Types",
+			T3."type" AS "Type_Time_Types",
+			T2."duration_minutes" AS "Duration Minutes_Times",
+			T2."end_time" AS "End Time_Times",
+			T2."order_positions" AS "Order Positions_Times",
+			T2."type" AS "Type_Times",
+			T2."employee_number" AS "Employee Number_Times",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_breaktimes" T4,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T3 ON T2."type" = T3."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T4."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(
+					(
+						(T2."type" = 2)
+						AND (((datepart(weekday, T2."start_time"))) = T4."dayofweek")
+						)
+					AND (((datepart(weekday, T2."start_time"))) = T5."dayofweek")
+					)
+				AND (T2."start_time" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+				)
+		) D2
+	) D1
+WHERE (
+		("Validity Date_Worktimes" = c57)
+		AND ("Validity Date_Break_times" = c58)
+		)
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc

+ 55 - 35
System/LOCOSOFT/IQD/Zeit/Zeit_Stempelungen_neu.sql

@@ -1,35 +1,55 @@
-select T1."employee_number" as "Employee Number_Employees",
-	   T1."name" as "Name_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T2."employee_number" as "Employee Number_Times",
-	   T2."order_number" as "Order Number_Times",
-	   T2."start_time" as "Start Time_Times",
-	   T2."type" as "Type_Times",
-	   T3."Order Number" as "Order Positions_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."exact_duration_seconds" / 60 as "Duration Minutes_Times",
-	   T4."type" as "Type_Time_Types",
-	   T4."description" as "Description_Time_Types",
-	   T2."start_time" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   CASE WHEN (T4."type" = 1) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "Anwesenheit",
-	   CASE WHEN (T4."type" = 2) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "produktiv",
-	   CASE WHEN (T4."type" = 1) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "unproduktiv",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   (T2."exact_duration_seconds" / 60) / 60 as "Ges. Std.",
-	   T2."order_number" as "Order Number",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   T1."break_time_registration" as "Break Time Registration_Employees"
-from "dbo"."employees" T1,
-	(("dbo"."times" T2 left outer join "dbo"."time_types" T4 on T2."type" = T4."type") left outer join "ims"."offene_Auftraege_Ums_ben_AW_fuer_Monteure" T3 on T3."order_number" = T2."order_number")
-where (T1."employee_number" = T2."employee_number")
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+SELECT T1."employee_number" AS "Employee Number_Employees",
+	T1."name" AS "Name_Employees",
+	T1."initials" AS "Initials_Employees",
+	T1."mechanic_number" AS "Mechanic Number_Employees",
+	T1."salesman_number" AS "Salesman Number_Employees",
+	T1."employment_date" AS "Employment Date_Employees",
+	T1."termination_date" AS "Termination Date_Employees",
+	T1."leave_date" AS "Leave Date_Employees",
+	T2."employee_number" AS "Employee Number_Times",
+	T2."order_number" AS "Order Number_Times",
+	T2."start_time" AS "Start Time_Times",
+	T2."type" AS "Type_Times",
+	T3."Order Number" AS "Order Positions_Times",
+	T2."end_time" AS "End Time_Times",
+	T2."exact_duration_seconds" / 60 AS "Duration Minutes_Times",
+	T4."type" AS "Type_Time_Types",
+	T4."description" AS "Description_Time_Types",
+	T2."start_time" AS "Datum",
+	'1' AS "Hauptbetrieb",
+	'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+	(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+	CASE 
+		WHEN (T4."type" = 1)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "Anwesenheit",
+	CASE 
+		WHEN (T4."type" = 2)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "produktiv",
+	CASE 
+		WHEN (T4."type" = 1)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "unproduktiv",
+	CASE 
+		WHEN (T2."type" = 2)
+			THEN ('produktiv')
+		ELSE ('unproduktiv')
+		END AS "Zeitkategorie",
+	'' AS "Zeitkategorie2",
+	(T2."exact_duration_seconds" / 60) / 60 AS "Ges. Std.",
+	T2."order_number" AS "Order Number",
+	(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+	T1."is_flextime" AS "Is Flextime_Employees",
+	T1."break_time_registration" AS "Break Time Registration_Employees"
+FROM "dbo"."employees" T1,
+	(
+		(
+			"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T4 ON T2."type" = T4."type"
+			) LEFT JOIN "ims"."offene_Auftraege_Ums_ben_AW_fuer_Monteure" T3 ON T3."order_number" = T2."order_number"
+		)
+WHERE (T1."employee_number" = T2."employee_number")
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc

+ 91 - 75
System/LOCOSOFT/IQD/Zeit/Zeit_ben_Std_LG.sql

@@ -1,75 +1,91 @@
-select distinct "Order Number" as "Order Number",
-	   "Labour Type" as "Labour Type",
-	   "Code_Labour_Types" as "Code_Labour_Types",
-	   "Description_Labour_Types" as "Description_Labour_Types",
-	   "Order Number_Labours" as "Order Number_Labours",
-	   "Subsidiary_Labours" as "Subsidiary_Labours",
-	   "Invoice Type_Labours" as "Invoice Type_Labours",
-	   "Mechanic No_Labours" as "Mechanic No_Labours",
-	   "AW/Std." as "AW/Std.",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Employee Number_Employee" as "Employee Number_Employee",
-	   "Name_Employee" as "Name_Employee",
-	   "Monteur" as "Monteur",
-	   "Invoice Date_Invoices" as "Invoice Date_Invoices",
-	   "Datum" as "Datum",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "ben. Std." as "ben. Std.",
-	   "Invoice Number" as "Invoice Number",
-	   "Is Canceled" as "Is Canceled",
-	   "Cancelation Number" as "Cancelation Number",
-	   "Cancelation Date" as "Cancelation Date",
-	   "Start Time" as "Start Time",
-	   "End Time" as "End Time",
-	   MIN("Invoice Date_Invoices") OVER (partition by "Order Number","Mechanic No_Labours") as "Min_Invoice_Date"
-from 
-(select "Order Number",
-	   '' as "Labour Type",
-	   '' as "Code_Labour_Types",
-	   '' as "Description_Labour_Types",
-	   "Order Number_Labours",
-	   "Subsidiary_Labours",
-	   0 as "Invoice Type_Labours",
-	   "Mechanic No_Labours",
-	   10 as "AW/Std.",
-	   'ben. Std.' as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Employee Number_Employee",
-	   "Name_Employee",
-	   "Monteur",
-	   "Invoice Date_Invoices",
-	   "Invoice Date_Invoices" as "Datum",
-	   "Duration Minutes_Times",
-	   "ben. Std.",
-	   0 as "Invoice Number",
-	   "Is Canceled",
-	   "Cancelation Number",
-	   "Cancelation Date",
-	   "Start Time",
-	   "End Time",
-	   MIN("Invoice Date_Invoices") OVER (partition by "Order Number","Mechanic No_Labours") as c26
-from 
-(select T2."mechanic_no" as "Mechanic No_Labours",
-	   T1."order_number" as "Order Number",
-	   T5."end_time" as "End Time",
-	   T5."start_time" as "Start Time",
-	   T4."cancelation_date" as "Cancelation Date",
-	   T4."cancelation_number" as "Cancelation Number",
-	   T4."is_canceled" as "Is Canceled",
-	   (T5."exact_duration_seconds" / 60) / 60 as "ben. Std.",
-	   T5."exact_duration_seconds" / 60 as "Duration Minutes_Times",
-	   T4."invoice_date" as "Invoice Date_Invoices",
-	   (rtrim((((T3."employee_number"))))) + ' - ' + T3."name" as "Monteur",
-	   T3."name" as "Name_Employee",
-	   T3."employee_number" as "Employee Number_Employee",
-	   '0' + (rtrim((((T3."subsidiary"))))) as "Subsidiary_Labours",
-	   T2."order_number" as "Order Number_Labours"
-from "LOCOSOFT"."dbo"."times" T5,
-	("dbo"."invoices" T4 left outer join (("dbo"."order_positions" T1 left outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."employees" T3 on T2."mechanic_no" = T3."employee_number") on (T2."invoice_number" = T4."invoice_number") and (T2."invoice_type" = T4."invoice_type"))
-where ((T2."order_number" = T5."order_number") and (T2."mechanic_no" = T5."employee_number"))
-and (((convert(float, T2."time_units"))) <> 0)
-) D2
-) D1
-where (c26 = "Invoice Date_Invoices")
--- order by "Order Number" asc,"Mechanic No_Labours" asc,"Start Time" asc
+SELECT DISTINCT "Order Number" AS "Order Number",
+	"Labour Type" AS "Labour Type",
+	"Code_Labour_Types" AS "Code_Labour_Types",
+	"Description_Labour_Types" AS "Description_Labour_Types",
+	"Order Number_Labours" AS "Order Number_Labours",
+	"Subsidiary_Labours" AS "Subsidiary_Labours",
+	"Invoice Type_Labours" AS "Invoice Type_Labours",
+	"Mechanic No_Labours" AS "Mechanic No_Labours",
+	"AW/Std." AS "AW/Std.",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Employee Number_Employee" AS "Employee Number_Employee",
+	"Name_Employee" AS "Name_Employee",
+	"Monteur" AS "Monteur",
+	"Invoice Date_Invoices" AS "Invoice Date_Invoices",
+	"Datum" AS "Datum",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"ben. Std." AS "ben. Std.",
+	"Invoice Number" AS "Invoice Number",
+	"Is Canceled" AS "Is Canceled",
+	"Cancelation Number" AS "Cancelation Number",
+	"Cancelation Date" AS "Cancelation Date",
+	"Start Time" AS "Start Time",
+	"End Time" AS "End Time",
+	MIN("Invoice Date_Invoices") OVER (
+		PARTITION BY "Order Number",
+		"Mechanic No_Labours"
+		) AS "Min_Invoice_Date"
+FROM (
+	SELECT "Order Number",
+		'' AS "Labour Type",
+		'' AS "Code_Labour_Types",
+		'' AS "Description_Labour_Types",
+		"Order Number_Labours",
+		"Subsidiary_Labours",
+		0 AS "Invoice Type_Labours",
+		"Mechanic No_Labours",
+		10 AS "AW/Std.",
+		'ben. Std.' AS "Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Employee Number_Employee",
+		"Name_Employee",
+		"Monteur",
+		"Invoice Date_Invoices",
+		"Invoice Date_Invoices" AS "Datum",
+		"Duration Minutes_Times",
+		"ben. Std.",
+		0 AS "Invoice Number",
+		"Is Canceled",
+		"Cancelation Number",
+		"Cancelation Date",
+		"Start Time",
+		"End Time",
+		MIN("Invoice Date_Invoices") OVER (
+			PARTITION BY "Order Number",
+			"Mechanic No_Labours"
+			) AS c26
+	FROM (
+		SELECT T2."mechanic_no" AS "Mechanic No_Labours",
+			T1."order_number" AS "Order Number",
+			T5."end_time" AS "End Time",
+			T5."start_time" AS "Start Time",
+			T4."cancelation_date" AS "Cancelation Date",
+			T4."cancelation_number" AS "Cancelation Number",
+			T4."is_canceled" AS "Is Canceled",
+			(T5."exact_duration_seconds" / 60) / 60 AS "ben. Std.",
+			T5."exact_duration_seconds" / 60 AS "Duration Minutes_Times",
+			T4."invoice_date" AS "Invoice Date_Invoices",
+			(rtrim((((T3."employee_number"))))) + ' - ' + T3."name" AS "Monteur",
+			T3."name" AS "Name_Employee",
+			T3."employee_number" AS "Employee Number_Employee",
+			'0' + (rtrim((((T3."subsidiary"))))) AS "Subsidiary_Labours",
+			T2."order_number" AS "Order Number_Labours"
+		FROM "LOCOSOFT"."dbo"."times" T5,
+			(
+				"dbo"."invoices" T4 LEFT JOIN (
+					(
+						"dbo"."order_positions" T1 LEFT JOIN "dbo"."labours" T2 ON T1."order_number" = T2."order_number"
+						) LEFT JOIN "dbo"."employees" T3 ON T2."mechanic_no" = T3."employee_number"
+					) ON (T2."invoice_number" = T4."invoice_number")
+					AND (T2."invoice_type" = T4."invoice_type")
+				)
+		WHERE (
+				(T2."order_number" = T5."order_number")
+				AND (T2."mechanic_no" = T5."employee_number")
+				)
+			AND (((convert(FLOAT, T2."time_units"))) <> 0)
+		) D2
+	) D1
+WHERE (c26 = "Invoice Date_Invoices")
+	-- order by "Order Number" asc,"Mechanic No_Labours" asc,"Start Time" asc

+ 45 - 36
System/LOCOSOFT/IQD/Zeit/Zeit_verk_Std.sql

@@ -1,36 +1,45 @@
-select T1."order_number" as "Order Number",
-	   T1."order_position" as "Order Position",
-	   T1."labour_type" as "Labour Type",
-	   T2."code" as "Code_Labour_Types",
-	   T2."description" as "Description_Labour_Types",
-	   T3."order_number" as "Order Number_Labours",
-	   T3."order_position" as "Order Position_Labours",
-	   T3."order_position_line" as "Order Position Line_Labours",
-	   '0' + (rtrim((((T4."subsidiary"))))) as "Subsidiary_Labours",
-	   T3."is_invoiced" as "Is Invoiced_Labours",
-	   T3."invoice_type" as "Invoice Type_Labours",
-	   T3."invoice_number" as "Invoice Number_Labours",
-	   T3."employee_no" as "Employee No_Labours",
-	   T3."mechanic_no" as "Mechanic No_Labours",
-	   T3."labour_operation_id" as "Labour Operation Id_Labours",
-	   T3."is_nominal" as "Is Nominal_Labours",
-	   T3."net_price_in_order" as "Net Price In Order_Labours",
-	   T3."rebate_percent" as "Rebate Percent_Labours",
-	   T3."goodwill_percent" as "Goodwill Percent_Labours",
-	   T3."charge_type" as "Charge Type_Labours",
-	   T3."text_line" as "Text Line_Labours",
-	   10 as "AW/Std.",
-	   ((convert(float, T3."time_units"))) / 10 as "verk. Std.",
-	   'verk. Std.' as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   T4."employee_number" as "Employee Number_Employee",
-	   T4."name" as "Name_Employee",
-	   (rtrim((((T4."employee_number"))))) + ' - ' + T4."name" as "Monteur",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."invoice_date" as "Datum",
-	   T3."time_units" as "Time Units_Labours",
-	   (convert(float, T3."time_units")) as "Time_Units_Zahl"
-from ("dbo"."invoices" T5 left outer join ((("dbo"."order_positions" T1 left outer join "dbo"."labour_types" T2 on T1."labour_type" = T2."code") left outer join "dbo"."labours" T3 on T1."order_number" = T3."order_number") left outer join "dbo"."employees" T4 on T3."mechanic_no" = T4."employee_number") on (T3."invoice_number" = T5."invoice_number") and (T3."invoice_type" = T5."invoice_type"))
-where (((convert(float, T3."time_units"))) <> 0)
--- order by "Order Number" asc,"Order Position" asc
+SELECT T1."order_number" AS "Order Number",
+	T1."order_position" AS "Order Position",
+	T1."labour_type" AS "Labour Type",
+	T2."code" AS "Code_Labour_Types",
+	T2."description" AS "Description_Labour_Types",
+	T3."order_number" AS "Order Number_Labours",
+	T3."order_position" AS "Order Position_Labours",
+	T3."order_position_line" AS "Order Position Line_Labours",
+	'0' + (rtrim((((T4."subsidiary"))))) AS "Subsidiary_Labours",
+	T3."is_invoiced" AS "Is Invoiced_Labours",
+	T3."invoice_type" AS "Invoice Type_Labours",
+	T3."invoice_number" AS "Invoice Number_Labours",
+	T3."employee_no" AS "Employee No_Labours",
+	T3."mechanic_no" AS "Mechanic No_Labours",
+	T3."labour_operation_id" AS "Labour Operation Id_Labours",
+	T3."is_nominal" AS "Is Nominal_Labours",
+	T3."net_price_in_order" AS "Net Price In Order_Labours",
+	T3."rebate_percent" AS "Rebate Percent_Labours",
+	T3."goodwill_percent" AS "Goodwill Percent_Labours",
+	T3."charge_type" AS "Charge Type_Labours",
+	T3."text_line" AS "Text Line_Labours",
+	10 AS "AW/Std.",
+	((convert(FLOAT, T3."time_units"))) / 10 AS "verk. Std.",
+	'verk. Std.' AS "Zeitkategorie",
+	'' AS "Zeitkategorie2",
+	T4."employee_number" AS "Employee Number_Employee",
+	T4."name" AS "Name_Employee",
+	(rtrim((((T4."employee_number"))))) + ' - ' + T4."name" AS "Monteur",
+	T5."invoice_number" AS "Invoice Number_Invoices",
+	T5."invoice_date" AS "Invoice Date_Invoices",
+	T5."invoice_date" AS "Datum",
+	T3."time_units" AS "Time Units_Labours",
+	(convert(FLOAT, T3."time_units")) AS "Time_Units_Zahl"
+FROM (
+	"dbo"."invoices" T5 LEFT JOIN (
+		(
+			(
+				"dbo"."order_positions" T1 LEFT JOIN "dbo"."labour_types" T2 ON T1."labour_type" = T2."code"
+				) LEFT JOIN "dbo"."labours" T3 ON T1."order_number" = T3."order_number"
+			) LEFT JOIN "dbo"."employees" T4 ON T3."mechanic_no" = T4."employee_number"
+		) ON (T3."invoice_number" = T5."invoice_number")
+		AND (T3."invoice_type" = T5."invoice_type")
+	)
+WHERE (((convert(FLOAT, T3."time_units"))) <> 0)
+	-- order by "Order Number" asc,"Order Position" asc

+ 27 - 14
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Imvoice_No_Order_No.sql

@@ -1,20 +1,33 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW ims.imvoice_no_order_no
+AS
+SELECT DISTINCT T1."order_number" AS "Order Number",
+	T1."invoice_number" AS "Invoice Number_ohne_type",
+	T2."invoice_type" AS "Invoice Type",
+	(left((((T2."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 6)) AS "Invoice Number"
+FROM (
+	"dbo"."invoices" T2 LEFT JOIN "dbo"."labours" T1 ON (T1."invoice_number" = T2."invoice_number")
+		AND (T1."invoice_type" = T2."invoice_type")
+	)
+WHERE (
+		(((left((((T2."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 6))) <> '')
+		AND (T2."invoice_date" >= convert(DATE, '2019-01-01'))
+		)
+	-- order by "Order Number" asc
 GO
-CREATE VIEW ims.imvoice_no_order_no AS
-
-select distinct T1."order_number" as "Order Number",
-	   T1."invoice_number" as "Invoice Number_ohne_type",
-	   T2."invoice_type" as "Invoice Type",
-	   (left((((T2."invoice_type"))),1)) + (left((((T1."invoice_number"))),6)) as "Invoice Number"
-from ("dbo"."invoices" T2 left outer join "dbo"."labours" T1 on (T1."invoice_number" = T2."invoice_number") and (T1."invoice_type" = T2."invoice_type"))
-where ((((left((((T2."invoice_type"))),1)) + (left((((T1."invoice_number"))),6))) <> '') and (T2."invoice_date" >= convert(date, '2019-01-01')))
--- order by "Order Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 121 - 114
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Kontenrahmen.sql

@@ -1,121 +1,128 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW ims.kontenrahmen AS
 
-select T1."KONTO_NR" as "Konto Nr",
-	   T1."KONTO_BEZEICHNUNG" as "Konto Bezeichnung",
-	   T1."KONTO_ART" as "Konto Art_ori",
-	   T1."KOSTENSTELLE" as "Kostenstelle",
-	   T1."STK" as "Stk",
-	   T1."KONTO_1" as "Konto 1",
-	   T1."KONTO_2" as "Konto 2",
-	   T1."KONTO_3" as "Konto 3",
-	   T1."KONTO_4" as "Konto 4",
-	   T1."KONTO_5" as "Konto 5",
-	   T1."EBENE1" as "Ebene1",
-	   T1."EBENE2" as "Ebene2",
-	   T1."EBENE3" as "Ebene3",
-	   T1."EBENE4" as "Ebene4",
-	   T1."EBENE5" as "Ebene5",
-	   T1."EBENE6" as "Ebene6",
-	   T1."EBENE7" as "Ebene7",
-	   T1."EBENE8" as "Ebene8",
-	   T1."EBENE9" as "Ebene9",
-	   T1."EBENE10" as "Ebene10",
-	   T1."EBENE11" as "Ebene11",
-	   T1."EBENE12" as "Ebene12",
-	   T1."EBENE13" as "Ebene13",
-	   T1."EBENE14" as "Ebene14",
-	   T1."EBENE15" as "Ebene15",
-	   T1."EBENE16" as "Ebene16",
-	   T1."EBENE17" as "Ebene17",
-	   T1."EBENE18" as "Ebene18",
-	   T1."EBENE19" as "Ebene19",
-	   T1."EBENE20" as "Ebene20",
-	   T1."EBENE21" as "Ebene21",
-	   T1."EBENE22" as "Ebene22",
-	   T1."EBENE23" as "Ebene23",
-	   T1."EBENE24" as "Ebene24",
-	   T1."EBENE25" as "Ebene25",
-	   T1."EBENE26" as "Ebene26",
-	   T1."EBENE27" as "Ebene27",
-	   T1."EBENE28" as "Ebene28",
-	   T1."EBENE29" as "Ebene29",
-	   T1."EBENE30" as "Ebene30",
-	   T1."EBENE31" as "Ebene31",
-	   T1."EBENE32" as "Ebene32",
-	   T1."EBENE33" as "Ebene33",
-	   T1."EBENE34" as "Ebene34",
-	   T1."EBENE35" as "Ebene35",
-	   T1."EBENE36" as "Ebene36",
-	   T1."EBENE37" as "Ebene37",
-	   T1."EBENE38" as "Ebene38",
-	   T1."EBENE39" as "Ebene39",
-	   T1."EBENE40" as "Ebene40",
-	   T1."EBENE41" as "Ebene41",
-	   T1."EBENE42" as "Ebene42",
-	   T1."EBENE43" as "Ebene43",
-	   T1."EBENE44" as "Ebene44",
-	   T1."EBENE45" as "Ebene45",
-	   T1."EBENE46" as "Ebene46",
-	   T1."EBENE47" as "Ebene47",
-	   T1."EBENE48" as "Ebene48",
-	   T1."EBENE49" as "Ebene49",
-	   T1."EBENE50" as "Ebene50",
-	   T1."EBENE51" as "Ebene51",
-	   T1."EBENE52" as "Ebene52",
-	   T1."EBENE53" as "Ebene53",
-	   T1."EBENE54" as "Ebene54",
-	   T1."EBENE55" as "Ebene55",
-	   T1."EBENE56" as "Ebene56",
-	   T1."EBENE57" as "Ebene57",
-	   T1."EBENE58" as "Ebene58",
-	   T1."EBENE59" as "Ebene59",
-	   T1."EBENE60" as "Ebene60",
-	   T1."EBENE61" as "Ebene61",
-	   T1."EBENE62" as "Ebene62",
-	   T1."EBENE63" as "Ebene63",
-	   T1."EBENE64" as "Ebene64",
-	   T1."EBENE65" as "Ebene65",
-	   T1."EBENE66" as "Ebene66",
-	   T1."EBENE67" as "Ebene67",
-	   T1."EBENE68" as "Ebene68",
-	   T1."EBENE69" as "Ebene69",
-	   T1."EBENE70" as "Ebene70",
-	   T1."EBENE71" as "Ebene71",
-	   T1."EBENE72" as "Ebene72",
-	   T1."EBENE73" as "Ebene73",
-	   T1."EBENE74" as "Ebene74",
-	   T1."EBENE75" as "Ebene75",
-	   T1."EBENE76" as "Ebene76",
-	   T1."EBENE77" as "Ebene77",
-	   T1."EBENE78" as "Ebene78",
-	   T1."EBENE79" as "Ebene79",
-	   T1."EBENE80" as "Ebene80",
-	   T1."EBENE81" as "Ebene81",
-	   T1."EBENE82" as "Ebene82",
-	   T1."EBENE83" as "Ebene83",
-	   T1."EBENE84" as "Ebene84",
-	   T1."EBENE85" as "Ebene85",
-	   T1."EBENE86" as "Ebene86",
-	   T1."EBENE87" as "Ebene87",
-	   T1."EBENE88" as "Ebene88",
-	   T1."EBENE89" as "Ebene89",
-	   T1."EBENE90" as "Ebene90",
-	   (len(T1."KONTO_NR" + 'Z') - 1) as "Anzahl Stellen_Konto_Nr",
-	   T1."KONTO_ART" as "Konto Art",
-	   T1."KONTO_ART" as "GuV_Bilanz",
-	   T1."KONTO_NR" as "Acct Nr",
-	   T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" as "Konto",
-	   (left(T1."KONTO_NR",1)) as "Susa"
-from "Kontenrahmen.csv" T1
-where (((left(T1."KONTO_NR",1))) <> '9')
+CREATE VIEW ims.kontenrahmen
+AS
+SELECT T1."KONTO_NR" AS "Konto Nr",
+	T1."KONTO_BEZEICHNUNG" AS "Konto Bezeichnung",
+	T1."KONTO_ART" AS "Konto Art_ori",
+	T1."KOSTENSTELLE" AS "Kostenstelle",
+	T1."STK" AS "Stk",
+	T1."KONTO_1" AS "Konto 1",
+	T1."KONTO_2" AS "Konto 2",
+	T1."KONTO_3" AS "Konto 3",
+	T1."KONTO_4" AS "Konto 4",
+	T1."KONTO_5" AS "Konto 5",
+	T1."EBENE1" AS "Ebene1",
+	T1."EBENE2" AS "Ebene2",
+	T1."EBENE3" AS "Ebene3",
+	T1."EBENE4" AS "Ebene4",
+	T1."EBENE5" AS "Ebene5",
+	T1."EBENE6" AS "Ebene6",
+	T1."EBENE7" AS "Ebene7",
+	T1."EBENE8" AS "Ebene8",
+	T1."EBENE9" AS "Ebene9",
+	T1."EBENE10" AS "Ebene10",
+	T1."EBENE11" AS "Ebene11",
+	T1."EBENE12" AS "Ebene12",
+	T1."EBENE13" AS "Ebene13",
+	T1."EBENE14" AS "Ebene14",
+	T1."EBENE15" AS "Ebene15",
+	T1."EBENE16" AS "Ebene16",
+	T1."EBENE17" AS "Ebene17",
+	T1."EBENE18" AS "Ebene18",
+	T1."EBENE19" AS "Ebene19",
+	T1."EBENE20" AS "Ebene20",
+	T1."EBENE21" AS "Ebene21",
+	T1."EBENE22" AS "Ebene22",
+	T1."EBENE23" AS "Ebene23",
+	T1."EBENE24" AS "Ebene24",
+	T1."EBENE25" AS "Ebene25",
+	T1."EBENE26" AS "Ebene26",
+	T1."EBENE27" AS "Ebene27",
+	T1."EBENE28" AS "Ebene28",
+	T1."EBENE29" AS "Ebene29",
+	T1."EBENE30" AS "Ebene30",
+	T1."EBENE31" AS "Ebene31",
+	T1."EBENE32" AS "Ebene32",
+	T1."EBENE33" AS "Ebene33",
+	T1."EBENE34" AS "Ebene34",
+	T1."EBENE35" AS "Ebene35",
+	T1."EBENE36" AS "Ebene36",
+	T1."EBENE37" AS "Ebene37",
+	T1."EBENE38" AS "Ebene38",
+	T1."EBENE39" AS "Ebene39",
+	T1."EBENE40" AS "Ebene40",
+	T1."EBENE41" AS "Ebene41",
+	T1."EBENE42" AS "Ebene42",
+	T1."EBENE43" AS "Ebene43",
+	T1."EBENE44" AS "Ebene44",
+	T1."EBENE45" AS "Ebene45",
+	T1."EBENE46" AS "Ebene46",
+	T1."EBENE47" AS "Ebene47",
+	T1."EBENE48" AS "Ebene48",
+	T1."EBENE49" AS "Ebene49",
+	T1."EBENE50" AS "Ebene50",
+	T1."EBENE51" AS "Ebene51",
+	T1."EBENE52" AS "Ebene52",
+	T1."EBENE53" AS "Ebene53",
+	T1."EBENE54" AS "Ebene54",
+	T1."EBENE55" AS "Ebene55",
+	T1."EBENE56" AS "Ebene56",
+	T1."EBENE57" AS "Ebene57",
+	T1."EBENE58" AS "Ebene58",
+	T1."EBENE59" AS "Ebene59",
+	T1."EBENE60" AS "Ebene60",
+	T1."EBENE61" AS "Ebene61",
+	T1."EBENE62" AS "Ebene62",
+	T1."EBENE63" AS "Ebene63",
+	T1."EBENE64" AS "Ebene64",
+	T1."EBENE65" AS "Ebene65",
+	T1."EBENE66" AS "Ebene66",
+	T1."EBENE67" AS "Ebene67",
+	T1."EBENE68" AS "Ebene68",
+	T1."EBENE69" AS "Ebene69",
+	T1."EBENE70" AS "Ebene70",
+	T1."EBENE71" AS "Ebene71",
+	T1."EBENE72" AS "Ebene72",
+	T1."EBENE73" AS "Ebene73",
+	T1."EBENE74" AS "Ebene74",
+	T1."EBENE75" AS "Ebene75",
+	T1."EBENE76" AS "Ebene76",
+	T1."EBENE77" AS "Ebene77",
+	T1."EBENE78" AS "Ebene78",
+	T1."EBENE79" AS "Ebene79",
+	T1."EBENE80" AS "Ebene80",
+	T1."EBENE81" AS "Ebene81",
+	T1."EBENE82" AS "Ebene82",
+	T1."EBENE83" AS "Ebene83",
+	T1."EBENE84" AS "Ebene84",
+	T1."EBENE85" AS "Ebene85",
+	T1."EBENE86" AS "Ebene86",
+	T1."EBENE87" AS "Ebene87",
+	T1."EBENE88" AS "Ebene88",
+	T1."EBENE89" AS "Ebene89",
+	T1."EBENE90" AS "Ebene90",
+	(len(T1."KONTO_NR" + 'Z') - 1) AS "Anzahl Stellen_Konto_Nr",
+	T1."KONTO_ART" AS "Konto Art",
+	T1."KONTO_ART" AS "GuV_Bilanz",
+	T1."KONTO_NR" AS "Acct Nr",
+	T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" AS "Konto",
+	(left(T1."KONTO_NR", 1)) AS "Susa"
+FROM "Kontenrahmen.csv" T1
+WHERE (((left(T1."KONTO_NR", 1))) <> '9')
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 33 - 20
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.OP_aus_LOC_Belege_8520.sql

@@ -1,26 +1,39 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW ims.op_aus_loc_belege_8520
+AS
+SELECT DISTINCT T1."document_type" AS "Document Type",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."invoice_number" AS "Invoice Number",
+	T1."skr51_branch" AS "Skr51 Branch",
+	'1' AS "Rechtseinheit",
+	'0' + (((T1."skr51_branch"))) AS "Betrieb",
+	'1' AS "Mandant",
+	'1' AS "Hauptbetrieb",
+	('0' + (((T1."skr51_branch")))) AS "Standort"
+FROM "journal_accountings" T1
+WHERE (
+		(
+			(T1."nominal_account_number" IN (8520))
+			AND (T1."document_type" = 'R')
+			)
+		AND (T1."invoice_date" >= convert(DATE, '2016-01-01'))
+		)
+	-- order by "Invoice Number" asc,"Nominal Account Number" asc
 GO
-CREATE VIEW ims.op_aus_loc_belege_8520 AS
-
-select distinct T1."document_type" as "Document Type",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   '1' as "Rechtseinheit",
-	   '0' + (((T1."skr51_branch"))) as "Betrieb",
-	   '1' as "Mandant",
-	   '1' as "Hauptbetrieb",
-	   ('0' + (((T1."skr51_branch")))) as "Standort"
-from "journal_accountings" T1
-where (((T1."nominal_account_number" IN (8520)) and (T1."document_type" = 'R')) and (T1."invoice_date" >= convert(date, '2016-01-01')))
--- order by "Invoice Number" asc,"Nominal Account Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 32 - 18
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.Serviceberater_Rechnung.sql

@@ -1,24 +1,38 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW ims.serviceberater_rechnung
+AS
+SELECT DISTINCT T1."invoice_type" AS "Invoice Type",
+	T1."invoice_number" AS "Invoice Number",
+	T2."order_taking_employee_no" AS "Order Taking Employee No",
+	T2."order_delivery_employee_no" AS "Order Delivery Employee No",
+	T1."invtype_invnr" AS "Invoice_Type_Invoice_Number",
+	T3."employee_number" AS "Employee Number_Employees",
+	T3."name" AS "Name_Employees",
+	(left((((T3."employee_number"))), 4)) + ' - ' + T3."name" AS "Serviceberater"
+FROM (
+	(
+		"LOCOSOFT"."dbo"."order_positions" T2 LEFT JOIN "dbo"."invoices" T1 ON T2."order_number" = T1."order_number"
+		) LEFT JOIN "LOCOSOFT"."dbo"."employees" T3 ON T2."order_taking_employee_no" = T3."employee_number"
+	)
+WHERE (
+		(T1."invoice_type" BETWEEN 2 AND 6)
+		AND (T2."order_date" >= convert(DATETIME, '2019-01-01 00:00:00.000'))
+		)
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
 GO
-CREATE VIEW ims.serviceberater_rechnung AS
-
-select distinct T1."invoice_type" as "Invoice Type",
-	   T1."invoice_number" as "Invoice Number",
-	   T2."order_taking_employee_no" as "Order Taking Employee No",
-	   T2."order_delivery_employee_no" as "Order Delivery Employee No",
-	   T1."invtype_invnr" as "Invoice_Type_Invoice_Number",
-	   T3."employee_number" as "Employee Number_Employees",
-	   T3."name" as "Name_Employees",
-	   (left((((T3."employee_number"))),4)) + ' - ' + T3."name" as "Serviceberater"
-from (("LOCOSOFT"."dbo"."order_positions" T2 left outer join "dbo"."invoices" T1 on T2."order_number" = T1."order_number") left outer join "LOCOSOFT"."dbo"."employees" T3 on T2."order_taking_employee_no" = T3."employee_number")
-where ((T1."invoice_type" BETWEEN 2 AND 6) and (T2."order_date" >= convert(datetime, '2019-01-01 00:00:00.000')))
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 20 - 13
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.ben_AW_Order_Number.sql

@@ -1,19 +1,26 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW ims.ben_aw_order_number
+AS
+SELECT T1."order_number" AS "Order Number",
+	SUM(T1."duration_minutes") AS "Summe_Duration_Minutes"
+FROM "dbo"."times" T1
+WHERE (T1."order_number" <> 0)
+GROUP BY (T1."order_number")
+	-- order by "Order Number" asc
 GO
-CREATE VIEW ims.ben_aw_order_number AS
-
-select T1."order_number" as "Order Number",
-	   SUM(T1."duration_minutes") as "Summe_Duration_Minutes"
-from "dbo"."times" T1
-where (T1."order_number" <> 0)
-group by (T1."order_number")
--- order by "Order Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 88 - 25
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/ims.offene_Auftraege_Ums_ben_AW_fuer_Monteure.sql

@@ -1,31 +1,94 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW ims.offene_auftraege_ums_ben_aw_fuer_monteure
+AS
+SELECT DISTINCT "Order Number_ori",
+	"Order Number",
+	"verk. Std. aus o.A."
+FROM (
+	SELECT T1."order_number" AS c4,
+		SUM((convert(FLOAT, T5."time_units")) / 10) AS "verk. Std. aus o.A."
+	FROM (
+		(
+			(
+				(
+					"dbo"."order_positions" T1 LEFT JOIN "dbo"."vehicles" T2 ON T1."vehicle_number" = T2."internal_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"."labours" T5 ON T1."order_number" = T5."order_number"
+		)
+	WHERE (
+			(
+				(
+					(T5."invoice_number" IS NULL)
+					AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+					)
+				AND (T5."net_price_in_order" IS NOT NULL)
+				)
+			AND (T5."labour_type" <> 'F')
+			)
+	GROUP BY T1."order_number"
+	) D2,
+	(
+		SELECT DISTINCT T1."order_number" AS "Order Number_ori",
+			CASE 
+				WHEN (T2."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"))) + ' - ' + T2."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"
+		FROM (
+			(
+				(
+					(
+						"dbo"."order_positions" T1 LEFT JOIN "dbo"."vehicles" T2 ON T1."vehicle_number" = T2."internal_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"."labours" T5 ON T1."order_number" = T5."order_number"
+			)
+		WHERE (
+				(
+					(
+						(T5."invoice_number" IS NULL)
+						AND (T1."order_date" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+						)
+					AND (T5."net_price_in_order" IS NOT NULL)
+					)
+				AND (T5."labour_type" <> 'F')
+				)
+		) D1
+WHERE (
+		("Order Number_ori" = c4)
+		OR (
+			("Order Number_ori" IS NULL)
+			AND (c4 IS NULL)
+			)
+		)
+	-- order by "Order Number_ori" asc
 GO
-CREATE VIEW ims.offene_auftraege_ums_ben_aw_fuer_monteure AS
-
-select distinct "Order Number_ori",
-	   "Order Number",
-	   "verk. Std. aus o.A."
-from 
-(select T1."order_number" as c4,
-	   SUM((convert(float, T5."time_units")) / 10) as "verk. Std. aus o.A."
-from (((("dbo"."order_positions" T1 left outer join "dbo"."vehicles" T2 on T1."vehicle_number" = T2."internal_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."labours" T5 on T1."order_number" = T5."order_number")
-where ((((T5."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2017-01-01 00:00:00.000'))) and (T5."net_price_in_order" IS NOT NULL)) and (T5."labour_type" <> 'F'))
-group by T1."order_number"
-) D2,
-	
-(select distinct T1."order_number" as "Order Number_ori",
-	   CASE WHEN (T2."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"))) + ' - ' + T2."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"
-from (((("dbo"."order_positions" T1 left outer join "dbo"."vehicles" T2 on T1."vehicle_number" = T2."internal_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."labours" T5 on T1."order_number" = T5."order_number")
-where ((((T5."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2017-01-01 00:00:00.000'))) and (T5."net_price_in_order" IS NOT NULL)) and (T5."labour_type" <> 'F'))
-) D1
-where (("Order Number_ori" = c4) or (("Order Number_ori" IS NULL) and (c4 IS NULL)))
--- order by "Order Number_ori" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 197 - 75
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_ben_aw_final_imr.sql

@@ -1,82 +1,204 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.aftersales_rechnungen_ben_aw_final_imr AS
 
-select "Invoice Type",
-	   "Invoice Number",
-	   "Subsidiary",
-	   "Invoice Date",
-	   "Service Date",
-	   "Is Canceled",
-	   "Vehicle Number",
-	   "Invoice_Type_Invoice_Number",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Serviceberater",
-	   "Umsatzart",
-	   "Fabrikat",
-	   "Fabrikat" as "Description_Makes",
-	   "Description_Models",
-	   "Model",
-	   "Fahrzeug",
-	   "Kostenstelle",
-	   "Marke",
-	   'Service' as "Auftragsart",
-	   "Geschäftsart",
-	   "Kunde",
-	   "Time Units",
-	   "Time Units_Zahl",
-	   10 as "AW/Std.",
-	   "verk. Std._",
-	   "Order Number",
-	   "Order Number_Rg_Ausg",
-	   "Kundenart",
-	   "Summe Duration Minutes",
-	   "Order Number_ben_AW",
-	   1 as "Anzahl_Datensätze_1",
-	   COUNT("Order Number_ben_AW") OVER (partition by "Order Number") as "Anzahl_Datensätze_2",
-	   (c38) / (COUNT("Order Number_ben_AW") OVER (partition by "Order Number")) as "ben. Std."
-from 
-(select (CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END) as "Order Number",
-	   T9."Summe_Duration_Minutes" / 60 as c38,
-	   T9."order_number" as "Order Number_ben_AW",
-	   T9."Summe_Duration_Minutes" as "Summe Duration Minutes",
-	   (left((((T8."employee_number"))),4)) + ' - ' + T8."name" as "Kundenart",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."invoice_date")))) <= 4) THEN ((CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END)) ELSE null END as "Order Number_Rg_Ausg",
-	   ((convert(float, T7."time_units"))) / 10 as "verk. Std._",
-	   (convert(float, T7."time_units")) as "Time Units_Zahl",
-	   T7."time_units" as "Time Units",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END as "Kunde",
-	   T3."zip_code" as "Geschäftsart",
-	   CASE WHEN (T4."description" = 'Ford') THEN ('1') WHEN (T4."description" = 'Nissan') THEN ('3') WHEN (T4."description" = 'Suzuki') THEN ('2') ELSE ('9') END as "Marke",
-	   CASE WHEN ((left((((T6."type"))),1)) IN ('1','6')) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN (T6."type" IN (40,41,44,45,46,47,48,49,88,89,50,90)) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN ((left((((T6."type"))),1)) IN ('2')) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN (T6."type" IN (42,92)) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN ((left((((T6."type"))),1)) IN ('3')) THEN ('45 - After Sales Kundendienst eigene Lackiererei') WHEN (T6."type" IN (43,93)) THEN ('45 - After Sales Kundendienst eigene Lackiererei') ELSE null END as "Kostenstelle",
-	   (left((left((((T1."vehicle_number"))),5)) + ' - ' + T5."description",100)) as "Fahrzeug",
-	   (left((ucase(T5."description")),3)) as "Model",
-	   T5."description" as "Description_Models",
-	   T4."description" as "Fabrikat",
-	   CASE WHEN (T1."invoice_type" = 6) THEN ('GWL') WHEN ((T1."invoice_type" = 4) or (T3."customer_number" = 100001)) THEN ('intern') ELSE ('extern') END as "Umsatzart",
-	   T2."Serviceberater" as "Serviceberater",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   (left((((T1."invoice_type"))),1)) + '_' + (left((((T1."invoice_number"))),9)) as "Invoice_Type_Invoice_Number",
-	   T1."vehicle_number" as "Vehicle Number",
-	   T1."is_canceled" as "Is Canceled",
-	   T1."service_date" as "Service Date",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."subsidiary" as "Subsidiary",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."invoice_type" as "Invoice Type"
-from "ims"."ben_AW_Order_Number" T9,
-	(((((((("dbo"."invoices" T1 left outer join "dbo"."customers_suppliers" T3 on T3."customer_number" = T1."paying_customer") left outer join "dbo"."vehicles" T10 on T1."vehicle_number" = T10."internal_number") left outer join "dbo"."makes" T4 on T10."make_number" = T4."make_number") left outer join "dbo"."models" T5 on (T10."make_number" = T5."make_number") and (T10."model_code" = T5."model_code")) left outer join "dbo"."labours" T7 on (T7."invoice_number" = T1."invoice_number") and (T7."invoice_type" = T1."invoice_type")) left outer join "dbo"."charge_type_descriptions" T6 on T6."type" = T7."charge_type") left outer join "dbo"."employees" T8 on T1."creating_employee" = T8."employee_number") left outer join "ims"."Serviceberater_Rechnung" T2 on T1."invtype_invnr" = T2."invtype_invnr")
-where (T7."order_number" = T9."order_number")
-and ((((T1."invoice_type" BETWEEN 2 AND 6) and (T1."is_canceled" <> 1)) and (T1."invoice_date" >= convert(date, '2017-01-01'))) and ((((convert(float, T7."time_units"))) <> 0) and (((convert(float, T7."time_units"))) IS NOT NULL)))
-) D1
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+CREATE VIEW staging.aftersales_rechnungen_ben_aw_final_imr
+AS
+SELECT "Invoice Type",
+	"Invoice Number",
+	"Subsidiary",
+	"Invoice Date",
+	"Service Date",
+	"Is Canceled",
+	"Vehicle Number",
+	"Invoice_Type_Invoice_Number",
+	'1' AS "Hauptbetrieb",
+	"Standort",
+	"Serviceberater",
+	"Umsatzart",
+	"Fabrikat",
+	"Fabrikat" AS "Description_Makes",
+	"Description_Models",
+	"Model",
+	"Fahrzeug",
+	"Kostenstelle",
+	"Marke",
+	'Service' AS "Auftragsart",
+	"Gesch�ftsart",
+	"Kunde",
+	"Time Units",
+	"Time Units_Zahl",
+	10 AS "AW/Std.",
+	"verk. Std._",
+	"Order Number",
+	"Order Number_Rg_Ausg",
+	"Kundenart",
+	"Summe Duration Minutes",
+	"Order Number_ben_AW",
+	1 AS "Anzahl_Datens�tze_1",
+	COUNT("Order Number_ben_AW") OVER (PARTITION BY "Order Number") AS "Anzahl_Datens�tze_2",
+	(c38) / (COUNT("Order Number_ben_AW") OVER (PARTITION BY "Order Number")) AS "ben. Std."
+FROM (
+	SELECT (
+			CASE 
+				WHEN (T2."Serviceberater" IS NOT NULL)
+					THEN (
+							(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+								CASE 
+									WHEN (T3."first_name" IS NULL)
+										THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+									ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+									END
+								)
+							)
+				ELSE (
+						(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+							CASE 
+								WHEN (T3."first_name" IS NULL)
+									THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+								ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+								END
+							)
+						)
+				END
+			) AS "Order Number",
+		T9."Summe_Duration_Minutes" / 60 AS c38,
+		T9."order_number" AS "Order Number_ben_AW",
+		T9."Summe_Duration_Minutes" AS "Summe Duration Minutes",
+		(left((((T8."employee_number"))), 4)) + ' - ' + T8."name" AS "Kundenart",
+		CASE 
+			WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."invoice_date")))) <= 4)
+				THEN (
+						(
+							CASE 
+								WHEN (T2."Serviceberater" IS NOT NULL)
+									THEN (
+											(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+												CASE 
+													WHEN (T3."first_name" IS NULL)
+														THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+													ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+													END
+												)
+											)
+								ELSE (
+										(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+											CASE 
+												WHEN (T3."first_name" IS NULL)
+													THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+												ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+												END
+											)
+										)
+								END
+							)
+						)
+			ELSE NULL
+			END AS "Order Number_Rg_Ausg",
+		((convert(FLOAT, T7."time_units"))) / 10 AS "verk. Std._",
+		(convert(FLOAT, T7."time_units")) AS "Time Units_Zahl",
+		T7."time_units" AS "Time Units",
+		CASE 
+			WHEN (T3."first_name" IS NULL)
+				THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+			ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+			END AS "Kunde",
+		T3."zip_code" AS "Gesch�ftsart",
+		CASE 
+			WHEN (T4."description" = 'Ford')
+				THEN ('1')
+			WHEN (T4."description" = 'Nissan')
+				THEN ('3')
+			WHEN (T4."description" = 'Suzuki')
+				THEN ('2')
+			ELSE ('9')
+			END AS "Marke",
+		CASE 
+			WHEN ((left((((T6."type"))), 1)) IN ('1', '6'))
+				THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+			WHEN (T6."type" IN (40, 41, 44, 45, 46, 47, 48, 49, 88, 89, 50, 90))
+				THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+			WHEN ((left((((T6."type"))), 1)) IN ('2'))
+				THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+			WHEN (T6."type" IN (42, 92))
+				THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+			WHEN ((left((((T6."type"))), 1)) IN ('3'))
+				THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+			WHEN (T6."type" IN (43, 93))
+				THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+			ELSE NULL
+			END AS "Kostenstelle",
+		(left((left((((T1."vehicle_number"))), 5)) + ' - ' + T5."description", 100)) AS "Fahrzeug",
+		(left((ucase(T5."description")), 3)) AS "Model",
+		T5."description" AS "Description_Models",
+		T4."description" AS "Fabrikat",
+		CASE 
+			WHEN (T1."invoice_type" = 6)
+				THEN ('GWL')
+			WHEN (
+					(T1."invoice_type" = 4)
+					OR (T3."customer_number" = 100001)
+					)
+				THEN ('intern')
+			ELSE ('extern')
+			END AS "Umsatzart",
+		T2."Serviceberater" AS "Serviceberater",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		(left((((T1."invoice_type"))), 1)) + '_' + (left((((T1."invoice_number"))), 9)) AS "Invoice_Type_Invoice_Number",
+		T1."vehicle_number" AS "Vehicle Number",
+		T1."is_canceled" AS "Is Canceled",
+		T1."service_date" AS "Service Date",
+		T1."invoice_date" AS "Invoice Date",
+		T1."subsidiary" AS "Subsidiary",
+		T1."invoice_number" AS "Invoice Number",
+		T1."invoice_type" AS "Invoice Type"
+	FROM "ims"."ben_AW_Order_Number" T9,
+		(
+			(
+				(
+					(
+						(
+							(
+								(
+									(
+										"dbo"."invoices" T1 LEFT JOIN "dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."paying_customer"
+										) LEFT JOIN "dbo"."vehicles" T10 ON T1."vehicle_number" = T10."internal_number"
+									) LEFT JOIN "dbo"."makes" T4 ON T10."make_number" = T4."make_number"
+								) LEFT JOIN "dbo"."models" T5 ON (T10."make_number" = T5."make_number")
+								AND (T10."model_code" = T5."model_code")
+							) LEFT JOIN "dbo"."labours" T7 ON (T7."invoice_number" = T1."invoice_number")
+							AND (T7."invoice_type" = T1."invoice_type")
+						) LEFT JOIN "dbo"."charge_type_descriptions" T6 ON T6."type" = T7."charge_type"
+					) LEFT JOIN "dbo"."employees" T8 ON T1."creating_employee" = T8."employee_number"
+				) LEFT JOIN "ims"."Serviceberater_Rechnung" T2 ON T1."invtype_invnr" = T2."invtype_invnr"
+			)
+	WHERE (T7."order_number" = T9."order_number")
+		AND (
+			(
+				(
+					(T1."invoice_type" BETWEEN 2 AND 6)
+					AND (T1."is_canceled" <> 1)
+					)
+				AND (T1."invoice_date" >= convert(DATE, '2017-01-01'))
+				)
+			AND (
+				(((convert(FLOAT, T7."time_units"))) <> 0)
+				AND (((convert(FLOAT, T7."time_units"))) IS NOT NULL)
+				)
+			)
+	) D1
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 431 - 261
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_neu_imr.sql

@@ -1,268 +1,438 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.aftersales_rechnungen_neu_imr AS
 
-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",
-	   '1' as "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",
-	   '0' + (((T3."branch_number"))) as "Standort",
-	   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 outer join "dbo"."invoice_types" T2 on T2."type" = T1."invoice_type") left outer join "dbo"."journal_accountings" T3 on T3."invoice_number" = T1."invtype_invnr") left outer join "dbo"."customers_suppliers" T4 on T4."customer_number" = T1."paying_customer") left outer join "dbo"."employees" T5 on T1."creating_employee" = T5."employee_number") left outer 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 outer join "dbo"."vehicles" T7 on T1."vehicle_number" = T7."internal_number") left outer join "dbo"."makes" T8 on T7."make_number" = T8."make_number") left outer join "dbo"."models" T9 on (T7."make_number" = T9."make_number") and (T7."model_code" = T9."model_code")) left outer 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
+CREATE VIEW staging.aftersales_rechnungen_neu_imr
+AS
+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",
+	'1' AS "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",
+		'0' + (((T3."branch_number"))) AS "Standort",
+		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
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 162 - 39
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.aftersales_rechnungen_verk_aw_final_imr.sql

@@ -1,45 +1,168 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.aftersales_rechnungen_verk_aw_final_imr
+AS
+SELECT T1."invoice_type" AS "Invoice Type",
+	T1."invoice_number" AS "Invoice Number",
+	T1."subsidiary" AS "Subsidiary",
+	T1."invoice_date" AS "Invoice Date",
+	T1."service_date" AS "Service Date",
+	T1."is_canceled" AS "Is Canceled",
+	T1."vehicle_number" AS "Vehicle Number",
+	(left((((T1."invoice_type"))), 1)) + '_' + (left((((T1."invoice_number"))), 9)) AS "Invoice_Type_Invoice_Number",
+	'1' AS "Hauptbetrieb",
+	'0' + (((T1."subsidiary"))) AS "Standort",
+	T2."Serviceberater" AS "Serviceberater",
+	CASE 
+		WHEN (T1."invoice_type" = 6)
+			THEN ('GWL')
+		WHEN (
+				(T1."invoice_type" = 4)
+				OR (T3."customer_number" = 100001)
+				)
+			THEN ('intern')
+		ELSE ('extern')
+		END AS "Umsatzart",
+	T4."description" AS "Fabrikat",
+	T4."description" AS "Description_Makes",
+	T5."description" AS "Description_Models",
+	(left((ucase(T5."description")), 3)) AS "Model",
+	(left((left((((T1."vehicle_number"))), 5)) + ' - ' + T5."description", 100)) AS "Fahrzeug",
+	CASE 
+		WHEN ((left((((T6."type"))), 1)) IN ('1', '6'))
+			THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+		WHEN (T6."type" IN (40, 41, 44, 45, 46, 47, 48, 49, 88, 89, 50, 90))
+			THEN ('41 - After Sales Kundendienst eigene Werkstatt')
+		WHEN ((left((((T6."type"))), 1)) IN ('2'))
+			THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+		WHEN (T6."type" IN (42, 92))
+			THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung')
+		WHEN ((left((((T6."type"))), 1)) IN ('3'))
+			THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+		WHEN (T6."type" IN (43, 93))
+			THEN ('45 - After Sales Kundendienst eigene Lackiererei')
+		ELSE NULL
+		END AS "Kostenstelle",
+	CASE 
+		WHEN (T4."description" = 'Ford')
+			THEN ('1')
+		WHEN (T4."description" = 'Nissan')
+			THEN ('3')
+		WHEN (T4."description" = 'Suzuki')
+			THEN ('2')
+		ELSE ('9')
+		END AS "Marke",
+	'Service' AS "Auftragsart",
+	T3."zip_code" AS "Gesch�ftsart",
+	CASE 
+		WHEN (T3."first_name" IS NULL)
+			THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+		ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+		END AS "Kunde",
+	T7."time_units" AS "Time Units",
+	(convert(FLOAT, T7."time_units")) AS "Time Units_Zahl",
+	10 AS "AW/Std.",
+	((convert(FLOAT, T7."time_units"))) / 10 AS "verk. Std.",
+	CASE 
+		WHEN (T2."Serviceberater" IS NOT NULL)
+			THEN (
+					(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+						CASE 
+							WHEN (T3."first_name" IS NULL)
+								THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+							ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+							END
+						)
+					)
+		ELSE (
+				(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+					CASE 
+						WHEN (T3."first_name" IS NULL)
+							THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+						ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+						END
+					)
+				)
+		END AS "Order Number",
+	CASE 
+		WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."invoice_date")))) <= 4)
+			THEN (
+					(
+						CASE 
+							WHEN (T2."Serviceberater" IS NOT NULL)
+								THEN (
+										(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + T2."Serviceberater" + ' - ' + (
+											CASE 
+												WHEN (T3."first_name" IS NULL)
+													THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+												ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+												END
+											)
+										)
+							ELSE (
+									(left((((T1."invoice_type"))), 1)) + (left((((T1."invoice_number"))), 9)) + ' - ' + (
+										CASE 
+											WHEN (T3."first_name" IS NULL)
+												THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+											ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+											END
+										)
+									)
+							END
+						)
+					)
+		ELSE NULL
+		END AS "Order Number_Rg_Ausg",
+	(left((((T8."employee_number"))), 4)) + ' - ' + T8."name" AS "Kundenart"
+FROM (
+	(
+		(
+			(
+				(
+					(
+						(
+							(
+								"dbo"."invoices" T1 LEFT JOIN "dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."paying_customer"
+								) LEFT JOIN "dbo"."vehicles" T9 ON T1."vehicle_number" = T9."internal_number"
+							) LEFT JOIN "dbo"."makes" T4 ON T9."make_number" = T4."make_number"
+						) LEFT JOIN "dbo"."models" T5 ON (T9."make_number" = T5."make_number")
+						AND (T9."model_code" = T5."model_code")
+					) LEFT JOIN "dbo"."labours" T7 ON (T7."invoice_number" = T1."invoice_number")
+					AND (T7."invoice_type" = T1."invoice_type")
+				) LEFT JOIN "dbo"."charge_type_descriptions" T6 ON T6."type" = T7."charge_type"
+			) LEFT JOIN "dbo"."employees" T8 ON T1."creating_employee" = T8."employee_number"
+		) LEFT JOIN "ims"."Serviceberater_Rechnung" T2 ON T1."invtype_invnr" = T2."invtype_invnr"
+	)
+WHERE (
+		(
+			(
+				(
+					(T1."invoice_type" BETWEEN 2 AND 6)
+					AND (T1."is_canceled" <> 1)
+					)
+				AND (T1."invoice_date" >= convert(DATE, '2017-01-01'))
+				)
+			AND (
+				(((convert(FLOAT, T7."time_units"))) <> 0)
+				AND (((convert(FLOAT, T7."time_units"))) IS NOT NULL)
+				)
+			)
+		AND (T7."mechanic_no" IS NOT NULL)
+		)
+	-- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
 GO
-CREATE VIEW staging.aftersales_rechnungen_verk_aw_final_imr AS
-
-select T1."invoice_type" as "Invoice Type",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."subsidiary" as "Subsidiary",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."service_date" as "Service Date",
-	   T1."is_canceled" as "Is Canceled",
-	   T1."vehicle_number" as "Vehicle Number",
-	   (left((((T1."invoice_type"))),1)) + '_' + (left((((T1."invoice_number"))),9)) as "Invoice_Type_Invoice_Number",
-	   '1' as "Hauptbetrieb",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   T2."Serviceberater" as "Serviceberater",
-	   CASE WHEN (T1."invoice_type" = 6) THEN ('GWL') WHEN ((T1."invoice_type" = 4) or (T3."customer_number" = 100001)) THEN ('intern') ELSE ('extern') END as "Umsatzart",
-	   T4."description" as "Fabrikat",
-	   T4."description" as "Description_Makes",
-	   T5."description" as "Description_Models",
-	   (left((ucase(T5."description")),3)) as "Model",
-	   (left((left((((T1."vehicle_number"))),5)) + ' - ' + T5."description",100)) as "Fahrzeug",
-	   CASE WHEN ((left((((T6."type"))),1)) IN ('1','6')) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN (T6."type" IN (40,41,44,45,46,47,48,49,88,89,50,90)) THEN ('41 - After Sales Kundendienst eigene Werkstatt') WHEN ((left((((T6."type"))),1)) IN ('2')) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN (T6."type" IN (42,92)) THEN ('44 - After Sales Kundendienst eigene Karosserieabteilung') WHEN ((left((((T6."type"))),1)) IN ('3')) THEN ('45 - After Sales Kundendienst eigene Lackiererei') WHEN (T6."type" IN (43,93)) THEN ('45 - After Sales Kundendienst eigene Lackiererei') ELSE null END as "Kostenstelle",
-	   CASE WHEN (T4."description" = 'Ford') THEN ('1') WHEN (T4."description" = 'Nissan') THEN ('3') WHEN (T4."description" = 'Suzuki') THEN ('2') ELSE ('9') END as "Marke",
-	   'Service' as "Auftragsart",
-	   T3."zip_code" as "Geschäftsart",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END as "Kunde",
-	   T7."time_units" as "Time Units",
-	   (convert(float, T7."time_units")) as "Time Units_Zahl",
-	   10 as "AW/Std.",
-	   ((convert(float, T7."time_units"))) / 10 as "verk. Std.",
-	   CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END as "Order Number",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."invoice_date")))) <= 4) THEN ((CASE WHEN (T2."Serviceberater" IS NOT NULL) THEN ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + T2."Serviceberater" + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) ELSE ((left((((T1."invoice_type"))),1)) + (left((((T1."invoice_number"))),9)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") END)) END)) ELSE null END as "Order Number_Rg_Ausg",
-	   (left((((T8."employee_number"))),4)) + ' - ' + T8."name" as "Kundenart"
-from (((((((("dbo"."invoices" T1 left outer join "dbo"."customers_suppliers" T3 on T3."customer_number" = T1."paying_customer") left outer join "dbo"."vehicles" T9 on T1."vehicle_number" = T9."internal_number") left outer join "dbo"."makes" T4 on T9."make_number" = T4."make_number") left outer join "dbo"."models" T5 on (T9."make_number" = T5."make_number") and (T9."model_code" = T5."model_code")) left outer join "dbo"."labours" T7 on (T7."invoice_number" = T1."invoice_number") and (T7."invoice_type" = T1."invoice_type")) left outer join "dbo"."charge_type_descriptions" T6 on T6."type" = T7."charge_type") left outer join "dbo"."employees" T8 on T1."creating_employee" = T8."employee_number") left outer join "ims"."Serviceberater_Rechnung" T2 on T1."invtype_invnr" = T2."invtype_invnr")
-where (((((T1."invoice_type" BETWEEN 2 AND 6) and (T1."is_canceled" <> 1)) and (T1."invoice_date" >= convert(date, '2017-01-01'))) and ((((convert(float, T7."time_units"))) <> 0) and (((convert(float, T7."time_units"))) IS NOT NULL))) and (T7."mechanic_no" IS NOT NULL))
--- order by "Invoice_Type_Invoice_Number" asc,"Invoice Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 81 - 37
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.current_date_prognose_imr.sql

@@ -1,43 +1,87 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.current_date_prognose_imr
+AS
+SELECT "Bundeslaender Id" AS "Bundeslaender Id",
+	"Datum" AS "Datum",
+	"Wochentage Id" AS "Wochentage Id",
+	"Arbeitstag Mofr" AS "Arbeitstag Mofr",
+	"Zaehler Mofr" AS "Zaehler Mofr",
+	"Summe Mofr" AS "Summe Mofr",
+	"Arbeitstag Mosa" AS "Arbeitstag Mosa",
+	"Zaehler Mosa" AS "Zaehler Mosa",
+	"Summe Mosa" AS "Summe Mosa",
+	"Feiertage Id" AS "Feiertage Id",
+	"Jahr" AS "Jahr",
+	CASE 
+		WHEN ("Arbeitstag Mofr" = 1)
+			THEN (RCOUNT("Arbeitstag Mofr"))
+		ELSE NULL
+		END AS "Arbeitstag Nr Jahr",
+	"Gesamt Arbeitstage" AS "Gesamt Arbeitstage"
+FROM (
+	SELECT T1."bundeslaender_id" AS "Bundeslaender Id",
+		T1."datum" AS "Datum",
+		T1."wochentage_id" AS "Wochentage Id",
+		T1."arbeitstag_mofr" AS "Arbeitstag Mofr",
+		T1."zaehler_mofr" AS "Zaehler Mofr",
+		T1."summe_mofr" AS "Summe Mofr",
+		T1."arbeitstag_mosa" AS "Arbeitstag Mosa",
+		T1."zaehler_mosa" AS "Zaehler Mosa",
+		T1."summe_mosa" AS "Summe Mosa",
+		T1."feiertage_id" AS "Feiertage Id",
+		(getdate()) - 1 AS "Jahr",
+		CASE 
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2008-01-01 00:00:00.000') AND convert(DATETIME, '2008-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2009-01-01 00:00:00.000') AND convert(DATETIME, '2009-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2010-01-01 00:00:00.000') AND convert(DATETIME, '2010-12-31 00:00:00.000'))
+				THEN (253)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2011-01-01 00:00:00.000') AND convert(DATETIME, '2011-12-31 00:00:00.000'))
+				THEN (250)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2012-01-01 00:00:00.000') AND convert(DATETIME, '2012-12-31 00:00:00.000'))
+				THEN (249)
+			ELSE (250)
+			END AS "Gesamt Arbeitstage"
+	FROM "Arbeitstage" T1
+	WHERE (
+			(
+				(
+					(
+						(T1."bundeslaender_id" = '2')
+						AND (T1."arbeitstag_mofr" = 1)
+						)
+					AND ((year(T1."datum")) = (year(((getdate()) - 1))))
+					)
+				AND (T1."datum" <= ((getdate()) - 1))
+				)
+			OR (
+				(
+					(
+						(T1."bundeslaender_id" = '2')
+						AND ((year(T1."datum")) = (year(((getdate()) - 1))))
+						)
+					AND (T1."datum" <= ((getdate()) - 1))
+					)
+				AND (T1."datum" IN (convert(DATETIME, '2022-01-01 00:00:00.000'), convert(DATETIME, '2023-01-01 00:00:00.000'), convert(DATETIME, '2024-01-01 00:00:00.000'), convert(DATETIME, '2025-01-01 00:00:00.000'), convert(DATETIME, '2026-01-01 00:00:00.000')))
+				)
+			)
+		-- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
+	) D1
 GO
-CREATE VIEW staging.current_date_prognose_imr AS
-
-select "Bundeslaender Id" as "Bundeslaender Id",
-	   "Datum" as "Datum",
-	   "Wochentage Id" as "Wochentage Id",
-	   "Arbeitstag Mofr" as "Arbeitstag Mofr",
-	   "Zaehler Mofr" as "Zaehler Mofr",
-	   "Summe Mofr" as "Summe Mofr",
-	   "Arbeitstag Mosa" as "Arbeitstag Mosa",
-	   "Zaehler Mosa" as "Zaehler Mosa",
-	   "Summe Mosa" as "Summe Mosa",
-	   "Feiertage Id" as "Feiertage Id",
-	   "Jahr" as "Jahr",
-	   CASE WHEN ("Arbeitstag Mofr" = 1) THEN (RCOUNT("Arbeitstag Mofr")) ELSE null END as "Arbeitstag Nr Jahr",
-	   "Gesamt Arbeitstage" as "Gesamt Arbeitstage"
-from 
-(select T1."bundeslaender_id" as "Bundeslaender Id",
-	   T1."datum" as "Datum",
-	   T1."wochentage_id" as "Wochentage Id",
-	   T1."arbeitstag_mofr" as "Arbeitstag Mofr",
-	   T1."zaehler_mofr" as "Zaehler Mofr",
-	   T1."summe_mofr" as "Summe Mofr",
-	   T1."arbeitstag_mosa" as "Arbeitstag Mosa",
-	   T1."zaehler_mosa" as "Zaehler Mosa",
-	   T1."summe_mosa" as "Summe Mosa",
-	   T1."feiertage_id" as "Feiertage Id",
-	   (getdate()) - 1 as "Jahr",
-	   CASE WHEN (T1."datum" BETWEEN convert(datetime, '2008-01-01 00:00:00.000') AND convert(datetime, '2008-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2009-01-01 00:00:00.000') AND convert(datetime, '2009-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2010-01-01 00:00:00.000') AND convert(datetime, '2010-12-31 00:00:00.000')) THEN (253) WHEN (T1."datum" BETWEEN convert(datetime, '2011-01-01 00:00:00.000') AND convert(datetime, '2011-12-31 00:00:00.000')) THEN (250) WHEN (T1."datum" BETWEEN convert(datetime, '2012-01-01 00:00:00.000') AND convert(datetime, '2012-12-31 00:00:00.000')) THEN (249) ELSE (250) END as "Gesamt Arbeitstage"
-from "Arbeitstage" T1
-where (((((T1."bundeslaender_id" = '2') and (T1."arbeitstag_mofr" = 1)) and ((year(T1."datum")) = (year(((getdate()) - 1))))) and (T1."datum" <= ((getdate()) - 1))) or ((((T1."bundeslaender_id" = '2') and ((year(T1."datum")) = (year(((getdate()) - 1))))) and (T1."datum" <= ((getdate()) - 1))) and (T1."datum" IN (convert(datetime, '2022-01-01 00:00:00.000'),convert(datetime, '2023-01-01 00:00:00.000'),convert(datetime, '2024-01-01 00:00:00.000'),convert(datetime, '2025-01-01 00:00:00.000'),convert(datetime, '2026-01-01 00:00:00.000')))))
--- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
-) D1
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 78 - 37
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.current_date_prognose_operativ_vj-2_imr.sql

@@ -1,43 +1,84 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.current_date_prognose_operativ_vj - 2 _imr
+AS
+SELECT "Bundeslaender Id" AS "Bundeslaender Id",
+	"Datum" AS "Datum",
+	"Wochentage Id" AS "Wochentage Id",
+	"Arbeitstag Mofr" AS "Arbeitstag Mofr",
+	"Zaehler Mofr" AS "Zaehler Mofr",
+	"Summe Mofr" AS "Summe Mofr",
+	"Arbeitstag Mosa" AS "Arbeitstag Mosa",
+	"Zaehler Mosa" AS "Zaehler Mosa",
+	"Summe Mosa" AS "Summe Mosa",
+	"Feiertage Id" AS "Feiertage Id",
+	"Jahr" AS "Jahr",
+	CASE 
+		WHEN ("Arbeitstag Mofr" = 1)
+			THEN (RCOUNT("Arbeitstag Mofr"))
+		ELSE NULL
+		END AS "Arbeitstag Nr Jahr",
+	"Gesamt Arbeitstage" AS "Gesamt Arbeitstage"
+FROM (
+	SELECT T1."bundeslaender_id" AS "Bundeslaender Id",
+		T1."datum" AS "Datum",
+		T1."wochentage_id" AS "Wochentage Id",
+		T1."arbeitstag_mofr" AS "Arbeitstag Mofr",
+		T1."zaehler_mofr" AS "Zaehler Mofr",
+		T1."summe_mofr" AS "Summe Mofr",
+		T1."arbeitstag_mosa" AS "Arbeitstag Mosa",
+		T1."zaehler_mosa" AS "Zaehler Mosa",
+		T1."summe_mosa" AS "Summe Mosa",
+		T1."feiertage_id" AS "Feiertage Id",
+		(getdate()) - INTERVAL '000 00:00:00.000' AS "Jahr",
+		CASE 
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2008-01-01 00:00:00.000') AND convert(DATETIME, '2008-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2009-01-01 00:00:00.000') AND convert(DATETIME, '2009-12-31 00:00:00.000'))
+				THEN (252)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2010-01-01 00:00:00.000') AND convert(DATETIME, '2010-12-31 00:00:00.000'))
+				THEN (253)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2011-01-01 00:00:00.000') AND convert(DATETIME, '2011-12-31 00:00:00.000'))
+				THEN (250)
+			WHEN (T1."datum" BETWEEN convert(DATETIME, '2012-01-01 00:00:00.000') AND convert(DATETIME, '2012-12-31 00:00:00.000'))
+				THEN (249)
+			ELSE (250)
+			END AS "Gesamt Arbeitstage"
+	FROM "Arbeitstage" T1
+	WHERE (
+			(
+				(
+					(T1."bundeslaender_id" = '4')
+					AND ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))
+					)
+				AND (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))
+				)
+			OR (
+				(
+					(
+						(T1."bundeslaender_id" = '4')
+						AND ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))
+						)
+					AND (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))
+					)
+				AND (T1."datum" IN (convert(DATETIME, '2012-01-01 00:00:00.000'), convert(DATETIME, '2013-01-01 00:00:00.000'), convert(DATETIME, '2014-01-01 00:00:00.000'), convert(DATETIME, '2015-01-01 00:00:00.000'), convert(DATETIME, '2016-01-01 00:00:00.000'), convert(DATETIME, '2017-01-01 00:00:00.000')))
+				)
+			)
+		-- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
+	) D1
 GO
-CREATE VIEW staging.current_date_prognose_operativ_vj-2_imr AS
-
-select "Bundeslaender Id" as "Bundeslaender Id",
-	   "Datum" as "Datum",
-	   "Wochentage Id" as "Wochentage Id",
-	   "Arbeitstag Mofr" as "Arbeitstag Mofr",
-	   "Zaehler Mofr" as "Zaehler Mofr",
-	   "Summe Mofr" as "Summe Mofr",
-	   "Arbeitstag Mosa" as "Arbeitstag Mosa",
-	   "Zaehler Mosa" as "Zaehler Mosa",
-	   "Summe Mosa" as "Summe Mosa",
-	   "Feiertage Id" as "Feiertage Id",
-	   "Jahr" as "Jahr",
-	   CASE WHEN ("Arbeitstag Mofr" = 1) THEN (RCOUNT("Arbeitstag Mofr")) ELSE null END as "Arbeitstag Nr Jahr",
-	   "Gesamt Arbeitstage" as "Gesamt Arbeitstage"
-from 
-(select T1."bundeslaender_id" as "Bundeslaender Id",
-	   T1."datum" as "Datum",
-	   T1."wochentage_id" as "Wochentage Id",
-	   T1."arbeitstag_mofr" as "Arbeitstag Mofr",
-	   T1."zaehler_mofr" as "Zaehler Mofr",
-	   T1."summe_mofr" as "Summe Mofr",
-	   T1."arbeitstag_mosa" as "Arbeitstag Mosa",
-	   T1."zaehler_mosa" as "Zaehler Mosa",
-	   T1."summe_mosa" as "Summe Mosa",
-	   T1."feiertage_id" as "Feiertage Id",
-	   (getdate()) - INTERVAL '000 00:00:00.000' as "Jahr",
-	   CASE WHEN (T1."datum" BETWEEN convert(datetime, '2008-01-01 00:00:00.000') AND convert(datetime, '2008-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2009-01-01 00:00:00.000') AND convert(datetime, '2009-12-31 00:00:00.000')) THEN (252) WHEN (T1."datum" BETWEEN convert(datetime, '2010-01-01 00:00:00.000') AND convert(datetime, '2010-12-31 00:00:00.000')) THEN (253) WHEN (T1."datum" BETWEEN convert(datetime, '2011-01-01 00:00:00.000') AND convert(datetime, '2011-12-31 00:00:00.000')) THEN (250) WHEN (T1."datum" BETWEEN convert(datetime, '2012-01-01 00:00:00.000') AND convert(datetime, '2012-12-31 00:00:00.000')) THEN (249) ELSE (250) END as "Gesamt Arbeitstage"
-from "Arbeitstage" T1
-where ((((T1."bundeslaender_id" = '4') and ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))) and (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))) or ((((T1."bundeslaender_id" = '4') and ((year(T1."datum")) = ((year(((getdate()) - INTERVAL '000 00:00:00.000'))) - 2))) and (T1."datum" <= ((getdate()) - INTERVAL '000 00:00:00.000'))) and (T1."datum" IN (convert(datetime, '2012-01-01 00:00:00.000'),convert(datetime, '2013-01-01 00:00:00.000'),convert(datetime, '2014-01-01 00:00:00.000'),convert(datetime, '2015-01-01 00:00:00.000'),convert(datetime, '2016-01-01 00:00:00.000'),convert(datetime, '2017-01-01 00:00:00.000')))))
--- order by "Bundeslaender Id" asc,"Datum" asc,"Wochentage Id" asc,"Arbeitstag Mofr" asc,"Zaehler Mofr" asc,"Summe Mofr" asc,"Arbeitstag Mosa" asc,"Zaehler Mosa" asc,"Summe Mosa" asc,"Feiertage Id" asc,"Gesamt Arbeitstage" asc
-) D1
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 121 - 114
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.kontenrahmen_gc_struct_skr_imr.sql

@@ -1,121 +1,128 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.kontenrahmen_gc_struct_skr_imr AS
 
-select T1."KONTO_NR" as "Konto Nr",
-	   T1."KONTO_BEZEICHNUNG" as "Konto Bezeichnung",
-	   T1."KONTO_ART" as "Konto Art_ori",
-	   T1."KOSTENSTELLE" as "Kostenstelle",
-	   T1."STK" as "Stk",
-	   T1."KONTO_1" as "Konto 1",
-	   T1."KONTO_2" as "Konto 2",
-	   T1."KONTO_3" as "Konto 3",
-	   T1."KONTO_4" as "Konto 4",
-	   T1."KONTO_5" as "Konto 5",
-	   T1."EBENE1" as "Ebene1",
-	   T1."EBENE2" as "Ebene2",
-	   T1."EBENE3" as "Ebene3",
-	   T1."EBENE4" as "Ebene4",
-	   T1."EBENE5" as "Ebene5",
-	   T1."EBENE6" as "Ebene6",
-	   T1."EBENE7" as "Ebene7",
-	   T1."EBENE8" as "Ebene8",
-	   T1."EBENE9" as "Ebene9",
-	   T1."EBENE10" as "Ebene10",
-	   T1."EBENE11" as "Ebene11",
-	   T1."EBENE12" as "Ebene12",
-	   T1."EBENE13" as "Ebene13",
-	   T1."EBENE14" as "Ebene14",
-	   T1."EBENE15" as "Ebene15",
-	   T1."EBENE16" as "Ebene16",
-	   T1."EBENE17" as "Ebene17",
-	   T1."EBENE18" as "Ebene18",
-	   T1."EBENE19" as "Ebene19",
-	   T1."EBENE20" as "Ebene20",
-	   T1."EBENE21" as "Ebene21",
-	   T1."EBENE22" as "Ebene22",
-	   T1."EBENE23" as "Ebene23",
-	   T1."EBENE24" as "Ebene24",
-	   T1."EBENE25" as "Ebene25",
-	   T1."EBENE26" as "Ebene26",
-	   T1."EBENE27" as "Ebene27",
-	   T1."EBENE28" as "Ebene28",
-	   T1."EBENE29" as "Ebene29",
-	   T1."EBENE30" as "Ebene30",
-	   T1."EBENE31" as "Ebene31",
-	   T1."EBENE32" as "Ebene32",
-	   T1."EBENE33" as "Ebene33",
-	   T1."EBENE34" as "Ebene34",
-	   T1."EBENE35" as "Ebene35",
-	   T1."EBENE36" as "Ebene36",
-	   T1."EBENE37" as "Ebene37",
-	   T1."EBENE38" as "Ebene38",
-	   T1."EBENE39" as "Ebene39",
-	   T1."EBENE40" as "Ebene40",
-	   T1."EBENE41" as "Ebene41",
-	   T1."EBENE42" as "Ebene42",
-	   T1."EBENE43" as "Ebene43",
-	   T1."EBENE44" as "Ebene44",
-	   T1."EBENE45" as "Ebene45",
-	   T1."EBENE46" as "Ebene46",
-	   T1."EBENE47" as "Ebene47",
-	   T1."EBENE48" as "Ebene48",
-	   T1."EBENE49" as "Ebene49",
-	   T1."EBENE50" as "Ebene50",
-	   T1."EBENE51" as "Ebene51",
-	   T1."EBENE52" as "Ebene52",
-	   T1."EBENE53" as "Ebene53",
-	   T1."EBENE54" as "Ebene54",
-	   T1."EBENE55" as "Ebene55",
-	   T1."EBENE56" as "Ebene56",
-	   T1."EBENE57" as "Ebene57",
-	   T1."EBENE58" as "Ebene58",
-	   T1."EBENE59" as "Ebene59",
-	   T1."EBENE60" as "Ebene60",
-	   T1."EBENE61" as "Ebene61",
-	   T1."EBENE62" as "Ebene62",
-	   T1."EBENE63" as "Ebene63",
-	   T1."EBENE64" as "Ebene64",
-	   T1."EBENE65" as "Ebene65",
-	   T1."EBENE66" as "Ebene66",
-	   T1."EBENE67" as "Ebene67",
-	   T1."EBENE68" as "Ebene68",
-	   T1."EBENE69" as "Ebene69",
-	   T1."EBENE70" as "Ebene70",
-	   T1."EBENE71" as "Ebene71",
-	   T1."EBENE72" as "Ebene72",
-	   T1."EBENE73" as "Ebene73",
-	   T1."EBENE74" as "Ebene74",
-	   T1."EBENE75" as "Ebene75",
-	   T1."EBENE76" as "Ebene76",
-	   T1."EBENE77" as "Ebene77",
-	   T1."EBENE78" as "Ebene78",
-	   T1."EBENE79" as "Ebene79",
-	   T1."EBENE80" as "Ebene80",
-	   T1."EBENE81" as "Ebene81",
-	   T1."EBENE82" as "Ebene82",
-	   T1."EBENE83" as "Ebene83",
-	   T1."EBENE84" as "Ebene84",
-	   T1."EBENE85" as "Ebene85",
-	   T1."EBENE86" as "Ebene86",
-	   T1."EBENE87" as "Ebene87",
-	   T1."EBENE88" as "Ebene88",
-	   T1."EBENE89" as "Ebene89",
-	   T1."EBENE90" as "Ebene90",
-	   (len(T1."KONTO_NR" + 'Z') - 1) as "Anzahl Stellen_Konto_Nr",
-	   T1."KONTO_ART" as "Konto Art",
-	   T1."KONTO_ART" as "GuV_Bilanz",
-	   T1."KONTO_NR" as "Acct Nr",
-	   T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" as "Konto",
-	   (left(T1."KONTO_NR",1)) as "Susa"
-from "Kontenrahmen.csv" T1
-where (((left(T1."KONTO_NR",1))) <> '9')
+CREATE VIEW staging.kontenrahmen_gc_struct_skr_imr
+AS
+SELECT T1."KONTO_NR" AS "Konto Nr",
+	T1."KONTO_BEZEICHNUNG" AS "Konto Bezeichnung",
+	T1."KONTO_ART" AS "Konto Art_ori",
+	T1."KOSTENSTELLE" AS "Kostenstelle",
+	T1."STK" AS "Stk",
+	T1."KONTO_1" AS "Konto 1",
+	T1."KONTO_2" AS "Konto 2",
+	T1."KONTO_3" AS "Konto 3",
+	T1."KONTO_4" AS "Konto 4",
+	T1."KONTO_5" AS "Konto 5",
+	T1."EBENE1" AS "Ebene1",
+	T1."EBENE2" AS "Ebene2",
+	T1."EBENE3" AS "Ebene3",
+	T1."EBENE4" AS "Ebene4",
+	T1."EBENE5" AS "Ebene5",
+	T1."EBENE6" AS "Ebene6",
+	T1."EBENE7" AS "Ebene7",
+	T1."EBENE8" AS "Ebene8",
+	T1."EBENE9" AS "Ebene9",
+	T1."EBENE10" AS "Ebene10",
+	T1."EBENE11" AS "Ebene11",
+	T1."EBENE12" AS "Ebene12",
+	T1."EBENE13" AS "Ebene13",
+	T1."EBENE14" AS "Ebene14",
+	T1."EBENE15" AS "Ebene15",
+	T1."EBENE16" AS "Ebene16",
+	T1."EBENE17" AS "Ebene17",
+	T1."EBENE18" AS "Ebene18",
+	T1."EBENE19" AS "Ebene19",
+	T1."EBENE20" AS "Ebene20",
+	T1."EBENE21" AS "Ebene21",
+	T1."EBENE22" AS "Ebene22",
+	T1."EBENE23" AS "Ebene23",
+	T1."EBENE24" AS "Ebene24",
+	T1."EBENE25" AS "Ebene25",
+	T1."EBENE26" AS "Ebene26",
+	T1."EBENE27" AS "Ebene27",
+	T1."EBENE28" AS "Ebene28",
+	T1."EBENE29" AS "Ebene29",
+	T1."EBENE30" AS "Ebene30",
+	T1."EBENE31" AS "Ebene31",
+	T1."EBENE32" AS "Ebene32",
+	T1."EBENE33" AS "Ebene33",
+	T1."EBENE34" AS "Ebene34",
+	T1."EBENE35" AS "Ebene35",
+	T1."EBENE36" AS "Ebene36",
+	T1."EBENE37" AS "Ebene37",
+	T1."EBENE38" AS "Ebene38",
+	T1."EBENE39" AS "Ebene39",
+	T1."EBENE40" AS "Ebene40",
+	T1."EBENE41" AS "Ebene41",
+	T1."EBENE42" AS "Ebene42",
+	T1."EBENE43" AS "Ebene43",
+	T1."EBENE44" AS "Ebene44",
+	T1."EBENE45" AS "Ebene45",
+	T1."EBENE46" AS "Ebene46",
+	T1."EBENE47" AS "Ebene47",
+	T1."EBENE48" AS "Ebene48",
+	T1."EBENE49" AS "Ebene49",
+	T1."EBENE50" AS "Ebene50",
+	T1."EBENE51" AS "Ebene51",
+	T1."EBENE52" AS "Ebene52",
+	T1."EBENE53" AS "Ebene53",
+	T1."EBENE54" AS "Ebene54",
+	T1."EBENE55" AS "Ebene55",
+	T1."EBENE56" AS "Ebene56",
+	T1."EBENE57" AS "Ebene57",
+	T1."EBENE58" AS "Ebene58",
+	T1."EBENE59" AS "Ebene59",
+	T1."EBENE60" AS "Ebene60",
+	T1."EBENE61" AS "Ebene61",
+	T1."EBENE62" AS "Ebene62",
+	T1."EBENE63" AS "Ebene63",
+	T1."EBENE64" AS "Ebene64",
+	T1."EBENE65" AS "Ebene65",
+	T1."EBENE66" AS "Ebene66",
+	T1."EBENE67" AS "Ebene67",
+	T1."EBENE68" AS "Ebene68",
+	T1."EBENE69" AS "Ebene69",
+	T1."EBENE70" AS "Ebene70",
+	T1."EBENE71" AS "Ebene71",
+	T1."EBENE72" AS "Ebene72",
+	T1."EBENE73" AS "Ebene73",
+	T1."EBENE74" AS "Ebene74",
+	T1."EBENE75" AS "Ebene75",
+	T1."EBENE76" AS "Ebene76",
+	T1."EBENE77" AS "Ebene77",
+	T1."EBENE78" AS "Ebene78",
+	T1."EBENE79" AS "Ebene79",
+	T1."EBENE80" AS "Ebene80",
+	T1."EBENE81" AS "Ebene81",
+	T1."EBENE82" AS "Ebene82",
+	T1."EBENE83" AS "Ebene83",
+	T1."EBENE84" AS "Ebene84",
+	T1."EBENE85" AS "Ebene85",
+	T1."EBENE86" AS "Ebene86",
+	T1."EBENE87" AS "Ebene87",
+	T1."EBENE88" AS "Ebene88",
+	T1."EBENE89" AS "Ebene89",
+	T1."EBENE90" AS "Ebene90",
+	(len(T1."KONTO_NR" + 'Z') - 1) AS "Anzahl Stellen_Konto_Nr",
+	T1."KONTO_ART" AS "Konto Art",
+	T1."KONTO_ART" AS "GuV_Bilanz",
+	T1."KONTO_NR" AS "Acct Nr",
+	T1."KONTO_NR" + ' - ' + T1."KONTO_BEZEICHNUNG" AS "Konto",
+	(left(T1."KONTO_NR", 1)) AS "Susa"
+FROM "Kontenrahmen.csv" T1
+WHERE (((left(T1."KONTO_NR", 1))) <> '9')
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 3 - 114
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.kreditorenbewegungen_imr.sql


+ 180 - 76
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_bilanz_imr.sql

@@ -1,83 +1,187 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.loc_belege_bilanz_imr AS
 
-select T1."accounting_date" as "Accounting Date",
-	   T1."document_type" as "Document Type",
-	   T1."document_number" as "Document Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."customer_number" as "Customer Number",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."document_date" as "Document Date",
-	   T1."posted_value" as "Posted Value",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_count" as "Posted Count",
-	   T1."branch_number" as "Branch Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."employee_number" as "Employee Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."journal_page" as "Journal Page",
-	   T1."journal_line" as "Journal Line",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."term_of_payment" as "Term Of Payment",
-	   '-' as "Posting Text",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."vat_key" as "Vat Key",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   (left(T1."free_form_accounting_text",100)) as "Free Form Accounting Text",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."accounting_date" as "Bookkeep Date",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   '' as "Text",
-	   '1' as "Mandant",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   (rtrim((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END))) + ' - ' + T2."account_description" as "Konto_mit_Bezeichnung",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   (substring((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END), 1, 1)) as "Susa",
-	   CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END as "Acct Nr",
-	   (len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1) as "Stellen_Konto_Nr"
-from "dbo"."nominal_accounts" T2,
-	("dbo"."journal_accountings" T1 left outer join "dbo"."accounts_characteristics" T3 on (((((T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref") and (T3."skr51_branch" = T1."skr51_branch")) and (T3."skr51_make" = T1."skr51_make")) and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((T2."is_profit_loss_account" = 'N') and (not ((rtrim((CASE WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1) THEN ('000' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2) THEN ('00' + (rtrim((((T1."nominal_account_number")))))) WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3) THEN ('0' + (rtrim((((T1."nominal_account_number")))))) ELSE ((rtrim((((T1."nominal_account_number")))))) END))) + ' - ' + T2."account_description") LIKE '9%'))
--- order by "Nominal Account Number" asc
+CREATE VIEW staging.loc_belege_bilanz_imr
+AS
+SELECT T1."accounting_date" AS "Accounting Date",
+	T1."document_type" AS "Document Type",
+	T1."document_number" AS "Document Number",
+	T1."position_in_document" AS "Position In Document",
+	T1."customer_number" AS "Customer Number",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."is_balanced" AS "Is Balanced",
+	T1."clearing_number" AS "Clearing Number",
+	T1."document_date" AS "Document Date",
+	T1."posted_value" AS "Posted Value",
+	T1."debit_or_credit" AS "Debit Or Credit",
+	T1."posted_count" AS "Posted Count",
+	T1."branch_number" AS "Branch Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."nominal_contra_account" AS "Nominal Contra Account",
+	T1."contra_account_text" AS "Contra Account Text",
+	T1."account_form_page_number" AS "Account Form Page Number",
+	T1."account_form_page_line" AS "Account Form Page Line",
+	T1."serial_number_each_month" AS "Serial Number Each Month",
+	T1."employee_number" AS "Employee Number",
+	T1."invoice_date" AS "Invoice Date",
+	T1."invoice_number" AS "Invoice Number",
+	T1."dunning_level" AS "Dunning Level",
+	T1."last_dunning_date" AS "Last Dunning Date",
+	T1."journal_page" AS "Journal Page",
+	T1."journal_line" AS "Journal Line",
+	T1."cash_discount" AS "Cash Discount",
+	T1."term_of_payment" AS "Term Of Payment",
+	'-' AS "Posting Text",
+	T1."vehicle_reference" AS "Vehicle Reference",
+	T1."vat_id_number" AS "Vat Id Number",
+	T1."account_statement_number" AS "Account Statement Number",
+	T1."account_statement_page" AS "Account Statement Page",
+	T1."vat_key" AS "Vat Key",
+	T1."days_for_cash_discount" AS "Days For Cash Discount",
+	T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+	T1."skr51_branch" AS "Skr51 Branch",
+	T1."skr51_make" AS "Skr51 Make",
+	T1."skr51_cost_center" AS "Skr51 Cost Center",
+	T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+	T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+	T1."previously_used_account_no" AS "Previously Used Account No",
+	(left(T1."free_form_accounting_text", 100)) AS "Free Form Accounting Text",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN ('Neuwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+			THEN ('Gebrauchtwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+			THEN ('Teile & Zubeh�r')
+		WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+			THEN ('Service')
+		WHEN (T1."skr51_cost_unit" = 0)
+			THEN ('Ohne Kostentr�ger')
+		ELSE NULL
+		END AS "Free Form Document Text",
+	T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+	T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+	('0' + (((T1."skr51_branch")))) AS "Betrieb",
+	T1."accounting_date" AS "Bookkeep Date",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+	'' AS "Text",
+	'1' AS "Mandant",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_value" / 100 * - 1)
+		ELSE (T1."posted_value" / 100)
+		END AS "Betrag",
+	(
+		rtrim((
+				CASE 
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+						THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+						THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+						THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+					ELSE ((rtrim((((T1."nominal_account_number"))))))
+					END
+				))
+		) + ' - ' + T2."account_description" AS "Konto_mit_Bezeichnung",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_count" / 100 * - 1)
+		ELSE (T1."posted_count" / 100)
+		END AS "Menge",
+	(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+	(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+	(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+	(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+	(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+		ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+		END AS "Kostentr�ger",
+	CASE 
+		WHEN (T2."is_profit_loss_account" = 'J')
+			THEN ('2')
+		ELSE ('1')
+		END AS "GuV_Bilanz",
+	(
+		substring((
+				CASE 
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+						THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+						THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+					WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+						THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+					ELSE ((rtrim((((T1."nominal_account_number"))))))
+					END
+				), 1, 1)
+		) AS "Susa",
+	CASE 
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+			THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+			THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+		WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+			THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+		ELSE ((rtrim((((T1."nominal_account_number"))))))
+		END AS "Acct Nr",
+	(len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1) AS "Stellen_Konto_Nr"
+FROM "dbo"."nominal_accounts" T2,
+	(
+		"dbo"."journal_accountings" T1 LEFT JOIN "dbo"."accounts_characteristics" T3 ON (
+				(
+					(
+						(
+							(T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+							AND (T3."skr51_branch" = T1."skr51_branch")
+							)
+						AND (T3."skr51_make" = T1."skr51_make")
+						)
+					AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+					)
+				AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+				)
+			AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+		)
+WHERE (
+		(T2."nominal_account_number" = T1."nominal_account_number")
+		AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+		)
+	AND (
+		(T2."is_profit_loss_account" = 'N')
+		AND (
+			NOT (
+				(
+					rtrim((
+							CASE 
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 1)
+									THEN ('000' + (rtrim((((T1."nominal_account_number"))))))
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 2)
+									THEN ('00' + (rtrim((((T1."nominal_account_number"))))))
+								WHEN (((len((rtrim((((T1."nominal_account_number"))))) + 'Z') - 1)) = 3)
+									THEN ('0' + (rtrim((((T1."nominal_account_number"))))))
+								ELSE ((rtrim((((T1."nominal_account_number"))))))
+								END
+							))
+					) + ' - ' + T2."account_description"
+				) LIKE '9%'
+			)
+		)
+	-- order by "Nominal Account Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 207 - 75
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_imr.sql

@@ -1,82 +1,214 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.loc_belege_imr AS
 
-select T1."accounting_date" as "Accounting Date",
-	   T1."document_type" as "Document Type",
-	   (T1."document_number") as "Document Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."customer_number" as "Customer Number",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."document_date" as "Document Date",
-	   T1."posted_value" as "Posted Value",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_count" as "Posted Count",
-	   T1."branch_number" as "Branch Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."employee_number" as "Employee Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."journal_page" as "Journal Page",
-	   T1."journal_line" as "Journal Line",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."term_of_payment" as "Term Of Payment",
-	   CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END as "Posting Text",
-	   CASE WHEN (T1."vehicle_reference" IS NULL) THEN (' - ') ELSE (T1."vehicle_reference") END as "Vehicle Reference",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."vat_key" as "Vat Key",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   '0' + (((T1."skr51_branch"))) as "Betrieb",
-	   CASE WHEN (((CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) = 'Saldenübernahme CDK') or (T1."accounting_date" = convert(date, '2019-01-01'))) THEN (convert(date, '2019-12-01')) ELSE (T1."accounting_date") END as "Bookkeep Date",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, (CASE WHEN (((CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) = 'Saldenübernahme CDK') or (T1."accounting_date" = convert(date, '2019-01-01'))) THEN (convert(date, '2019-12-01')) ELSE (T1."accounting_date") END))))) <= 90) THEN ((convert(varchar(50), ((T1."document_number")))) + ' - ' + (CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) + '/' + (CASE WHEN (T1."vehicle_reference" IS NULL) THEN (' - ') ELSE (T1."vehicle_reference") END) + ' - ' + (left((((T1."employee_number")))))) ELSE ('Buchungen älter 90 Tage') END as "Text",
-	   '1' as "Mandant",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   (left((((T1."nominal_account_number"))),4)) + ' - ' + T2."account_description" as "Konto_mit_Bezeichnung",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   (left((((T1."nominal_account_number"))),1)) as "Susa",
-	   CASE WHEN (T1."skr51_cost_center" <> 0) THEN ((rtrim((((T1."nominal_account_number"))))) + '_' + (rtrim((((T1."skr51_cost_center")))))) ELSE ((((T1."nominal_account_number")))) END as "Acct Nr"
-from "dbo"."nominal_accounts" T2,
-	("dbo"."journal_accountings" T1 left outer join "dbo"."accounts_characteristics" T3 on (((((T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref") and (T3."skr51_branch" = T1."skr51_branch")) and (T3."skr51_make" = T1."skr51_make")) and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((T2."is_profit_loss_account" = 'J') and (not (CASE WHEN (T1."posting_text" IS NULL) THEN (' - ') ELSE (T1."posting_text") END) IN ('G&V-Abschlussbuchung')))
--- order by "Nominal Account Number" asc
+CREATE VIEW staging.loc_belege_imr
+AS
+SELECT T1."accounting_date" AS "Accounting Date",
+	T1."document_type" AS "Document Type",
+	(T1."document_number") AS "Document Number",
+	T1."position_in_document" AS "Position In Document",
+	T1."customer_number" AS "Customer Number",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."is_balanced" AS "Is Balanced",
+	T1."clearing_number" AS "Clearing Number",
+	T1."document_date" AS "Document Date",
+	T1."posted_value" AS "Posted Value",
+	T1."debit_or_credit" AS "Debit Or Credit",
+	T1."posted_count" AS "Posted Count",
+	T1."branch_number" AS "Branch Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."nominal_contra_account" AS "Nominal Contra Account",
+	T1."contra_account_text" AS "Contra Account Text",
+	T1."account_form_page_number" AS "Account Form Page Number",
+	T1."account_form_page_line" AS "Account Form Page Line",
+	T1."serial_number_each_month" AS "Serial Number Each Month",
+	T1."employee_number" AS "Employee Number",
+	T1."invoice_date" AS "Invoice Date",
+	T1."invoice_number" AS "Invoice Number",
+	T1."dunning_level" AS "Dunning Level",
+	T1."last_dunning_date" AS "Last Dunning Date",
+	T1."journal_page" AS "Journal Page",
+	T1."journal_line" AS "Journal Line",
+	T1."cash_discount" AS "Cash Discount",
+	T1."term_of_payment" AS "Term Of Payment",
+	CASE 
+		WHEN (T1."posting_text" IS NULL)
+			THEN (' - ')
+		ELSE (T1."posting_text")
+		END AS "Posting Text",
+	CASE 
+		WHEN (T1."vehicle_reference" IS NULL)
+			THEN (' - ')
+		ELSE (T1."vehicle_reference")
+		END AS "Vehicle Reference",
+	T1."vat_id_number" AS "Vat Id Number",
+	T1."account_statement_number" AS "Account Statement Number",
+	T1."account_statement_page" AS "Account Statement Page",
+	T1."vat_key" AS "Vat Key",
+	T1."days_for_cash_discount" AS "Days For Cash Discount",
+	T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+	T1."skr51_branch" AS "Skr51 Branch",
+	T1."skr51_make" AS "Skr51 Make",
+	T1."skr51_cost_center" AS "Skr51 Cost Center",
+	T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+	T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+	T1."previously_used_account_no" AS "Previously Used Account No",
+	T1."free_form_accounting_text" AS "Free Form Accounting Text",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN ('Neuwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+			THEN ('Gebrauchtwagen')
+		WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+			THEN ('Teile & Zubeh�r')
+		WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+			THEN ('Service')
+		WHEN (T1."skr51_cost_unit" = 0)
+			THEN ('Ohne Kostentr�ger')
+		ELSE NULL
+		END AS "Free Form Document Text",
+	T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+	T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+	'0' + (((T1."skr51_branch"))) AS "Betrieb",
+	CASE 
+		WHEN (
+				(
+					(
+						CASE 
+							WHEN (T1."posting_text" IS NULL)
+								THEN (' - ')
+							ELSE (T1."posting_text")
+							END
+						) = 'Saldenübernahme CDK'
+					)
+				OR (T1."accounting_date" = convert(DATE, '2019-01-01'))
+				)
+			THEN (convert(DATE, '2019-12-01'))
+		ELSE (T1."accounting_date")
+		END AS "Bookkeep Date",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+	CASE 
+		WHEN (
+				(
+					- 1 * datediff(day, (getdate()), (
+							convert(DATETIME, (
+									CASE 
+										WHEN (
+												(
+													(
+														CASE 
+															WHEN (T1."posting_text" IS NULL)
+																THEN (' - ')
+															ELSE (T1."posting_text")
+															END
+														) = 'Saldenübernahme CDK'
+													)
+												OR (T1."accounting_date" = convert(DATE, '2019-01-01'))
+												)
+											THEN (convert(DATE, '2019-12-01'))
+										ELSE (T1."accounting_date")
+										END
+									))
+							))
+					) <= 90
+				)
+			THEN (
+					(convert(VARCHAR(50), ((T1."document_number")))) + ' - ' + (
+						CASE 
+							WHEN (T1."posting_text" IS NULL)
+								THEN (' - ')
+							ELSE (T1."posting_text")
+							END
+						) + '/' + (
+						CASE 
+							WHEN (T1."vehicle_reference" IS NULL)
+								THEN (' - ')
+							ELSE (T1."vehicle_reference")
+							END
+						) + ' - ' + (left((((T1."employee_number")))))
+					)
+		ELSE ('Buchungen �lter 90 Tage')
+		END AS "Text",
+	'1' AS "Mandant",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_value" / 100 * - 1)
+		ELSE (T1."posted_value" / 100)
+		END AS "Betrag",
+	(left((((T1."nominal_account_number"))), 4)) + ' - ' + T2."account_description" AS "Konto_mit_Bezeichnung",
+	CASE 
+		WHEN (T1."debit_or_credit" = 'H')
+			THEN (T1."posted_count" / 100 * - 1)
+		ELSE (T1."posted_count" / 100)
+		END AS "Menge",
+	(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+	(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+	(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+	(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+	(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+	(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+	CASE 
+		WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+			THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+		ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+		END AS "Kostentr�ger",
+	CASE 
+		WHEN (T2."is_profit_loss_account" = 'J')
+			THEN ('2')
+		ELSE ('1')
+		END AS "GuV_Bilanz",
+	(left((((T1."nominal_account_number"))), 1)) AS "Susa",
+	CASE 
+		WHEN (T1."skr51_cost_center" <> 0)
+			THEN ((rtrim((((T1."nominal_account_number"))))) + '_' + (rtrim((((T1."skr51_cost_center"))))))
+		ELSE ((((T1."nominal_account_number"))))
+		END AS "Acct Nr"
+FROM "dbo"."nominal_accounts" T2,
+	(
+		"dbo"."journal_accountings" T1 LEFT JOIN "dbo"."accounts_characteristics" T3 ON (
+				(
+					(
+						(
+							(T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+							AND (T3."skr51_branch" = T1."skr51_branch")
+							)
+						AND (T3."skr51_make" = T1."skr51_make")
+						)
+					AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+					)
+				AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+				)
+			AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+		)
+WHERE (
+		(T2."nominal_account_number" = T1."nominal_account_number")
+		AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+		)
+	AND (
+		(T2."is_profit_loss_account" = 'J')
+		AND (
+			NOT (
+				CASE 
+					WHEN (T1."posting_text" IS NULL)
+						THEN (' - ')
+					ELSE (T1."posting_text")
+					END
+				) IN ('G&V-Abschlussbuchung')
+			)
+		)
+	-- order by "Nominal Account Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 1211 - 412
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_nw_gw_vk_imr.sql

@@ -1,419 +1,1218 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.loc_belege_nw_gw_vk_imr AS
 
-select "Accounting Date" as "Accounting Date",
-	   "Document Type" as "Document Type",
-	   "Document Number" as "Document Number",
-	   "Position In Document" as "Position In Document",
-	   (day((convert(datetime, (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))) - c142)) 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" as "Betrag",
-	   "Konto_mit_Bezeichnung" as "Konto_mit_Bezeichnung",
-	   "Menge_2" as "Menge_2",
-	   "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" as "Acct Nr",
-	   "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 ('FZG-Geschäft') ELSE ('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",
-	   SUM("Betrag") OVER (partition by "Vehicle Reference_berechnet") 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",
-	   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 ('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 ('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",
-	   "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",
-	   COUNT("Accounting Date") OVER (partition by "Vehicle Reference_berechnet") as "Menge_2",
-	   ("Menge_1" / COUNT("Accounting Date") OVER (partition by "Vehicle Reference_berechnet")) as "Menge",
-	   "Vehicle Reference_berechnet" as "Vehicle Reference_berechnet",
-	   "var. Kosten" as "var. Kosten",
-	   "Dealer Vehicle Type_Vehicles" as "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles" as "Dealer Vehicle Number_Vehicles"
-from 
-(select "Accounting Date",
-	   "Document Type",
-	   "Document Number",
-	   "Position In Document",
-	   "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_journal_accountings",
-	   "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",
-	   "Nom_Account_Is Profit Loss Account",
-	   "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Marke",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag",
-	   "Konto_mit_Bezeichnung",
-	   "Menge_2",
-	   "Stellen Cost Center",
-	   "KST",
-	   "Stellen Sales Channel",
-	   "Absatzkanal",
-	   "Stellen Cost Unit",
-	   "Kostenträger_mit_Null",
-	   "Kostenträger",
-	   "GuV_Bilanz",
-	   "Susa",
-	   "Nominal Account Number" as "Acct Nr",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   "FZG_ja_nein",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Invoice Date_Invoices",
-	   "Is Canceled_Invoices",
-	   "Vehicle Number_Invoices",
-	   "Invoice_Date_1",
-	   "Internal Number_Vehicles",
-	   "Vin_Vehicles",
-	   "Dealer Vehicle Type",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Make Number_Vehicles",
-	   "Description_Make",
-	   "Fabrikat",
-	   "Model Code_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Owner Number_Vehicles",
-	   "Holder Number_Vehicles",
-	   "First Name_Owner_Customer",
-	   "Family Name_Owner_Customer",
-	   "First Name_Holder_Customer",
-	   "Family Name_Holder_Customer",
-	   "Kunde",
-	   "Sales_Channel_FZG",
-	   "Rechtseinheit" as "Hauptbetrieb",
-	   "Betrieb" as "Standort_alt",
-	   "Salesman Number_Vehicles",
-	   "Employee Number_Employees_Salesman",
-	   "Name_Employees_Salesman",
-	   "Verkäufer",
-	   "Standort_FZG_Verkauf",
-	   "Standort",
-	   "FZG",
-	   "Konto_mit_Bezeichnung" as "Konto",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Body Paint Description" as "Farbe",
-	   "Erlös FZG",
-	   "Nachlass",
-	   "VAK FZG",
-	   "Erlös Zulass.",
-	   "VAK Zulass.",
-	   "Erlös Gar.",
-	   "VAK Gar.",
-	   "Erlös Nachr./Aufb.",
-	   "VAK Nachr./Aufb.",
-	   "VAK Fertigm.",
-	   "Erlös Überf.",
-	   "VAK Überf.",
-	   "Erlös Sonst._8900",
-	   "Erlös Sonst.",
-	   "VAK Sonst.",
-	   "VAK intern",
-	   "Erlös Prov.",
-	   "Boni/VK-Hilfen",
-	   "Erlös ges.",
-	   "VAK ges.",
-	   1 as "Menge_1",
-	   "Vehicle Reference_berechnet",
-	   "var. Kosten",
-	   "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles",
-	   c152 as c142,
-	   MAX("Vehicle Number_Invoices") OVER (partition by "Vehicle Reference_berechnet") as c143,
-	   (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 c144,
-	   (CASE WHEN ((SUM("FZG_ja_nein") OVER (partition by "Vehicle Reference_berechnet")) > 0) THEN ('FZG-Geschäft') ELSE ('Buchungen ohne FZG-Geschäft') END) as c145
-from 
-(select (left(T1."vehicle_reference",7)) + '_' + (right(T1."vehicle_reference",17)) as "Vehicle Reference_berechnet",
-	   (((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) as "Invoice Date_journal_accountings",
-	   T2."in_buy_invoice_no_date" as c152,
-	   T8."dealer_vehicle_number" as "Dealer Vehicle Number_Vehicles",
-	   T8."dealer_vehicle_type" as "Dealer Vehicle Type_Vehicles",
-	   CASE WHEN (T1."nominal_account_number" IN (4300,4560,4580,4530,4540,4590,4591,4690,4621,4622,4623,4500,4501,4502,4503,4504,5501)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "var. Kosten",
-	   (CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK ges.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös ges.",
-	   CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "Boni/VK-Hilfen",
-	   CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Prov.",
-	   CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK intern",
-	   (CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK Sonst.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös Sonst.",
-	   CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Sonst._8900",
-	   CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Fertigm.",
-	   CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK FZG",
-	   CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Nachlass",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös FZG",
-	   T8."body_paint_description" as "Body Paint Description",
-	   T8."body_paint_code" as "Body Paint Code",
-	   ((left((((T1."nominal_account_number"))),4)) + ' - ' + T4."account_description") as "Konto_mit_Bezeichnung",
-	   (left(((left(T1."vehicle_reference",7)) + '_' + (right(T1."vehicle_reference",17))),7)) + ' / ' + ((left((((T8."internal_number"))),5))) + ' - ' + T10."description" + ' - ' + (((left((((T8."holder_number"))),7)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END) + ' ' + T3."family_name")) as "FZG",
-	   CASE WHEN (T2."out_subsidiary" IS NULL) THEN ((('0' + (((T1."skr51_branch")))))) ELSE ('0' + (((T2."out_subsidiary")))) END as "Standort",
-	   T2."out_subsidiary" as "Standort_FZG_Verkauf",
-	   (left((((T8."salesman_number"))),4)) + ' - ' + T12."name" as "Verkäufer",
-	   T12."name" as "Name_Employees_Salesman",
-	   T12."employee_number" as "Employee Number_Employees_Salesman",
-	   T8."salesman_number" as "Salesman Number_Vehicles",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T7."is_canceled" <> '1')) THEN (T1."skr51_sales_channel") ELSE null END as "Sales_Channel_FZG",
-	   ((left((((T8."holder_number"))),7)) + ' - ' + (CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END) + ' ' + T3."family_name") as "Kunde",
-	   T3."family_name" as "Family Name_Holder_Customer",
-	   CASE WHEN (T3."first_name" IS NULL) THEN ('') ELSE (T3."first_name") END as "First Name_Holder_Customer",
-	   T11."family_name" as "Family Name_Owner_Customer",
-	   T11."first_name" as "First Name_Owner_Customer",
-	   T8."holder_number" as "Holder Number_Vehicles",
-	   T8."owner_number" as "Owner Number_Vehicles",
-	   (left((ucase(T10."description")),3)) as "Model",
-	   T10."description" as "Model_Detail",
-	   T10."model_code" as "Model Code_Models",
-	   CASE WHEN (((rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description") = '3 - Husqvarna') THEN ('Husqvarna') ELSE (T9."description") END as "Fabrikat",
-	   T9."description" as "Description_Make",
-	   T8."make_number" as "Make Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (left(T1."vehicle_reference",1)) as "Dealer Vehicle Type",
-	   T8."vin" as "Vin_Vehicles",
-	   T8."internal_number" as "Internal Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) THEN (((((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))))) ELSE null END as "Invoice_Date_1",
-	   T7."vehicle_number" as "Vehicle Number_Invoices",
-	   T7."is_canceled" as "Is Canceled_Invoices",
-	   T7."invoice_date" as "Invoice Date_Invoices",
-	   T7."invoice_number" as "Invoice Number_Invoices",
-	   T7."invoice_type" as "Invoice Type_Invoices",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END as "FZG_ja_nein",
-	   T6."document_type_description" as "Document Type Description",
-	   T6."document_type_in_journal" as "Document Type In Journal",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   (left((((T1."nominal_account_number"))),1)) as "Susa",
-	   CASE WHEN (T4."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name")) END as "Kostenträger",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T5."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T5."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge_2",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) <= 360) THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (rtrim((((T1."employee_number")))))) ELSE ('Buchungen älter 360 Tage') END as "Text",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description" as "Marke",
-	   T1."accounting_date" as "Accounting Date",
-	   T4."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T3."zip_code" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."position_in_document" as "Position In Document",
-	   T1."document_number" as "Document Number",
-	   T1."document_type" as "Document Type"
-from "nominal_accounts" T4,
-	(((((((((("journal_accountings" T1 left outer join "vehicles" T8 on (right(T1."vehicle_reference",17)) = T8."vin") left outer join "LOCOSOFT"."dbo"."dealer_vehicles" T2 on (T8."dealer_vehicle_type" = T2."dealer_vehicle_type") and (T8."dealer_vehicle_number" = T2."dealer_vehicle_number")) left outer join "customers_suppliers" T3 on T3."customer_number" = T8."holder_number") left outer join "accounts_characteristics" T5 on (((((T5."skr51_make" = T1."skr51_make") and (T5."skr51_cost_center" = T1."skr51_cost_center")) and (T5."skr51_sales_channel" = T1."skr51_sales_channel")) and (T5."skr51_cost_unit" = T1."skr51_cost_unit")) and (T5."skr51_branch" = T1."skr51_branch")) and (T5."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")) left outer join "document_types" T6 on T6."document_type_in_journal" = T1."document_type") left outer join "invoices" T7 on T7."invtype_invnr" = T1."invoice_number") left outer join "makes" T9 on T8."make_number" = T9."make_number") left outer join "models" T10 on (T8."make_number" = T10."make_number") and (T8."model_code" = T10."model_code")) left outer join "customers_suppliers" T11 on T11."customer_number" = T8."owner_number") left outer join "employees" T12 on (T8."salesman_number" <> 0) and (T8."salesman_number" = T12."salesman_number"))
-where ((T4."nominal_account_number" = T1."nominal_account_number") and (T4."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and (((T4."is_profit_loss_account" = 'J') and (T1."vehicle_reference" <> ' ')) and (T1."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,5001,5005,5006,5007,5008,5003,5004,4300,4560,4580,4530,4540,4590,4591,4690,4621,4622,4623,4500,4501,4502,4503,4504,5501,7220,7800,7850,7801,7802,7803,7804,7805,7806,7807,7820,7825,7827,7808,8030,7030,8870,8871,8872,8220,7220)))
-) D2
-) D1
-where (((((c143 = "Internal Number_Vehicles") or ("Internal Number_Vehicles" IS NULL)) or (c143 IS NULL)) and (c144 >= convert(date, '2017-01-01'))) and (c145 = 'FZG-Geschäft'))
--- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
+CREATE VIEW staging.loc_belege_nw_gw_vk_imr
+AS
+SELECT "Accounting Date" AS "Accounting Date",
+	"Document Type" AS "Document Type",
+	"Document Number" AS "Document Number",
+	"Position In Document" AS "Position In Document",
+	(
+		day((
+				convert(DATETIME, (
+						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
+						))
+				) - c142)
+		) 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" AS "Betrag",
+	"Konto_mit_Bezeichnung" AS "Konto_mit_Bezeichnung",
+	"Menge_2" AS "Menge_2",
+	"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" AS "Acct Nr",
+	"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 ('FZG-Gesch�ft')
+		ELSE ('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",
+	SUM("Betrag") OVER (PARTITION BY "Vehicle Reference_berechnet") 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",
+	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 ('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 ('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",
+	"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",
+	COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet") AS "Menge_2",
+	("Menge_1" / COUNT("Accounting Date") OVER (PARTITION BY "Vehicle Reference_berechnet")) AS "Menge",
+	"Vehicle Reference_berechnet" AS "Vehicle Reference_berechnet",
+	"var. Kosten" AS "var. Kosten",
+	"Dealer Vehicle Type_Vehicles" AS "Dealer Vehicle Type_Vehicles",
+	"Dealer Vehicle Number_Vehicles" AS "Dealer Vehicle Number_Vehicles"
+FROM (
+	SELECT "Accounting Date",
+		"Document Type",
+		"Document Number",
+		"Position In Document",
+		"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_journal_accountings",
+		"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",
+		"Nom_Account_Is Profit Loss Account",
+		"Rechtseinheit",
+		"Betrieb",
+		"Accounting Date" AS "Bookkeep Date",
+		"Marke",
+		"Text",
+		'1' AS "Mandant",
+		"Betrag",
+		"Konto_mit_Bezeichnung",
+		"Menge_2",
+		"Stellen Cost Center",
+		"KST",
+		"Stellen Sales Channel",
+		"Absatzkanal",
+		"Stellen Cost Unit",
+		"Kostentr�ger_mit_Null",
+		"Kostentr�ger",
+		"GuV_Bilanz",
+		"Susa",
+		"Nominal Account Number" AS "Acct Nr",
+		"Document Type In Journal",
+		"Document Type Description",
+		"FZG_ja_nein",
+		"Invoice Type_Invoices",
+		"Invoice Number_Invoices",
+		"Invoice Date_Invoices",
+		"Is Canceled_Invoices",
+		"Vehicle Number_Invoices",
+		"Invoice_Date_1",
+		"Internal Number_Vehicles",
+		"Vin_Vehicles",
+		"Dealer Vehicle Type",
+		"Fahrzeugtyp",
+		"Fahrzeugart",
+		"Make Number_Vehicles",
+		"Description_Make",
+		"Fabrikat",
+		"Model Code_Models",
+		"Model_Detail",
+		"Model",
+		"Owner Number_Vehicles",
+		"Holder Number_Vehicles",
+		"First Name_Owner_Customer",
+		"Family Name_Owner_Customer",
+		"First Name_Holder_Customer",
+		"Family Name_Holder_Customer",
+		"Kunde",
+		"Sales_Channel_FZG",
+		"Rechtseinheit" AS "Hauptbetrieb",
+		"Betrieb" AS "Standort_alt",
+		"Salesman Number_Vehicles",
+		"Employee Number_Employees_Salesman",
+		"Name_Employees_Salesman",
+		"Verk�ufer",
+		"Standort_FZG_Verkauf",
+		"Standort",
+		"FZG",
+		"Konto_mit_Bezeichnung" AS "Konto",
+		"Body Paint Code",
+		"Body Paint Description",
+		"Body Paint Description" AS "Farbe",
+		"Erl�s FZG",
+		"Nachlass",
+		"VAK FZG",
+		"Erl�s Zulass.",
+		"VAK Zulass.",
+		"Erl�s Gar.",
+		"VAK Gar.",
+		"Erl�s Nachr./Aufb.",
+		"VAK Nachr./Aufb.",
+		"VAK Fertigm.",
+		"Erl�s �berf.",
+		"VAK �berf.",
+		"Erl�s Sonst._8900",
+		"Erl�s Sonst.",
+		"VAK Sonst.",
+		"VAK intern",
+		"Erl�s Prov.",
+		"Boni/VK-Hilfen",
+		"Erl�s ges.",
+		"VAK ges.",
+		1 AS "Menge_1",
+		"Vehicle Reference_berechnet",
+		"var. Kosten",
+		"Dealer Vehicle Type_Vehicles",
+		"Dealer Vehicle Number_Vehicles",
+		c152 AS c142,
+		MAX("Vehicle Number_Invoices") OVER (PARTITION BY "Vehicle Reference_berechnet") AS c143,
+		(
+			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 c144,
+		(
+			CASE 
+				WHEN ((SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet")) > 0)
+					THEN ('FZG-Gesch�ft')
+				ELSE ('Buchungen ohne FZG-Gesch�ft')
+				END
+			) AS c145
+	FROM (
+		SELECT (left(T1."vehicle_reference", 7)) + '_' + (right(T1."vehicle_reference", 17)) AS "Vehicle Reference_berechnet",
+			(((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) AS "Invoice Date_journal_accountings",
+			T2."in_buy_invoice_no_date" AS c152,
+			T8."dealer_vehicle_number" AS "Dealer Vehicle Number_Vehicles",
+			T8."dealer_vehicle_type" AS "Dealer Vehicle Type_Vehicles",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (4300, 4560, 4580, 4530, 4540, 4590, 4591, 4690, 4621, 4622, 4623, 4500, 4501, 4502, 4503, 4504, 5501))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "var. Kosten",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7240))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					)
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7800, 7850))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) AS "VAK ges.",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8900))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					)
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) AS "Erl�s ges.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7800, 7850))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "Boni/VK-Hilfen",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Prov.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK intern",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7240))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END
+				) AS "VAK Sonst.",
+			(
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) + (
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8900))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END
+				) AS "Erl�s Sonst.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8900))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Sonst._8900",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7030))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK �berf.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8030))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s �berf.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7240))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Fertigm.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7220))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Nachr./Aufb.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8220))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Nachr./Aufb.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7210))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Gar.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8210))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Gar.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7200))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK Zulass.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8200))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s Zulass.",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								)
+							)
+				ELSE (0)
+				END AS "VAK FZG",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Nachlass",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+					THEN (
+							(
+								CASE 
+									WHEN (T1."debit_or_credit" = 'H')
+										THEN (T1."posted_value" / 100 * - 1)
+									ELSE (T1."posted_value" / 100)
+									END
+								) * - 1
+							)
+				ELSE (0)
+				END AS "Erl�s FZG",
+			T8."body_paint_description" AS "Body Paint Description",
+			T8."body_paint_code" AS "Body Paint Code",
+			((left((((T1."nominal_account_number"))), 4)) + ' - ' + T4."account_description") AS "Konto_mit_Bezeichnung",
+			(left(((left(T1."vehicle_reference", 7)) + '_' + (right(T1."vehicle_reference", 17))), 7)) + ' / ' + ((left((((T8."internal_number"))), 5))) + ' - ' + T10."description" + ' - ' + (
+				(
+					(left((((T8."holder_number"))), 7)) + ' - ' + (
+						CASE 
+							WHEN (T3."first_name" IS NULL)
+								THEN ('')
+							ELSE (T3."first_name")
+							END
+						) + ' ' + T3."family_name"
+					)
+				) AS "FZG",
+			CASE 
+				WHEN (T2."out_subsidiary" IS NULL)
+					THEN ((('0' + (((T1."skr51_branch"))))))
+				ELSE ('0' + (((T2."out_subsidiary"))))
+				END AS "Standort",
+			T2."out_subsidiary" AS "Standort_FZG_Verkauf",
+			(left((((T8."salesman_number"))), 4)) + ' - ' + T12."name" AS "Verk�ufer",
+			T12."name" AS "Name_Employees_Salesman",
+			T12."employee_number" AS "Employee Number_Employees_Salesman",
+			T8."salesman_number" AS "Salesman Number_Vehicles",
+			('0' + (((T1."skr51_branch")))) AS "Betrieb",
+			T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+			CASE 
+				WHEN (
+						(
+							(
+								CASE 
+									WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+										THEN (1)
+									ELSE (0)
+									END
+								) = 1
+							)
+						AND (T7."is_canceled" <> '1')
+						)
+					THEN (T1."skr51_sales_channel")
+				ELSE NULL
+				END AS "Sales_Channel_FZG",
+			(
+				(left((((T8."holder_number"))), 7)) + ' - ' + (
+					CASE 
+						WHEN (T3."first_name" IS NULL)
+							THEN ('')
+						ELSE (T3."first_name")
+						END
+					) + ' ' + T3."family_name"
+				) AS "Kunde",
+			T3."family_name" AS "Family Name_Holder_Customer",
+			CASE 
+				WHEN (T3."first_name" IS NULL)
+					THEN ('')
+				ELSE (T3."first_name")
+				END AS "First Name_Holder_Customer",
+			T11."family_name" AS "Family Name_Owner_Customer",
+			T11."first_name" AS "First Name_Owner_Customer",
+			T8."holder_number" AS "Holder Number_Vehicles",
+			T8."owner_number" AS "Owner Number_Vehicles",
+			(left((ucase(T10."description")), 3)) AS "Model",
+			T10."description" AS "Model_Detail",
+			T10."model_code" AS "Model Code_Models",
+			CASE 
+				WHEN (((rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description") = '3 - Husqvarna')
+					THEN ('Husqvarna')
+				ELSE (T9."description")
+				END AS "Fabrikat",
+			T9."description" AS "Description_Make",
+			T8."make_number" AS "Make Number_Vehicles",
+			CASE 
+				WHEN (
+						(
+							CASE 
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+									THEN ('NW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+									THEN ('VFW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+									THEN ('GW')
+								WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+									THEN ('TZ')
+								ELSE NULL
+								END
+							) IN ('VFW', 'TZ', 'NW')
+						)
+					THEN ('Neuwagen')
+				ELSE ('Gebrauchtwagen')
+				END AS "Fahrzeugart",
+			CASE 
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+					THEN ('NW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+					THEN ('VFW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+					THEN ('GW')
+				WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+					THEN ('TZ')
+				ELSE NULL
+				END AS "Fahrzeugtyp",
+			(left(T1."vehicle_reference", 1)) AS "Dealer Vehicle Type",
+			T8."vin" AS "Vin_Vehicles",
+			T8."internal_number" AS "Internal Number_Vehicles",
+			CASE 
+				WHEN (
+						(
+							CASE 
+								WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+									THEN (1)
+								ELSE (0)
+								END
+							) = 1
+						)
+					THEN (((((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2))))))))
+				ELSE NULL
+				END AS "Invoice_Date_1",
+			T7."vehicle_number" AS "Vehicle Number_Invoices",
+			T7."is_canceled" AS "Is Canceled_Invoices",
+			T7."invoice_date" AS "Invoice Date_Invoices",
+			T7."invoice_number" AS "Invoice Number_Invoices",
+			T7."invoice_type" AS "Invoice Type_Invoices",
+			CASE 
+				WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+					THEN (1)
+				ELSE (0)
+				END AS "FZG_ja_nein",
+			T6."document_type_description" AS "Document Type Description",
+			T6."document_type_in_journal" AS "Document Type In Journal",
+			T1."nominal_account_number" AS "Nominal Account Number",
+			(left((((T1."nominal_account_number"))), 1)) AS "Susa",
+			CASE 
+				WHEN (T4."is_profit_loss_account" = 'J')
+					THEN ('2')
+				ELSE ('1')
+				END AS "GuV_Bilanz",
+			CASE 
+				WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+					THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name")))
+				ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name"))
+				END AS "Kostentr�ger",
+			(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T5."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+			(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+			(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T5."skr51_sales_channel_name" AS "Absatzkanal",
+			(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+			(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T5."skr51_cost_center_name" AS "KST",
+			(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_count" / 100 * - 1)
+				ELSE (T1."posted_count" / 100)
+				END AS "Menge_2",
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_value" / 100 * - 1)
+				ELSE (T1."posted_value" / 100)
+				END AS "Betrag",
+			CASE 
+				WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) <= 360)
+					THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (rtrim((((T1."employee_number"))))))
+				ELSE ('Buchungen �lter 360 Tage')
+				END AS "Text",
+			(rtrim((((T1."skr51_make"))))) + ' - ' + T5."skr51_make_description" AS "Marke",
+			T1."accounting_date" AS "Accounting Date",
+			T4."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+			CASE 
+				WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+					THEN ('Neuwagen')
+				WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+					THEN ('Gebrauchtwagen')
+				WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+					THEN ('Teile & Zubeh�r')
+				WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+					THEN ('Service')
+				WHEN (T1."skr51_cost_unit" = 0)
+					THEN ('Ohne Kostentr�ger')
+				ELSE NULL
+				END AS "Free Form Document Text",
+			T3."zip_code" AS "Free Form Accounting Text",
+			T1."previously_used_account_no" AS "Previously Used Account No",
+			T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+			T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+			T1."skr51_cost_center" AS "Skr51 Cost Center",
+			T1."skr51_make" AS "Skr51 Make",
+			T1."skr51_branch" AS "Skr51 Branch",
+			T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+			T1."days_for_cash_discount" AS "Days For Cash Discount",
+			T1."vat_key" AS "Vat Key",
+			T1."account_statement_page" AS "Account Statement Page",
+			T1."account_statement_number" AS "Account Statement Number",
+			T1."vat_id_number" AS "Vat Id Number",
+			T1."vehicle_reference" AS "Vehicle Reference",
+			T1."posting_text" AS "Posting Text",
+			T1."term_of_payment" AS "Term Of Payment",
+			T1."cash_discount" AS "Cash Discount",
+			T1."journal_line" AS "Journal Line",
+			T1."journal_page" AS "Journal Page",
+			T1."last_dunning_date" AS "Last Dunning Date",
+			T1."dunning_level" AS "Dunning Level",
+			T1."invoice_number" AS "Invoice Number",
+			T1."employee_number" AS "Employee Number",
+			T1."serial_number_each_month" AS "Serial Number Each Month",
+			T1."account_form_page_line" AS "Account Form Page Line",
+			T1."account_form_page_number" AS "Account Form Page Number",
+			T1."contra_account_text" AS "Contra Account Text",
+			T1."nominal_contra_account" AS "Nominal Contra Account",
+			T1."customer_contra_account" AS "Customer Contra Account",
+			T1."branch_number" AS "Branch Number",
+			T1."posted_count" AS "Posted Count",
+			T1."debit_or_credit" AS "Debit Or Credit",
+			T1."posted_value" AS "Posted Value",
+			T1."document_date" AS "Document Date",
+			T1."clearing_number" AS "Clearing Number",
+			T1."is_balanced" AS "Is Balanced",
+			T1."position_in_document" AS "Position In Document",
+			T1."document_number" AS "Document Number",
+			T1."document_type" AS "Document Type"
+		FROM "nominal_accounts" T4,
+			(
+				(
+					(
+						(
+							(
+								(
+									(
+										(
+											(
+												(
+													"journal_accountings" T1 LEFT JOIN "vehicles" T8 ON (right(T1."vehicle_reference", 17)) = T8."vin"
+													) LEFT JOIN "LOCOSOFT"."dbo"."dealer_vehicles" T2 ON (T8."dealer_vehicle_type" = T2."dealer_vehicle_type")
+													AND (T8."dealer_vehicle_number" = T2."dealer_vehicle_number")
+												) LEFT JOIN "customers_suppliers" T3 ON T3."customer_number" = T8."holder_number"
+											) LEFT JOIN "accounts_characteristics" T5 ON (
+												(
+													(
+														(
+															(T5."skr51_make" = T1."skr51_make")
+															AND (T5."skr51_cost_center" = T1."skr51_cost_center")
+															)
+														AND (T5."skr51_sales_channel" = T1."skr51_sales_channel")
+														)
+													AND (T5."skr51_cost_unit" = T1."skr51_cost_unit")
+													)
+												AND (T5."skr51_branch" = T1."skr51_branch")
+												)
+											AND (T5."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+										) LEFT JOIN "document_types" T6 ON T6."document_type_in_journal" = T1."document_type"
+									) LEFT JOIN "invoices" T7 ON T7."invtype_invnr" = T1."invoice_number"
+								) LEFT JOIN "makes" T9 ON T8."make_number" = T9."make_number"
+							) LEFT JOIN "models" T10 ON (T8."make_number" = T10."make_number")
+							AND (T8."model_code" = T10."model_code")
+						) LEFT JOIN "customers_suppliers" T11 ON T11."customer_number" = T8."owner_number"
+					) LEFT JOIN "employees" T12 ON (T8."salesman_number" <> 0)
+					AND (T8."salesman_number" = T12."salesman_number")
+				)
+		WHERE (
+				(T4."nominal_account_number" = T1."nominal_account_number")
+				AND (T4."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+				)
+			AND (
+				(
+					(T4."is_profit_loss_account" = 'J')
+					AND (T1."vehicle_reference" <> ' ')
+					)
+				AND (T1."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, 5001, 5005, 5006, 5007, 5008, 5003, 5004, 4300, 4560, 4580, 4530, 4540, 4590, 4591, 4690, 4621, 4622, 4623, 4500, 4501, 4502, 4503, 4504, 5501, 7220, 7800, 7850, 7801, 7802, 7803, 7804, 7805, 7806, 7807, 7820, 7825, 7827, 7808, 8030, 7030, 8870, 8871, 8872, 8220, 7220))
+				)
+		) D2
+	) D1
+WHERE (
+		(
+			(
+				(
+					(c143 = "Internal Number_Vehicles")
+					OR ("Internal Number_Vehicles" IS NULL)
+					)
+				OR (c143 IS NULL)
+				)
+			AND (c144 >= convert(DATE, '2017-01-01'))
+			)
+		AND (c145 = 'FZG-Gesch�ft')
+		)
+	-- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 1334 - 551
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.loc_belege_nw_gw_vk_stk_fibu_imr.sql

@@ -1,558 +1,1341 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.loc_belege_nw_gw_vk_stk_fibu_imr 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 ('FZG-Geschäft') ELSE ('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 ('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 ('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",
-	   '0' + (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 "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_journal_accountings",
-	   "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",
-	   "Nom_Account_Is Profit Loss Account",
-	   "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Marke",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag_ori",
-	   "Konto_mit_Bezeichnung",
-	   "Menge",
-	   "Stellen Cost Center",
-	   "KST",
-	   "Stellen Sales Channel",
-	   "Absatzkanal",
-	   "Stellen Cost Unit",
-	   "Kostenträger_mit_Null",
-	   "Kostenträger",
-	   "GuV_Bilanz",
-	   '3' as "Susa",
-	   "Acct Nr_ori",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   "FZG_ja_nein",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Invoice Date_Invoices",
-	   "Is Canceled_Invoices",
-	   "Vehicle Number_Invoices",
-	   "Invoice_Date_1",
-	   "Internal Number_Vehicles",
-	   "Vin_Vehicles",
-	   "Dealer Vehicle Type",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Make Number_Vehicles",
-	   "Description_Make",
-	   "Description_Make" as "Fabrikat",
-	   "Model Code_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Owner Number_Vehicles",
-	   "Holder Number_Vehicles",
-	   "First Name_Owner_Customer",
-	   "Family Name_Owner_Customer",
-	   "First Name_Holder_Customer",
-	   "Family Name_Holder_Customer",
-	   "Kunde",
-	   "Sales_Channel_FZG",
-	   "Rechtseinheit" as "Hauptbetrieb",
-	   "Betrieb" as "Standort_alt",
-	   "Salesman Number_Vehicles",
-	   "Employee Number_Employees_Salesman",
-	   "Name_Employees_Salesman",
-	   "Verkäufer",
-	   "Standort_FZG_Verkauf",
-	   "FZG",
-	   "Konto_mit_Bezeichnung" as "Konto",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Body Paint Description" as "Farbe",
-	   "Erlös FZG",
-	   "Nachlass",
-	   "VAK FZG",
-	   "Erlös Zulass.",
-	   "VAK Zulass.",
-	   "Erlös Gar.",
-	   "VAK Gar.",
-	   "Erlös Nachr./Aufb.",
-	   "VAK Nachr./Aufb.",
-	   "VAK Fertigm.",
-	   "Erlös Überf.",
-	   "VAK Überf.",
-	   "Erlös Sonst._8900",
-	   "Erlös Sonst.",
-	   "VAK Sonst.",
-	   "VAK intern",
-	   "Erlös Prov.",
-	   "Boni/VK-Hilfen",
-	   "Erlös ges.",
-	   "VAK ges.",
-	   1 as "Menge_1",
-	   "Vehicle Reference_berechnet",
-	   "Acct Nr",
-	   "Dealer Vehicle Type_Vehicles",
-	   "Dealer Vehicle Number_Vehicles",
-	   MAX("Vehicle Number_Invoices") OVER (partition by "Vehicle Reference_berechnet") as c142,
-	   (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 c143,
-	   (CASE WHEN ((SUM("FZG_ja_nein") OVER (partition by "Vehicle Reference_berechnet")) > 0) THEN ('FZG-Geschäft') ELSE ('Buchungen ohne FZG-Geschäft') END) as c144
-from 
-(select (left(T1."vehicle_reference",8)) + '_' + (right(T1."vehicle_reference",17)) as "Vehicle Reference_berechnet",
-	   (((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) as "Invoice Date_journal_accountings",
-	   T6."dealer_vehicle_number" as "Dealer Vehicle Number_Vehicles",
-	   T6."dealer_vehicle_type" as "Dealer Vehicle Type_Vehicles",
-	   (ltrim(((left((((T1."nominal_account_number"))),4)) + '_STK'))) as "Acct Nr",
-	   (CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK ges.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + ((CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END)) + (CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös ges.",
-	   CASE WHEN (T1."nominal_account_number" IN (7800,7850)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "Boni/VK-Hilfen",
-	   CASE WHEN (T1."nominal_account_number" IN (8800,8801,8870,8820,8830)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Prov.",
-	   CASE WHEN (T1."nominal_account_number" IN (5001,5005,5006,5007,5008,5003,5004)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK intern",
-	   (CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END) as "VAK Sonst.",
-	   (CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) + (CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END) as "Erlös Sonst.",
-	   CASE WHEN (T1."nominal_account_number" IN (8900)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Sonst._8900",
-	   CASE WHEN (T1."nominal_account_number" IN (7030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (8030)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Überf.",
-	   CASE WHEN (T1."nominal_account_number" IN (7240)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Fertigm.",
-	   CASE WHEN (T1."nominal_account_number" IN (7220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (8220)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Nachr./Aufb.",
-	   CASE WHEN (T1."nominal_account_number" IN (7210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (8210)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Gar.",
-	   CASE WHEN (T1."nominal_account_number" IN (7200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (8200)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös Zulass.",
-	   CASE WHEN (T1."nominal_account_number" IN (7000,7010,7100,7101,7110,7111,7120,7510)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END)) ELSE (0) END as "VAK FZG",
-	   CASE WHEN (T1."nominal_account_number" IN (8001,8011,5002)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Nachlass",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8111,8112,8510,8928,8934)) THEN ((CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) * -1) ELSE (0) END as "Erlös FZG",
-	   T6."body_paint_description" as "Body Paint Description",
-	   T6."body_paint_code" as "Body Paint Code",
-	   ((left((((T1."nominal_account_number"))),4)) + ' - ' + T2."account_description") as "Konto_mit_Bezeichnung",
-	   (right(T6."vin",5)) + ' / ' + ((left((((T6."internal_number"))),5))) + ' - ' + T8."description" + ' - ' + (CASE WHEN (T10."first_name" <> ' ') THEN ((left((((T6."holder_number"))),6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((left((((T6."holder_number"))),6)) + ' - ' + T10."family_name") END) as "FZG",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_branch") ELSE null END as "Standort_FZG_Verkauf",
-	   (left((((T6."salesman_number"))),3)) + ' - ' + T11."name" as "Verkäufer",
-	   T11."name" as "Name_Employees_Salesman",
-	   T11."employee_number" as "Employee Number_Employees_Salesman",
-	   T6."salesman_number" as "Salesman Number_Vehicles",
-	   ('0' + (((T1."skr51_branch")))) as "Betrieb",
-	   T1."subsidiary_to_company_ref" as "Rechtseinheit",
-	   CASE WHEN (((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) and (T5."is_canceled" <> '1')) THEN (T1."skr51_sales_channel") ELSE null END as "Sales_Channel_FZG",
-	   CASE WHEN (T10."first_name" <> ' ') THEN ((left((((T6."holder_number"))),6)) + ' - ' + T10."first_name" + ' ' + T10."family_name") ELSE ((left((((T6."holder_number"))),6)) + ' - ' + T10."family_name") END as "Kunde",
-	   T10."family_name" as "Family Name_Holder_Customer",
-	   T10."first_name" as "First Name_Holder_Customer",
-	   T9."family_name" as "Family Name_Owner_Customer",
-	   T9."first_name" as "First Name_Owner_Customer",
-	   T6."holder_number" as "Holder Number_Vehicles",
-	   T6."owner_number" as "Owner Number_Vehicles",
-	   (left((ucase(T8."description")),3)) as "Model",
-	   T8."description" as "Model_Detail",
-	   T8."model_code" as "Model Code_Models",
-	   T7."description" as "Description_Make",
-	   T6."make_number" as "Make Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN (((left(T1."vehicle_reference",1))) IN ('N')) THEN ('NW') WHEN (((left(T1."vehicle_reference",1))) IN ('V')) THEN ('VFW') WHEN (((left(T1."vehicle_reference",1))) IN ('G','D')) THEN ('GW') WHEN (((left(T1."vehicle_reference",1))) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (left(T1."vehicle_reference",1)) as "Dealer Vehicle Type",
-	   T6."vin" as "Vin_Vehicles",
-	   T6."internal_number" as "Internal Number_Vehicles",
-	   CASE WHEN ((CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END) = 1) THEN (((((nconvert((left(T1."invoice_date",4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))))) ELSE null END as "Invoice_Date_1",
-	   T5."vehicle_number" as "Vehicle Number_Invoices",
-	   T5."is_canceled" as "Is Canceled_Invoices",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_type" as "Invoice Type_Invoices",
-	   CASE WHEN (T1."nominal_account_number" IN (8000,8010,8100,8110,8510)) THEN (1) ELSE (0) END as "FZG_ja_nein",
-	   T4."document_type_description" as "Document Type Description",
-	   T4."document_type_in_journal" as "Document Type In Journal",
-	   (left((((T1."nominal_account_number"))),4)) + '_STK' as "Acct Nr_ori",
-	   CASE WHEN (T2."is_profit_loss_account" = 'J') THEN ('2') ELSE ('1') END as "GuV_Bilanz",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))) ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")) END as "Kostenträger",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") as "Kostenträger_mit_Null",
-	   (len((((T1."skr51_cost_unit"))) + 'Z') - 1) as "Stellen Cost Unit",
-	   (rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" as "Absatzkanal",
-	   (len((((T1."skr51_sales_channel"))) + 'Z') - 1) as "Stellen Sales Channel",
-	   (rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" as "KST",
-	   (len((((T1."skr51_cost_center"))) + 'Z') - 1) as "Stellen Cost Center",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END as "Betrag_ori",
-	   CASE WHEN ((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) <= 360) THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (((T1."employee_number")))) ELSE ('Buchungen älter 360 Tage') END as "Text",
-	   (rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" as "Marke",
-	   T1."accounting_date" as "Accounting Date",
-	   T2."is_profit_loss_account" as "Nom_Account_Is Profit Loss Account",
-	   CASE WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49) THEN ('Neuwagen') WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59) THEN ('Gebrauchtwagen') WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69) THEN ('Teile & Zubehör') WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79) THEN ('Service') WHEN (T1."skr51_cost_unit" = 0) THEN ('Ohne Kostenträger') ELSE null END as "Free Form Document Text",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."customer_number" as "Customer Number",
-	   T1."position_in_document" as "Position In Document",
-	   T1."document_number" as "Document Number",
-	   T1."document_type" as "Document Type"
-from "nominal_accounts" T2,
-	((((((((("journal_accountings" T1 left outer join "accounts_characteristics" T3 on (((((T3."skr51_make" = T1."skr51_make") and (T3."skr51_cost_center" = T1."skr51_cost_center")) and (T3."skr51_sales_channel" = T1."skr51_sales_channel")) and (T3."skr51_cost_unit" = T1."skr51_cost_unit")) and (T3."skr51_branch" = T1."skr51_branch")) and (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")) left outer join "document_types" T4 on T4."document_type_in_journal" = T1."document_type") left outer join "invoices" T5 on T5."invtype_invnr" = T1."invoice_number") left outer join "vehicles" T6 on (right(T1."vehicle_reference",17)) = T6."vin") left outer join "makes" T7 on T6."make_number" = T7."make_number") left outer join "models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code")) left outer join "customers_suppliers" T9 on T9."customer_number" = T6."owner_number") left outer join "customers_suppliers" T10 on T10."customer_number" = T6."holder_number") left outer join "employees" T11 on (T6."salesman_number" <> 0) and (T6."salesman_number" = T11."salesman_number"))
-where ((T2."nominal_account_number" = T1."nominal_account_number") and (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref"))
-and ((((T2."is_profit_loss_account" = 'J') and (T1."vehicle_reference" <> ' ')) and (T1."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 (T1."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 = 'FZG-Geschäft')) and (c143 = "Invoice_Date_1"))
-) D4
--- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
+CREATE VIEW staging.loc_belege_nw_gw_vk_stk_fibu_imr
+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 ('FZG-Gesch�ft')
+			ELSE ('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 ('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 ('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",
+		'0' + (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 "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_journal_accountings",
+			"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",
+			"Nom_Account_Is Profit Loss Account",
+			"Rechtseinheit",
+			"Betrieb",
+			"Accounting Date" AS "Bookkeep Date",
+			"Marke",
+			"Text",
+			'1' AS "Mandant",
+			"Betrag_ori",
+			"Konto_mit_Bezeichnung",
+			"Menge",
+			"Stellen Cost Center",
+			"KST",
+			"Stellen Sales Channel",
+			"Absatzkanal",
+			"Stellen Cost Unit",
+			"Kostentr�ger_mit_Null",
+			"Kostentr�ger",
+			"GuV_Bilanz",
+			'3' AS "Susa",
+			"Acct Nr_ori",
+			"Document Type In Journal",
+			"Document Type Description",
+			"FZG_ja_nein",
+			"Invoice Type_Invoices",
+			"Invoice Number_Invoices",
+			"Invoice Date_Invoices",
+			"Is Canceled_Invoices",
+			"Vehicle Number_Invoices",
+			"Invoice_Date_1",
+			"Internal Number_Vehicles",
+			"Vin_Vehicles",
+			"Dealer Vehicle Type",
+			"Fahrzeugtyp",
+			"Fahrzeugart",
+			"Make Number_Vehicles",
+			"Description_Make",
+			"Description_Make" AS "Fabrikat",
+			"Model Code_Models",
+			"Model_Detail",
+			"Model",
+			"Owner Number_Vehicles",
+			"Holder Number_Vehicles",
+			"First Name_Owner_Customer",
+			"Family Name_Owner_Customer",
+			"First Name_Holder_Customer",
+			"Family Name_Holder_Customer",
+			"Kunde",
+			"Sales_Channel_FZG",
+			"Rechtseinheit" AS "Hauptbetrieb",
+			"Betrieb" AS "Standort_alt",
+			"Salesman Number_Vehicles",
+			"Employee Number_Employees_Salesman",
+			"Name_Employees_Salesman",
+			"Verk�ufer",
+			"Standort_FZG_Verkauf",
+			"FZG",
+			"Konto_mit_Bezeichnung" AS "Konto",
+			"Body Paint Code",
+			"Body Paint Description",
+			"Body Paint Description" AS "Farbe",
+			"Erl�s FZG",
+			"Nachlass",
+			"VAK FZG",
+			"Erl�s Zulass.",
+			"VAK Zulass.",
+			"Erl�s Gar.",
+			"VAK Gar.",
+			"Erl�s Nachr./Aufb.",
+			"VAK Nachr./Aufb.",
+			"VAK Fertigm.",
+			"Erl�s �berf.",
+			"VAK �berf.",
+			"Erl�s Sonst._8900",
+			"Erl�s Sonst.",
+			"VAK Sonst.",
+			"VAK intern",
+			"Erl�s Prov.",
+			"Boni/VK-Hilfen",
+			"Erl�s ges.",
+			"VAK ges.",
+			1 AS "Menge_1",
+			"Vehicle Reference_berechnet",
+			"Acct Nr",
+			"Dealer Vehicle Type_Vehicles",
+			"Dealer Vehicle Number_Vehicles",
+			MAX("Vehicle Number_Invoices") OVER (PARTITION BY "Vehicle Reference_berechnet") AS c142,
+			(
+				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 c143,
+			(
+				CASE 
+					WHEN ((SUM("FZG_ja_nein") OVER (PARTITION BY "Vehicle Reference_berechnet")) > 0)
+						THEN ('FZG-Gesch�ft')
+					ELSE ('Buchungen ohne FZG-Gesch�ft')
+					END
+				) AS c144
+		FROM (
+			SELECT (left(T1."vehicle_reference", 8)) + '_' + (right(T1."vehicle_reference", 17)) AS "Vehicle Reference_berechnet",
+				(((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2)))))) AS "Invoice Date_journal_accountings",
+				T6."dealer_vehicle_number" AS "Dealer Vehicle Number_Vehicles",
+				T6."dealer_vehicle_type" AS "Dealer Vehicle Type_Vehicles",
+				(ltrim(((left((((T1."nominal_account_number"))), 4)) + '_STK'))) AS "Acct Nr",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7240))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (7030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											)
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7800, 7850))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS "VAK ges.",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					(
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8200))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8210))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8220))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8030))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						) + (
+						CASE 
+							WHEN (T1."nominal_account_number" IN (8900))
+								THEN (
+										(
+											CASE 
+												WHEN (T1."debit_or_credit" = 'H')
+													THEN (T1."posted_value" / 100 * - 1)
+												ELSE (T1."posted_value" / 100)
+												END
+											) * - 1
+										)
+							ELSE (0)
+							END
+						)
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS "Erl�s ges.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7800, 7850))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "Boni/VK-Hilfen",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8800, 8801, 8870, 8820, 8830))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Prov.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (5001, 5005, 5006, 5007, 5008, 5003, 5004))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK intern",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7240))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (7030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										)
+									)
+						ELSE (0)
+						END
+					) AS "VAK Sonst.",
+				(
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8200))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8210))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8220))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8030))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) + (
+					CASE 
+						WHEN (T1."nominal_account_number" IN (8900))
+							THEN (
+									(
+										CASE 
+											WHEN (T1."debit_or_credit" = 'H')
+												THEN (T1."posted_value" / 100 * - 1)
+											ELSE (T1."posted_value" / 100)
+											END
+										) * - 1
+									)
+						ELSE (0)
+						END
+					) AS "Erl�s Sonst.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8900))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Sonst._8900",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK �berf.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8030))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s �berf.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7240))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Fertigm.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Nachr./Aufb.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8220))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Nachr./Aufb.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Gar.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8210))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Gar.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK Zulass.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8200))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s Zulass.",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (7000, 7010, 7100, 7101, 7110, 7111, 7120, 7510))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									)
+								)
+					ELSE (0)
+					END AS "VAK FZG",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8001, 8011, 5002))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Nachlass",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8111, 8112, 8510, 8928, 8934))
+						THEN (
+								(
+									CASE 
+										WHEN (T1."debit_or_credit" = 'H')
+											THEN (T1."posted_value" / 100 * - 1)
+										ELSE (T1."posted_value" / 100)
+										END
+									) * - 1
+								)
+					ELSE (0)
+					END AS "Erl�s FZG",
+				T6."body_paint_description" AS "Body Paint Description",
+				T6."body_paint_code" AS "Body Paint Code",
+				((left((((T1."nominal_account_number"))), 4)) + ' - ' + T2."account_description") AS "Konto_mit_Bezeichnung",
+				(right(T6."vin", 5)) + ' / ' + ((left((((T6."internal_number"))), 5))) + ' - ' + T8."description" + ' - ' + (
+					CASE 
+						WHEN (T10."first_name" <> ' ')
+							THEN ((left((((T6."holder_number"))), 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+						ELSE ((left((((T6."holder_number"))), 6)) + ' - ' + T10."family_name")
+						END
+					) AS "FZG",
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_branch")
+					ELSE NULL
+					END AS "Standort_FZG_Verkauf",
+				(left((((T6."salesman_number"))), 3)) + ' - ' + T11."name" AS "Verk�ufer",
+				T11."name" AS "Name_Employees_Salesman",
+				T11."employee_number" AS "Employee Number_Employees_Salesman",
+				T6."salesman_number" AS "Salesman Number_Vehicles",
+				('0' + (((T1."skr51_branch")))) AS "Betrieb",
+				T1."subsidiary_to_company_ref" AS "Rechtseinheit",
+				CASE 
+					WHEN (
+							(
+								(
+									CASE 
+										WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+											THEN (1)
+										ELSE (0)
+										END
+									) = 1
+								)
+							AND (T5."is_canceled" <> '1')
+							)
+						THEN (T1."skr51_sales_channel")
+					ELSE NULL
+					END AS "Sales_Channel_FZG",
+				CASE 
+					WHEN (T10."first_name" <> ' ')
+						THEN ((left((((T6."holder_number"))), 6)) + ' - ' + T10."first_name" + ' ' + T10."family_name")
+					ELSE ((left((((T6."holder_number"))), 6)) + ' - ' + T10."family_name")
+					END AS "Kunde",
+				T10."family_name" AS "Family Name_Holder_Customer",
+				T10."first_name" AS "First Name_Holder_Customer",
+				T9."family_name" AS "Family Name_Owner_Customer",
+				T9."first_name" AS "First Name_Owner_Customer",
+				T6."holder_number" AS "Holder Number_Vehicles",
+				T6."owner_number" AS "Owner Number_Vehicles",
+				(left((ucase(T8."description")), 3)) AS "Model",
+				T8."description" AS "Model_Detail",
+				T8."model_code" AS "Model Code_Models",
+				T7."description" AS "Description_Make",
+				T6."make_number" AS "Make Number_Vehicles",
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+										THEN ('NW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+										THEN ('VFW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+										THEN ('GW')
+									WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+										THEN ('TZ')
+									ELSE NULL
+									END
+								) IN ('VFW', 'TZ', 'NW')
+							)
+						THEN ('Neuwagen')
+					ELSE ('Gebrauchtwagen')
+					END AS "Fahrzeugart",
+				CASE 
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('N'))
+						THEN ('NW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('V'))
+						THEN ('VFW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('G', 'D'))
+						THEN ('GW')
+					WHEN (((left(T1."vehicle_reference", 1))) IN ('T'))
+						THEN ('TZ')
+					ELSE NULL
+					END AS "Fahrzeugtyp",
+				(left(T1."vehicle_reference", 1)) AS "Dealer Vehicle Type",
+				T6."vin" AS "Vin_Vehicles",
+				T6."internal_number" AS "Internal Number_Vehicles",
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+										THEN (1)
+									ELSE (0)
+									END
+								) = 1
+							)
+						THEN (((((nconvert((left(T1."invoice_date", 4)) + (substring(T1."invoice_date", 6, 2)) + (substring(T1."invoice_date", 9, 2))))))))
+					ELSE NULL
+					END AS "Invoice_Date_1",
+				T5."vehicle_number" AS "Vehicle Number_Invoices",
+				T5."is_canceled" AS "Is Canceled_Invoices",
+				T5."invoice_date" AS "Invoice Date_Invoices",
+				T5."invoice_number" AS "Invoice Number_Invoices",
+				T5."invoice_type" AS "Invoice Type_Invoices",
+				CASE 
+					WHEN (T1."nominal_account_number" IN (8000, 8010, 8100, 8110, 8510))
+						THEN (1)
+					ELSE (0)
+					END AS "FZG_ja_nein",
+				T4."document_type_description" AS "Document Type Description",
+				T4."document_type_in_journal" AS "Document Type In Journal",
+				(left((((T1."nominal_account_number"))), 4)) + '_STK' AS "Acct Nr_ori",
+				CASE 
+					WHEN (T2."is_profit_loss_account" = 'J')
+						THEN ('2')
+					ELSE ('1')
+					END AS "GuV_Bilanz",
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN (((rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name")))
+					ELSE ((rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name"))
+					END AS "Kostentr�ger",
+				(rtrim((((T1."skr51_make"))))) + ' - ' + (rtrim((((T1."skr51_cost_unit"))))) + ' - ' + (T3."skr51_cost_unit_name") AS "Kostentr�ger_mit_Null",
+				(len((((T1."skr51_cost_unit"))) + 'Z') - 1) AS "Stellen Cost Unit",
+				(rtrim((((T1."skr51_sales_channel"))))) + ' - ' + T3."skr51_sales_channel_name" AS "Absatzkanal",
+				(len((((T1."skr51_sales_channel"))) + 'Z') - 1) AS "Stellen Sales Channel",
+				(rtrim((((T1."skr51_cost_center"))))) + ' - ' + T3."skr51_cost_center_name" AS "KST",
+				(len((((T1."skr51_cost_center"))) + 'Z') - 1) AS "Stellen Cost Center",
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_count" / 100 * - 1)
+					ELSE (T1."posted_count" / 100)
+					END AS "Menge",
+				CASE 
+					WHEN (T1."debit_or_credit" = 'H')
+						THEN (T1."posted_value" / 100 * - 1)
+					ELSE (T1."posted_value" / 100)
+					END AS "Betrag_ori",
+				CASE 
+					WHEN ((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) <= 360)
+						THEN (T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + (((T1."employee_number"))))
+					ELSE ('Buchungen �lter 360 Tage')
+					END AS "Text",
+				(rtrim((((T1."skr51_make"))))) + ' - ' + T3."skr51_make_description" AS "Marke",
+				T1."accounting_date" AS "Accounting Date",
+				T2."is_profit_loss_account" AS "Nom_Account_Is Profit Loss Account",
+				CASE 
+					WHEN (T1."skr51_cost_unit" BETWEEN 1 AND 49)
+						THEN ('Neuwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 50 AND 59)
+						THEN ('Gebrauchtwagen')
+					WHEN (T1."skr51_cost_unit" BETWEEN 60 AND 69)
+						THEN ('Teile & Zubeh�r')
+					WHEN (T1."skr51_cost_unit" BETWEEN 70 AND 79)
+						THEN ('Service')
+					WHEN (T1."skr51_cost_unit" = 0)
+						THEN ('Ohne Kostentr�ger')
+					ELSE NULL
+					END AS "Free Form Document Text",
+				T1."free_form_accounting_text" AS "Free Form Accounting Text",
+				T1."previously_used_account_no" AS "Previously Used Account No",
+				T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+				T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+				T1."skr51_cost_center" AS "Skr51 Cost Center",
+				T1."skr51_make" AS "Skr51 Make",
+				T1."skr51_branch" AS "Skr51 Branch",
+				T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+				T1."days_for_cash_discount" AS "Days For Cash Discount",
+				T1."vat_key" AS "Vat Key",
+				T1."account_statement_page" AS "Account Statement Page",
+				T1."account_statement_number" AS "Account Statement Number",
+				T1."vat_id_number" AS "Vat Id Number",
+				T1."vehicle_reference" AS "Vehicle Reference",
+				T1."posting_text" AS "Posting Text",
+				T1."term_of_payment" AS "Term Of Payment",
+				T1."cash_discount" AS "Cash Discount",
+				T1."journal_line" AS "Journal Line",
+				T1."journal_page" AS "Journal Page",
+				T1."last_dunning_date" AS "Last Dunning Date",
+				T1."dunning_level" AS "Dunning Level",
+				T1."invoice_number" AS "Invoice Number",
+				T1."employee_number" AS "Employee Number",
+				T1."serial_number_each_month" AS "Serial Number Each Month",
+				T1."account_form_page_line" AS "Account Form Page Line",
+				T1."account_form_page_number" AS "Account Form Page Number",
+				T1."contra_account_text" AS "Contra Account Text",
+				T1."nominal_contra_account" AS "Nominal Contra Account",
+				T1."customer_contra_account" AS "Customer Contra Account",
+				T1."branch_number" AS "Branch Number",
+				T1."posted_count" AS "Posted Count",
+				T1."debit_or_credit" AS "Debit Or Credit",
+				T1."posted_value" AS "Posted Value",
+				T1."document_date" AS "Document Date",
+				T1."clearing_number" AS "Clearing Number",
+				T1."is_balanced" AS "Is Balanced",
+				T1."nominal_account_number" AS "Nominal Account Number",
+				T1."customer_number" AS "Customer Number",
+				T1."position_in_document" AS "Position In Document",
+				T1."document_number" AS "Document Number",
+				T1."document_type" AS "Document Type"
+			FROM "nominal_accounts" T2,
+				(
+					(
+						(
+							(
+								(
+									(
+										(
+											(
+												(
+													"journal_accountings" T1 LEFT JOIN "accounts_characteristics" T3 ON (
+															(
+																(
+																	(
+																		(T3."skr51_make" = T1."skr51_make")
+																		AND (T3."skr51_cost_center" = T1."skr51_cost_center")
+																		)
+																	AND (T3."skr51_sales_channel" = T1."skr51_sales_channel")
+																	)
+																AND (T3."skr51_cost_unit" = T1."skr51_cost_unit")
+																)
+															AND (T3."skr51_branch" = T1."skr51_branch")
+															)
+														AND (T3."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+													) LEFT JOIN "document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
+												) LEFT JOIN "invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
+											) LEFT JOIN "vehicles" T6 ON (right(T1."vehicle_reference", 17)) = T6."vin"
+										) LEFT JOIN "makes" T7 ON T6."make_number" = T7."make_number"
+									) LEFT JOIN "models" T8 ON (T6."make_number" = T8."make_number")
+									AND (T6."model_code" = T8."model_code")
+								) LEFT JOIN "customers_suppliers" T9 ON T9."customer_number" = T6."owner_number"
+							) LEFT JOIN "customers_suppliers" T10 ON T10."customer_number" = T6."holder_number"
+						) LEFT JOIN "employees" T11 ON (T6."salesman_number" <> 0)
+						AND (T6."salesman_number" = T11."salesman_number")
+					)
+			WHERE (
+					(T2."nominal_account_number" = T1."nominal_account_number")
+					AND (T2."subsidiary_to_company_ref" = T1."subsidiary_to_company_ref")
+					)
+				AND (
+					(
+						(
+							(T2."is_profit_loss_account" = 'J')
+							AND (T1."vehicle_reference" <> ' ')
+							)
+						AND (T1."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 (T1."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 = 'FZG-Gesch�ft')
+				)
+			AND (c143 = "Invoice_Date_1")
+			)
+	) D4
+	-- order by "Vehicle Reference_berechnet" asc,"Vehicle Reference" asc,"Nominal Account Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 484 - 258
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.nw_gw_be_auf_nw_gw_bestand_cat_imr.sql

@@ -1,265 +1,491 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.nw_gw_be_auf_nw_gw_bestand_cat_imr AS
 
-select convert(date, '1900-01-01') as "Accounting Date",
-	   '' as "Document Type",
-	   0 as "Document Number_2",
-	   0 as "Position In Document",
-	   0 as "Customer Number",
-	   0 as "Nominal Account Number",
-	   '' as "Is Balanced",
-	   0 as "Clearing Number",
-	   convert(date, '1900-01-01') as "Document Date",
-	   0 as "Posted Value",
-	   '' as "Debit Or Credit",
-	   0 as "Posted Count",
-	   0 as "Branch Number",
-	   0 as "Customer Contra Account",
-	   0 as "Nominal Contra Account",
-	   '' as "Contra Account Text",
-	   0 as "Account Form Page Number",
-	   0 as "Account Form Page Line",
-	   0 as "Serial Number Each Month",
-	   0 as "Employee Number",
-	   convert(date, '1900-01-01') as "Invoice Date",
-	   '' as "Invoice Number",
-	   '' as "Dunning Level",
-	   convert(date, '1900-01-01') as "Last Dunning Date",
-	   0 as "Journal Page",
-	   0 as "Journal Line",
-	   0 as "Cash Discount",
-	   0 as "Term Of Payment",
-	   '' as "Posting Text",
-	   '' as "Vehicle Reference",
-	   '' as "Vat Id Number",
-	   0 as "Account Statement Number",
-	   0 as "Account Statement Page",
-	   '' as "Vat Key",
-	   0 as "Days For Cash Discount",
-	   convert(date, '1900-01-01') as "Day Of Actual Accounting",
-	   0 as "Skr51 Branch",
-	   0 as "Skr51 Make",
-	   0 as "Skr51 Cost Center",
-	   0 as "Skr51 Sales Channel",
-	   0 as "Skr51 Cost Unit",
-	   '' as "Previously Used Account No",
-	   "Free Form Accounting Text",
-	   "Free Form Document Text",
-	   '' as "Nom_Account_Is Profit Loss Account",
-	   '1' as "Rechtseinheit",
-	   '' as "Betrieb",
-	   convert(date, '1900-01-01') as "Bookkeep Date",
-	   '' as "Marke",
-	   '' as "Text",
-	   '1' as "Mandant",
-	   0 as "Betrag",
-	   '' as "Konto_mit_Bezeichnung",
-	   0 as "Menge_FIBU",
-	   0 as "Stellen Cost Center",
-	   '' as "KST",
-	   0 as "Stellen Sales Channel",
-	   '' as "Absatzkanal",
-	   0 as "Stellen Cost Unit",
-	   '' as "Kostenträger_mit_Null",
-	   '' as "Kostenträger",
-	   '' as "GuV_Bilanz",
-	   '' as "Susa",
-	   '' as "Acct Nr",
-	   0 as "Stellen_Konto_Nr",
-	   SUM(0) OVER (partition by "Vehicle_Reference_berechnet") as "Summe_Betrag",
-	   "Internal Number_ori",
-	   "Vin",
-	   "License Plate",
-	   "License Plate Country",
-	   "License Plate Season",
-	   "Make Number",
-	   "Free Form Make Text",
-	   "Model Code",
-	   "Free Form Model Text",
-	   "Is Roadworthy",
-	   "Is Customer Vehicle",
-	   "Dealer Vehicle Type",
-	   "Dealer Vehicle Number",
-	   "First Registration Date",
-	   "Readmission Date",
-	   "Next Service Date",
-	   "Next Service Km",
-	   "Next Service Miles",
-	   "Production Year",
-	   "Owner Number",
-	   "Holder Number",
-	   "Previous Owner Number",
-	   "Previous Owner Counter",
-	   "Last Holder Change Date",
-	   "German Kba Hsn",
-	   "German Kba Tsn",
-	   "Austria Nat Code",
-	   "Is Prefer Km",
-	   "Mileage Km",
-	   "Mileage Miles",
-	   "Odometer Reading Date",
-	   "Engine Number",
-	   "Gear Number",
-	   "Unloaded Weight",
-	   "Gross Vehicle Weight",
-	   "Power Kw",
-	   "Cubic Capacity",
-	   "Is All Accidents Repaired",
-	   "Accidents Counter",
-	   "Has Tyre Pressure Sensor",
-	   "Carkey Number",
-	   "Internal Source Flag",
-	   "Emission Code",
-	   "First Sold Country",
-	   "First Sold Dealer Code",
-	   "Body Paint Code",
-	   "Body Paint Description",
-	   "Is Body Paint Metallic",
-	   "Interior Paint Code",
-	   "Interior Paint Description",
-	   "Trim Code",
-	   "Trim Description",
-	   "Fine Dust Label",
-	   "Internal Assignment",
-	   "Ricambi Free Input",
-	   "Document Number",
-	   "Salesman Number",
-	   "Sale Date",
-	   "Next Emission Test Date",
-	   "Next General Inspection Date",
-	   "Next Rust Inspection Date",
-	   "Next Exceptional Inspection Da",
-	   "Last Change Date",
-	   "Last Change Employee No",
-	   "Created Date",
-	   "Created Employee No",
-	   "Last Change Subsidiary",
-	   MIN(convert(date, '1900-01-01')) OVER (partition by "Vehicle_Reference_berechnet") as "Minimum_Accounting_Date",
-	   "Hauptbetrieb",
-	   "Standort",
-	   "Description_Makes",
-	   "Description_Makes" as "Fabrikat",
-	   "Description_Models",
-	   "Model_Detail",
-	   "Model",
-	   "Fahrzeugtyp",
-	   "Fahrzeugart",
-	   "Body Paint Description" as "Farbe",
-	   "Customer Number_Vorbesitzer",
-	   "First Name_Vorbesitzer",
-	   "Family Name_Vorbesitzer",
-	   "Vorbesitzer",
-	   "First Registration Date" as "Zulassungsdatum",
-	   1 as "Menge_1",
-	   COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet") as "Menge_2",
-	   1 / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet")) as "Menge",
-	   "Einsatz",
-	   CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END as "Standtage_Berechnung",
-	   (c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet")) as "Standtage",
-	   CASE WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 0 AND 30) THEN ('0 - 30 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 31 AND 60) THEN ('31 - 60 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 61 AND 90) THEN ('61 - 90 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 91 AND 180) THEN ('91 - 180 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) BETWEEN 181 AND 360) THEN ('181 - 360 Tage') WHEN ((CASE WHEN (c170 IS NULL) THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))))) ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (partition by "Vehicle_Reference_berechnet"))) END) > 360) THEN ('> 360 Tage') ELSE null END as "Standtagestaffel",
-	   (getdate()) as "Heute",
-	   "FZG",
-	   "Vehicle_Reference_berechnet",
-	   "Internal Number_ori" as "Internal Number"
-from 
-(select ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) as "Vehicle_Reference_berechnet",
-	   T2."internal_number" as "Internal Number_ori",
-	   ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) + ' - ' + (substring((CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END), 1, 170)) as "FZG",
-	   -1 * datediff(day, (getdate()), T1."in_buy_invoice_no_date") as c169,
-	   T1."in_buy_invoice_no_date" as c170,
-	   -1 * datediff(day, (getdate()), T1."in_arrival_date") as c171,
-	   T1."calc_basic_charge" + T1."calc_accessory" + T1."calc_extra_expenses" + T1."calc_usage_value_encr_external" + T1."calc_usage_value_encr_internal" + T1."calc_usage_value_encr_other" + T1."calc_total_writedown" + T1."calc_commission_for_arranging" + T1."calc_cost_internal_invoices" + T1."calc_cost_other" + T1."calc_returns_workshop" + (T1."calc_sales_aid" + T1."calc_sales_aid_finish" + T1."calc_sales_aid_bonus") * -1 as "Einsatz",
-	   T2."first_registration_date" as "First Registration Date",
-	   (left((((T5."customer_number"))),7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" as "Vorbesitzer",
-	   T5."family_name" as "Family Name_Vorbesitzer",
-	   T5."first_name" as "First Name_Vorbesitzer",
-	   T5."customer_number" as "Customer Number_Vorbesitzer",
-	   T2."body_paint_description" as "Body Paint Description",
-	   CASE WHEN ((CASE WHEN ((left(T2."dealer_vehicle_type",1)) IN ('N')) THEN ('NW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('V')) THEN ('VFW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('D')) THEN ('GW diff.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('G')) THEN ('GW regel.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('T')) THEN ('TZ') ELSE null END) IN ('VFW','TZ','NW')) THEN ('Neuwagen') ELSE ('Gebrauchtwagen') END as "Fahrzeugart",
-	   CASE WHEN ((left(T2."dealer_vehicle_type",1)) IN ('N')) THEN ('NW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('V')) THEN ('VFW') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('D')) THEN ('GW diff.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('G')) THEN ('GW regel.') WHEN ((left(T2."dealer_vehicle_type",1)) IN ('T')) THEN ('TZ') ELSE null END as "Fahrzeugtyp",
-	   (substring((upper((CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END))), 1, 3)) as "Model",
-	   CASE WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) IS NULL) THEN ('keine Angabe') WHEN ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END) LIKE '???%') THEN ('???') ELSE ((CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END)) END as "Model_Detail",
-	   CASE WHEN (T4."description" = 'DIVERSE') THEN (T2."free_form_model_text") ELSE (T4."description") END as "Description_Models",
-	   T3."description" as "Description_Makes",
-	   '0' + (((T1."in_subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."in_subsidiary")))) = '015                                                                                                                                                                                                                                                            ') THEN ('15') ELSE (T1."client_db") END as "Hauptbetrieb",
-	   T2."last_change_subsidiary" as "Last Change Subsidiary",
-	   T2."created_employee_no" as "Created Employee No",
-	   T2."created_date" as "Created Date",
-	   T2."last_change_employee_no" as "Last Change Employee No",
-	   T2."last_change_date" as "Last Change Date",
-	   T2."next_exceptional_inspection_da" as "Next Exceptional Inspection Da",
-	   T2."next_rust_inspection_date" as "Next Rust Inspection Date",
-	   T2."next_general_inspection_date" as "Next General Inspection Date",
-	   T2."next_emission_test_date" as "Next Emission Test Date",
-	   T2."sale_date" as "Sale Date",
-	   T2."salesman_number" as "Salesman Number",
-	   T2."document_number" as "Document Number",
-	   T2."ricambi_free_input" as "Ricambi Free Input",
-	   T2."internal_assignment" as "Internal Assignment",
-	   T2."fine_dust_label" as "Fine Dust Label",
-	   T2."trim_description" as "Trim Description",
-	   T2."trim_code" as "Trim Code",
-	   T2."interior_paint_description" as "Interior Paint Description",
-	   T2."interior_paint_code" as "Interior Paint Code",
-	   T2."is_body_paint_metallic" as "Is Body Paint Metallic",
-	   T2."body_paint_code" as "Body Paint Code",
-	   T2."first_sold_dealer_code" as "First Sold Dealer Code",
-	   T2."first_sold_country" as "First Sold Country",
-	   T2."emission_code" as "Emission Code",
-	   T2."internal_source_flag" as "Internal Source Flag",
-	   T2."carkey_number" as "Carkey Number",
-	   T2."has_tyre_pressure_sensor" as "Has Tyre Pressure Sensor",
-	   T2."accidents_counter" as "Accidents Counter",
-	   T2."is_all_accidents_repaired" as "Is All Accidents Repaired",
-	   T2."cubic_capacity" as "Cubic Capacity",
-	   T2."power_kw" as "Power Kw",
-	   T2."gross_vehicle_weight" as "Gross Vehicle Weight",
-	   T2."unloaded_weight" as "Unloaded Weight",
-	   T2."gear_number" as "Gear Number",
-	   T2."engine_number" as "Engine Number",
-	   T2."odometer_reading_date" as "Odometer Reading Date",
-	   T2."mileage_miles" as "Mileage Miles",
-	   T2."mileage_km" as "Mileage Km",
-	   T2."is_prefer_km" as "Is Prefer Km",
-	   T2."austria_nat_code" as "Austria Nat Code",
-	   T2."german_kba_tsn" as "German Kba Tsn",
-	   T2."german_kba_hsn" as "German Kba Hsn",
-	   T2."last_holder_change_date" as "Last Holder Change Date",
-	   T2."previous_owner_counter" as "Previous Owner Counter",
-	   T2."previous_owner_number" as "Previous Owner Number",
-	   T2."holder_number" as "Holder Number",
-	   T2."owner_number" as "Owner Number",
-	   T2."production_year" as "Production Year",
-	   T2."next_service_miles" as "Next Service Miles",
-	   T2."next_service_km" as "Next Service Km",
-	   T2."next_service_date" as "Next Service Date",
-	   T2."readmission_date" as "Readmission Date",
-	   T2."dealer_vehicle_number" as "Dealer Vehicle Number",
-	   T2."dealer_vehicle_type" as "Dealer Vehicle Type",
-	   T2."is_customer_vehicle" as "Is Customer Vehicle",
-	   T2."is_roadworthy" as "Is Roadworthy",
-	   T2."free_form_model_text" as "Free Form Model Text",
-	   T2."model_code" as "Model Code",
-	   T2."free_form_make_text" as "Free Form Make Text",
-	   T2."make_number" as "Make Number",
-	   T2."license_plate_season" as "License Plate Season",
-	   T2."license_plate_country" as "License Plate Country",
-	   T2."license_plate" as "License Plate",
-	   T2."vin" as "Vin",
-	   (rtrim((((T1."dealer_vehicle_number"))))) as "Free Form Document Text",
-	   T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))) as "Free Form Accounting Text"
-from (((("dbo"."vehicles" T2 left outer join "dbo"."makes" T3 on T2."make_number" = T3."make_number") left outer join "dbo"."models" T4 on (T2."make_number" = T4."make_number") and (T2."model_code" = T4."model_code")) left outer join "dbo"."customers_suppliers" T5 on T2."previous_owner_number" = T5."customer_number") left outer join "LOCOSOFT"."dbo"."dealer_vehicles" T1 on (T1."dealer_vehicle_type" = T2."dealer_vehicle_type") and (T1."dealer_vehicle_number" = T2."dealer_vehicle_number"))
-where ((T1."in_arrival_date" IS NOT NULL) and (T1."out_invoice_number" IS NULL))
--- order by "Vehicle_Reference_berechnet" asc
-) D1
+CREATE VIEW staging.nw_gw_be_auf_nw_gw_bestand_cat_imr
+AS
+SELECT convert(DATE, '1900-01-01') AS "Accounting Date",
+	'' AS "Document Type",
+	0 AS "Document Number_2",
+	0 AS "Position In Document",
+	0 AS "Customer Number",
+	0 AS "Nominal Account Number",
+	'' AS "Is Balanced",
+	0 AS "Clearing Number",
+	convert(DATE, '1900-01-01') AS "Document Date",
+	0 AS "Posted Value",
+	'' AS "Debit Or Credit",
+	0 AS "Posted Count",
+	0 AS "Branch Number",
+	0 AS "Customer Contra Account",
+	0 AS "Nominal Contra Account",
+	'' AS "Contra Account Text",
+	0 AS "Account Form Page Number",
+	0 AS "Account Form Page Line",
+	0 AS "Serial Number Each Month",
+	0 AS "Employee Number",
+	convert(DATE, '1900-01-01') AS "Invoice Date",
+	'' AS "Invoice Number",
+	'' AS "Dunning Level",
+	convert(DATE, '1900-01-01') AS "Last Dunning Date",
+	0 AS "Journal Page",
+	0 AS "Journal Line",
+	0 AS "Cash Discount",
+	0 AS "Term Of Payment",
+	'' AS "Posting Text",
+	'' AS "Vehicle Reference",
+	'' AS "Vat Id Number",
+	0 AS "Account Statement Number",
+	0 AS "Account Statement Page",
+	'' AS "Vat Key",
+	0 AS "Days For Cash Discount",
+	convert(DATE, '1900-01-01') AS "Day Of Actual Accounting",
+	0 AS "Skr51 Branch",
+	0 AS "Skr51 Make",
+	0 AS "Skr51 Cost Center",
+	0 AS "Skr51 Sales Channel",
+	0 AS "Skr51 Cost Unit",
+	'' AS "Previously Used Account No",
+	"Free Form Accounting Text",
+	"Free Form Document Text",
+	'' AS "Nom_Account_Is Profit Loss Account",
+	'1' AS "Rechtseinheit",
+	'' AS "Betrieb",
+	convert(DATE, '1900-01-01') AS "Bookkeep Date",
+	'' AS "Marke",
+	'' AS "Text",
+	'1' AS "Mandant",
+	0 AS "Betrag",
+	'' AS "Konto_mit_Bezeichnung",
+	0 AS "Menge_FIBU",
+	0 AS "Stellen Cost Center",
+	'' AS "KST",
+	0 AS "Stellen Sales Channel",
+	'' AS "Absatzkanal",
+	0 AS "Stellen Cost Unit",
+	'' AS "Kostentr�ger_mit_Null",
+	'' AS "Kostentr�ger",
+	'' AS "GuV_Bilanz",
+	'' AS "Susa",
+	'' AS "Acct Nr",
+	0 AS "Stellen_Konto_Nr",
+	SUM(0) OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Summe_Betrag",
+	"Internal Number_ori",
+	"Vin",
+	"License Plate",
+	"License Plate Country",
+	"License Plate Season",
+	"Make Number",
+	"Free Form Make Text",
+	"Model Code",
+	"Free Form Model Text",
+	"Is Roadworthy",
+	"Is Customer Vehicle",
+	"Dealer Vehicle Type",
+	"Dealer Vehicle Number",
+	"First Registration Date",
+	"Readmission Date",
+	"Next Service Date",
+	"Next Service Km",
+	"Next Service Miles",
+	"Production Year",
+	"Owner Number",
+	"Holder Number",
+	"Previous Owner Number",
+	"Previous Owner Counter",
+	"Last Holder Change Date",
+	"German Kba Hsn",
+	"German Kba Tsn",
+	"Austria Nat Code",
+	"Is Prefer Km",
+	"Mileage Km",
+	"Mileage Miles",
+	"Odometer Reading Date",
+	"Engine Number",
+	"Gear Number",
+	"Unloaded Weight",
+	"Gross Vehicle Weight",
+	"Power Kw",
+	"Cubic Capacity",
+	"Is All Accidents Repaired",
+	"Accidents Counter",
+	"Has Tyre Pressure Sensor",
+	"Carkey Number",
+	"Internal Source Flag",
+	"Emission Code",
+	"First Sold Country",
+	"First Sold Dealer Code",
+	"Body Paint Code",
+	"Body Paint Description",
+	"Is Body Paint Metallic",
+	"Interior Paint Code",
+	"Interior Paint Description",
+	"Trim Code",
+	"Trim Description",
+	"Fine Dust Label",
+	"Internal Assignment",
+	"Ricambi Free Input",
+	"Document Number",
+	"Salesman Number",
+	"Sale Date",
+	"Next Emission Test Date",
+	"Next General Inspection Date",
+	"Next Rust Inspection Date",
+	"Next Exceptional Inspection Da",
+	"Last Change Date",
+	"Last Change Employee No",
+	"Created Date",
+	"Created Employee No",
+	"Last Change Subsidiary",
+	MIN(convert(DATE, '1900-01-01')) OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Minimum_Accounting_Date",
+	"Hauptbetrieb",
+	"Standort",
+	"Description_Makes",
+	"Description_Makes" AS "Fabrikat",
+	"Description_Models",
+	"Model_Detail",
+	"Model",
+	"Fahrzeugtyp",
+	"Fahrzeugart",
+	"Body Paint Description" AS "Farbe",
+	"Customer Number_Vorbesitzer",
+	"First Name_Vorbesitzer",
+	"Family Name_Vorbesitzer",
+	"Vorbesitzer",
+	"First Registration Date" AS "Zulassungsdatum",
+	1 AS "Menge_1",
+	COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet") AS "Menge_2",
+	1 / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")) AS "Menge",
+	"Einsatz",
+	CASE 
+		WHEN (c170 IS NULL)
+			THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+		ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+		END AS "Standtage_Berechnung",
+	(c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")) AS "Standtage",
+	CASE 
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 0 AND 30
+				)
+			THEN ('0 - 30 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 31 AND 60
+				)
+			THEN ('31 - 60 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 61 AND 90
+				)
+			THEN ('61 - 90 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 91 AND 180
+				)
+			THEN ('91 - 180 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) BETWEEN 181 AND 360
+				)
+			THEN ('181 - 360 Tage')
+		WHEN (
+				(
+					CASE 
+						WHEN (c170 IS NULL)
+							THEN ((((c171) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))))
+						ELSE ((c169) / (COUNT("Dealer Vehicle Number") OVER (PARTITION BY "Vehicle_Reference_berechnet")))
+						END
+					) > 360
+				)
+			THEN ('> 360 Tage')
+		ELSE NULL
+		END AS "Standtagestaffel",
+	(getdate()) AS "Heute",
+	"FZG",
+	"Vehicle_Reference_berechnet",
+	"Internal Number_ori" AS "Internal Number"
+FROM (
+	SELECT ((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) AS "Vehicle_Reference_berechnet",
+		T2."internal_number" AS "Internal Number_ori",
+		((T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))))) + ' - ' + (
+			substring((
+					CASE 
+						WHEN (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									) IS NULL
+								)
+							THEN ('keine Angabe')
+						WHEN (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									) LIKE '???%'
+								)
+							THEN ('???')
+						ELSE (
+								(
+									CASE 
+										WHEN (T4."description" = 'DIVERSE')
+											THEN (T2."free_form_model_text")
+										ELSE (T4."description")
+										END
+									)
+								)
+						END
+					), 1, 170)
+			) AS "FZG",
+		- 1 * datediff(day, (getdate()), T1."in_buy_invoice_no_date") AS c169,
+		T1."in_buy_invoice_no_date" AS c170,
+		- 1 * datediff(day, (getdate()), T1."in_arrival_date") AS c171,
+		T1."calc_basic_charge" + T1."calc_accessory" + T1."calc_extra_expenses" + T1."calc_usage_value_encr_external" + T1."calc_usage_value_encr_internal" + T1."calc_usage_value_encr_other" + T1."calc_total_writedown" + T1."calc_commission_for_arranging" + T1."calc_cost_internal_invoices" + T1."calc_cost_other" + T1."calc_returns_workshop" + (T1."calc_sales_aid" + T1."calc_sales_aid_finish" + T1."calc_sales_aid_bonus") * - 1 AS "Einsatz",
+		T2."first_registration_date" AS "First Registration Date",
+		(left((((T5."customer_number"))), 7)) + ' - ' + T5."first_name" + ' ' + T5."family_name" AS "Vorbesitzer",
+		T5."family_name" AS "Family Name_Vorbesitzer",
+		T5."first_name" AS "First Name_Vorbesitzer",
+		T5."customer_number" AS "Customer Number_Vorbesitzer",
+		T2."body_paint_description" AS "Body Paint Description",
+		CASE 
+			WHEN (
+					(
+						CASE 
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('N'))
+								THEN ('NW')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('V'))
+								THEN ('VFW')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('D'))
+								THEN ('GW diff.')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('G'))
+								THEN ('GW regel.')
+							WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('T'))
+								THEN ('TZ')
+							ELSE NULL
+							END
+						) IN ('VFW', 'TZ', 'NW')
+					)
+				THEN ('Neuwagen')
+			ELSE ('Gebrauchtwagen')
+			END AS "Fahrzeugart",
+		CASE 
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('N'))
+				THEN ('NW')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('V'))
+				THEN ('VFW')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('D'))
+				THEN ('GW diff.')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('G'))
+				THEN ('GW regel.')
+			WHEN ((left(T2."dealer_vehicle_type", 1)) IN ('T'))
+				THEN ('TZ')
+			ELSE NULL
+			END AS "Fahrzeugtyp",
+		(
+			substring((
+					upper((
+							CASE 
+								WHEN (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											) IS NULL
+										)
+									THEN ('keine Angabe')
+								WHEN (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											) LIKE '???%'
+										)
+									THEN ('???')
+								ELSE (
+										(
+											CASE 
+												WHEN (T4."description" = 'DIVERSE')
+													THEN (T2."free_form_model_text")
+												ELSE (T4."description")
+												END
+											)
+										)
+								END
+							))
+					), 1, 3)
+			) AS "Model",
+		CASE 
+			WHEN (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						) IS NULL
+					)
+				THEN ('keine Angabe')
+			WHEN (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						) LIKE '???%'
+					)
+				THEN ('???')
+			ELSE (
+					(
+						CASE 
+							WHEN (T4."description" = 'DIVERSE')
+								THEN (T2."free_form_model_text")
+							ELSE (T4."description")
+							END
+						)
+					)
+			END AS "Model_Detail",
+		CASE 
+			WHEN (T4."description" = 'DIVERSE')
+				THEN (T2."free_form_model_text")
+			ELSE (T4."description")
+			END AS "Description_Models",
+		T3."description" AS "Description_Makes",
+		'0' + (((T1."in_subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."in_subsidiary")))) = '015                                                                                                                                                                                                                                                            ')
+				THEN ('15')
+			ELSE (T1."client_db")
+			END AS "Hauptbetrieb",
+		T2."last_change_subsidiary" AS "Last Change Subsidiary",
+		T2."created_employee_no" AS "Created Employee No",
+		T2."created_date" AS "Created Date",
+		T2."last_change_employee_no" AS "Last Change Employee No",
+		T2."last_change_date" AS "Last Change Date",
+		T2."next_exceptional_inspection_da" AS "Next Exceptional Inspection Da",
+		T2."next_rust_inspection_date" AS "Next Rust Inspection Date",
+		T2."next_general_inspection_date" AS "Next General Inspection Date",
+		T2."next_emission_test_date" AS "Next Emission Test Date",
+		T2."sale_date" AS "Sale Date",
+		T2."salesman_number" AS "Salesman Number",
+		T2."document_number" AS "Document Number",
+		T2."ricambi_free_input" AS "Ricambi Free Input",
+		T2."internal_assignment" AS "Internal Assignment",
+		T2."fine_dust_label" AS "Fine Dust Label",
+		T2."trim_description" AS "Trim Description",
+		T2."trim_code" AS "Trim Code",
+		T2."interior_paint_description" AS "Interior Paint Description",
+		T2."interior_paint_code" AS "Interior Paint Code",
+		T2."is_body_paint_metallic" AS "Is Body Paint Metallic",
+		T2."body_paint_code" AS "Body Paint Code",
+		T2."first_sold_dealer_code" AS "First Sold Dealer Code",
+		T2."first_sold_country" AS "First Sold Country",
+		T2."emission_code" AS "Emission Code",
+		T2."internal_source_flag" AS "Internal Source Flag",
+		T2."carkey_number" AS "Carkey Number",
+		T2."has_tyre_pressure_sensor" AS "Has Tyre Pressure Sensor",
+		T2."accidents_counter" AS "Accidents Counter",
+		T2."is_all_accidents_repaired" AS "Is All Accidents Repaired",
+		T2."cubic_capacity" AS "Cubic Capacity",
+		T2."power_kw" AS "Power Kw",
+		T2."gross_vehicle_weight" AS "Gross Vehicle Weight",
+		T2."unloaded_weight" AS "Unloaded Weight",
+		T2."gear_number" AS "Gear Number",
+		T2."engine_number" AS "Engine Number",
+		T2."odometer_reading_date" AS "Odometer Reading Date",
+		T2."mileage_miles" AS "Mileage Miles",
+		T2."mileage_km" AS "Mileage Km",
+		T2."is_prefer_km" AS "Is Prefer Km",
+		T2."austria_nat_code" AS "Austria Nat Code",
+		T2."german_kba_tsn" AS "German Kba Tsn",
+		T2."german_kba_hsn" AS "German Kba Hsn",
+		T2."last_holder_change_date" AS "Last Holder Change Date",
+		T2."previous_owner_counter" AS "Previous Owner Counter",
+		T2."previous_owner_number" AS "Previous Owner Number",
+		T2."holder_number" AS "Holder Number",
+		T2."owner_number" AS "Owner Number",
+		T2."production_year" AS "Production Year",
+		T2."next_service_miles" AS "Next Service Miles",
+		T2."next_service_km" AS "Next Service Km",
+		T2."next_service_date" AS "Next Service Date",
+		T2."readmission_date" AS "Readmission Date",
+		T2."dealer_vehicle_number" AS "Dealer Vehicle Number",
+		T2."dealer_vehicle_type" AS "Dealer Vehicle Type",
+		T2."is_customer_vehicle" AS "Is Customer Vehicle",
+		T2."is_roadworthy" AS "Is Roadworthy",
+		T2."free_form_model_text" AS "Free Form Model Text",
+		T2."model_code" AS "Model Code",
+		T2."free_form_make_text" AS "Free Form Make Text",
+		T2."make_number" AS "Make Number",
+		T2."license_plate_season" AS "License Plate Season",
+		T2."license_plate_country" AS "License Plate Country",
+		T2."license_plate" AS "License Plate",
+		T2."vin" AS "Vin",
+		(rtrim((((T1."dealer_vehicle_number"))))) AS "Free Form Document Text",
+		T1."dealer_vehicle_type" + (rtrim((((T1."dealer_vehicle_number"))))) AS "Free Form Accounting Text"
+	FROM (
+		(
+			(
+				(
+					"dbo"."vehicles" T2 LEFT JOIN "dbo"."makes" T3 ON T2."make_number" = T3."make_number"
+					) LEFT JOIN "dbo"."models" T4 ON (T2."make_number" = T4."make_number")
+					AND (T2."model_code" = T4."model_code")
+				) LEFT JOIN "dbo"."customers_suppliers" T5 ON T2."previous_owner_number" = T5."customer_number"
+			) LEFT JOIN "LOCOSOFT"."dbo"."dealer_vehicles" T1 ON (T1."dealer_vehicle_type" = T2."dealer_vehicle_type")
+			AND (T1."dealer_vehicle_number" = T2."dealer_vehicle_number")
+		)
+	WHERE (
+			(T1."in_arrival_date" IS NOT NULL)
+			AND (T1."out_invoice_number" IS NULL)
+			)
+		-- order by "Vehicle_Reference_berechnet" asc
+	) D1
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 155 - 99
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_dg_anz_tage_imr.sql

@@ -1,106 +1,162 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.offene_auftraege_dg_anz_tage_imr AS
 
-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",
-	   "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",
-	   1 / (COUNT("Subsidiary") OVER (partition by "Order Number_ori")) as "DG",
-	   "Anzahl Tage_1",
-	   ("Anzahl Tage_1") / (COUNT("Subsidiary") OVER (partition by "Order Number_ori")) as "Anzahl Tage"
-from 
-(select T1."order_number" as "Order Number_ori",
-	   (-1 * datediff(day, (getdate()), T1."order_date")) as "Anzahl Tage_1",
-	   (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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."labour_types" T3 on T3."code" = T2."labour_type") left outer join "dbo"."employees" T4 on T1."order_taking_employee_no" = T4."employee_number") left outer join "dbo"."customers_suppliers" T5 on T1."order_customer" = T5."customer_number") left outer join "dbo"."vehicles" T6 on T1."vehicle_number" = T6."internal_number") left outer join "dbo"."makes" T7 on T6."make_number" = T7."make_number") left outer join "dbo"."models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code"))
-where (((T2."invoice_number" IS NULL) and (T1."order_date" >= convert(datetime, '2020-01-01 00:00:00.000'))) and (T2."labour_type" <> 'F'))
--- order by "Order Number_ori" asc
-) D1
+CREATE VIEW staging.offene_auftraege_dg_anz_tage_imr
+AS
+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",
+	"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",
+	1 / (COUNT("Subsidiary") OVER (PARTITION BY "Order Number_ori")) AS "DG",
+	"Anzahl Tage_1",
+	("Anzahl Tage_1") / (COUNT("Subsidiary") OVER (PARTITION BY "Order Number_ori")) AS "Anzahl Tage"
+FROM (
+	SELECT T1."order_number" AS "Order Number_ori",
+		(- 1 * datediff(day, (getdate()), T1."order_date")) AS "Anzahl Tage_1",
+		(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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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")
+		)
+	WHERE (
+			(
+				(T2."invoice_number" IS NULL)
+				AND (T1."order_date" >= convert(DATETIME, '2020-01-01 00:00:00.000'))
+				)
+			AND (T2."labour_type" <> 'F')
+			)
+		-- order by "Order Number_ori" asc
+	) D1
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 163 - 106
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_teile_imr.sql

@@ -1,113 +1,170 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.offene_auftraege_teile_imr AS
 
-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",
-	   "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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."parts" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."employees" T3 on T1."order_taking_employee_no" = T3."employee_number") left outer join "dbo"."customers_suppliers" T4 on T1."order_customer" = T4."customer_number") left outer join "dbo"."vehicles" T5 on T1."vehicle_number" = T5."internal_number") left outer join "dbo"."makes" T6 on T5."make_number" = T6."make_number") left outer 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
+CREATE VIEW staging.offene_auftraege_teile_imr
+AS
+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",
+	"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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 171 - 110
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.offene_auftraege_ums_ben_aw_imr.sql

@@ -1,117 +1,178 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.offene_auftraege_ums_ben_aw_imr AS
 
-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",
-	   "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",
-	   '0' + (((T1."subsidiary"))) as "Standort",
-	   CASE WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   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 outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."labour_types" T3 on T3."code" = T2."labour_type") left outer join "dbo"."employees" T4 on T1."order_taking_employee_no" = T4."employee_number") left outer join "dbo"."customers_suppliers" T5 on T1."order_customer" = T5."customer_number") left outer join "dbo"."vehicles" T6 on T1."vehicle_number" = T6."internal_number") left outer join "dbo"."makes" T7 on T6."make_number" = T7."make_number") left outer join "dbo"."models" T8 on (T6."make_number" = T8."make_number") and (T6."model_code" = T8."model_code")) left outer 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
+CREATE VIEW staging.offene_auftraege_ums_ben_aw_imr
+AS
+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",
+	"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",
+		'0' + (((T1."subsidiary"))) AS "Standort",
+		CASE 
+			WHEN (('0' + (((T1."subsidiary")))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		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
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 33 - 20
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_8520_imr.sql

@@ -1,26 +1,39 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.op_aus_loc_belege_8520_imr
+AS
+SELECT DISTINCT T1."document_type" AS "Document Type",
+	T1."nominal_account_number" AS "Nominal Account Number",
+	T1."customer_contra_account" AS "Customer Contra Account",
+	T1."invoice_number" AS "Invoice Number",
+	T1."skr51_branch" AS "Skr51 Branch",
+	'1' AS "Rechtseinheit",
+	'0' + (((T1."skr51_branch"))) AS "Betrieb",
+	'1' AS "Mandant",
+	'1' AS "Hauptbetrieb",
+	('0' + (((T1."skr51_branch")))) AS "Standort"
+FROM "journal_accountings" T1
+WHERE (
+		(
+			(T1."nominal_account_number" IN (8520))
+			AND (T1."document_type" = 'R')
+			)
+		AND (T1."invoice_date" >= convert(DATE, '2016-01-01'))
+		)
+	-- order by "Invoice Number" asc,"Nominal Account Number" asc
 GO
-CREATE VIEW staging.op_aus_loc_belege_8520_imr AS
-
-select distinct T1."document_type" as "Document Type",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   '1' as "Rechtseinheit",
-	   '0' + (((T1."skr51_branch"))) as "Betrieb",
-	   '1' as "Mandant",
-	   '1' as "Hauptbetrieb",
-	   ('0' + (((T1."skr51_branch")))) as "Standort"
-from "journal_accountings" T1
-where (((T1."nominal_account_number" IN (8520)) and (T1."document_type" = 'R')) and (T1."invoice_date" >= convert(date, '2016-01-01')))
--- order by "Invoice Number" asc,"Nominal Account Number" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 3 - 193
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_deb_saldo_imr.sql


+ 319 - 166
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_nur_8520_imr.sql

@@ -1,173 +1,326 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.op_aus_loc_belege_nur_8520_imr AS
 
-select "Accounting Date",
-	   "Document Type",
-	   "Document Number",
-	   "Position In Document",
-	   "Nominal Account Number",
-	   "Customer 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",
-	   "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",
-	   '1' as "Rechtseinheit",
-	   "Betrieb",
-	   "Accounting Date" as "Bookkeep Date",
-	   "Text",
-	   '1' as "Mandant",
-	   "Betrag",
-	   "Menge",
-	   "Nominal Account Number" as "Acct Nr",
-	   "Customer Number_suctomer_supplier",
-	   "Is Supplier",
-	   "First Name",
-	   "Family Name",
-	   "Document Type In Journal",
-	   "Document Type Description",
-	   SUM("Betrag") OVER (partition by "Customer Number_suctomer_supplier") as "Debitorsaldo_",
-	   "Invoice Type_Invoices",
-	   "Invoice Number_Invoices",
-	   "Subsidiary_Invoices",
-	   "Invoice Date_Invoices",
-	   "Hauptbetrieb",
-	   "Standort_ori",
-	   "Sel Name",
-	   "Betrag" as "OP-Saldo",
-	   "Kostenstelle",
-	   "Tage",
-	   "Staffel",
-	   "Kunde",
-	   "Beleg",
-	   "Employee Number_Employees_journal_accountings",
-	   "Name_Employees_journal_accountings",
-	   "Anzahl_Stellen_Invoice_Number",
-	   "Forderungsart_aus_erste_Stelle_Inv_Number",
-	   "Forderungsart",
-	   "Standort_aus_2_Stelle_Invoice_Number",
-	   "Betrieb" as "Standort",
-	   "Standort_ims_8520"
-from 
-(select T3."customer_number" as "Customer Number_suctomer_supplier",
-	   T6."Standort" as "Standort_ims_8520",
-	   ('0' + (((T1."subsidiary_to_company_ref")))) as "Betrieb",
-	   CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((substring(T1."invoice_number", 2, 1)) BETWEEN '1' AND '4')) THEN ('0' + (substring(T1."invoice_number", 2, 1))) ELSE null END as "Standort_aus_2_Stelle_Invoice_Number",
-	   CASE WHEN (T1."dunning_level" = 'Gesperrt') THEN ('Versicherung') ELSE ((CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('2','3','4'))) THEN ('Service') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('5'))) THEN ('Teile') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('6'))) THEN ('Garantie') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('7'))) THEN ('Neuwagen') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('8'))) THEN ('Gebrauchtwagen') WHEN (T1."document_type" = 'R') THEN ('Rent') ELSE ('nicht zuzuordnen') END)) END as "Forderungsart",
-	   CASE WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('2','3','4'))) THEN ('Service') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('5'))) THEN ('Teile') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('6'))) THEN ('Garantie') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('7'))) THEN ('Neuwagen') WHEN ((((len(T1."invoice_number" + 'Z') - 1)) = 7) and ((left(T1."invoice_number",1)) IN ('8'))) THEN ('Gebrauchtwagen') WHEN (T1."document_type" = 'R') THEN ('Rent') ELSE ('nicht zuzuordnen') END as "Forderungsart_aus_erste_Stelle_Inv_Number",
-	   (len(T1."invoice_number" + 'Z') - 1) as "Anzahl_Stellen_Invoice_Number",
-	   T2."name" as "Name_Employees_journal_accountings",
-	   T2."employee_number" as "Employee Number_Employees_journal_accountings",
-	   CASE WHEN (T1."posting_text" IS NOT NULL) THEN ((substring((convert(varchar(50), ((T1."document_number")))), 1, 7)) + ' - ' + T1."posting_text" + ' - ' + (convert(varchar(50), year(T1."document_date")) + '-' + convert(varchar(50), month(T1."document_date")) + '-' + convert(varchar(50), day(T1."document_date")))) ELSE ((convert(varchar(50), year(T1."document_date")) + '-' + convert(varchar(50), month(T1."document_date")) + '-' + convert(varchar(50), day(T1."document_date")))) END as "Beleg",
-	   CASE WHEN (T3."first_name" <> ' ') THEN ((left((((T3."customer_number"))),7)) + ' - ' + T3."first_name" + ' ' + T3."family_name") ELSE ((left((((T3."customer_number"))),7)) + ' - ' + T3."family_name") END as "Kunde",
-	   CASE WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 0 AND 14) THEN ('< 2 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 15 AND 28) THEN ('2 - 4 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 29 AND 42) THEN ('4 - 6 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) BETWEEN 43 AND 84) THEN ('6 - 12 Wochen') WHEN (((-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date"))))) > 84) THEN ('> 12 Wochen') ELSE null END as "Staffel",
-	   (-1 * datediff(day, (getdate()), (convert(datetime, T1."accounting_date")))) as "Tage",
-	   CASE WHEN (T5."invoice_type" IN (2,4,6,3)) THEN ('3') WHEN (T5."invoice_type" IN (5)) THEN ('6') WHEN (T5."invoice_type" IN (7)) THEN ('1') WHEN (T5."invoice_type" IN (8)) THEN ('2') WHEN (T4."document_type_in_journal" = 'R') THEN ('7') ELSE null END as "Kostenstelle",
-	   (CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_value" / 100 * -1) ELSE (T1."posted_value" / 100) END) as "Betrag",
-	   (left((((T2."employee_number"))),3)) + ' - ' + T2."name" as "Sel Name",
-	   CASE WHEN (T5."subsidiary" IS NOT NULL) THEN ('0' + (left((((T5."subsidiary"))),1))) ELSE ('nicht zuzuordnen') END as "Standort_ori",
-	   CASE WHEN ((('0' + (((T1."subsidiary_to_company_ref"))))) IN ('015                                                                                                                                                                                                                                                            ')) THEN ('15') ELSE ('1') END as "Hauptbetrieb",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."subsidiary" as "Subsidiary_Invoices",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_type" as "Invoice Type_Invoices",
-	   T4."document_type_description" as "Document Type Description",
-	   T4."document_type_in_journal" as "Document Type In Journal",
-	   T3."family_name" as "Family Name",
-	   T3."first_name" as "First Name",
-	   T3."is_supplier" as "Is Supplier",
-	   T1."nominal_account_number" as "Nominal Account Number",
-	   CASE WHEN (T1."debit_or_credit" = 'H') THEN (T1."posted_count" / 100 * -1) ELSE (T1."posted_count" / 100) END as "Menge",
-	   ((rtrim((convert(varchar(50), ((T1."document_number")))))) + ' - ' + T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + ((left((((T2."employee_number"))))) + ' - ' + T2."name")) as "Text",
-	   T1."accounting_date" as "Accounting Date",
-	   T1."free_form_document_text" as "Free Form Document Text",
-	   T1."free_form_accounting_text" as "Free Form Accounting Text",
-	   T1."previously_used_account_no" as "Previously Used Account No",
-	   T1."skr51_cost_unit" as "Skr51 Cost Unit",
-	   T1."skr51_sales_channel" as "Skr51 Sales Channel",
-	   T1."skr51_cost_center" as "Skr51 Cost Center",
-	   T1."skr51_make" as "Skr51 Make",
-	   T1."skr51_branch" as "Skr51 Branch",
-	   T1."day_of_actual_accounting" as "Day Of Actual Accounting",
-	   T1."days_for_cash_discount" as "Days For Cash Discount",
-	   T1."vat_key" as "Vat Key",
-	   T1."account_statement_page" as "Account Statement Page",
-	   T1."account_statement_number" as "Account Statement Number",
-	   T1."vat_id_number" as "Vat Id Number",
-	   T1."vehicle_reference" as "Vehicle Reference",
-	   T1."posting_text" as "Posting Text",
-	   T1."term_of_payment" as "Term Of Payment",
-	   T1."cash_discount" as "Cash Discount",
-	   T1."journal_line" as "Journal Line",
-	   T1."journal_page" as "Journal Page",
-	   T1."last_dunning_date" as "Last Dunning Date",
-	   T1."dunning_level" as "Dunning Level",
-	   T1."invoice_number" as "Invoice Number",
-	   T1."invoice_date" as "Invoice Date",
-	   T1."employee_number" as "Employee Number",
-	   T1."serial_number_each_month" as "Serial Number Each Month",
-	   T1."account_form_page_line" as "Account Form Page Line",
-	   T1."account_form_page_number" as "Account Form Page Number",
-	   T1."contra_account_text" as "Contra Account Text",
-	   T1."nominal_contra_account" as "Nominal Contra Account",
-	   T1."customer_contra_account" as "Customer Contra Account",
-	   T1."branch_number" as "Branch Number",
-	   T1."posted_count" as "Posted Count",
-	   T1."debit_or_credit" as "Debit Or Credit",
-	   T1."posted_value" as "Posted Value",
-	   T1."document_date" as "Document Date",
-	   T1."clearing_number" as "Clearing Number",
-	   T1."is_balanced" as "Is Balanced",
-	   T1."customer_number" as "Customer Number",
-	   T1."position_in_document" as "Position In Document",
-	   (T1."document_number") as "Document Number",
-	   T1."document_type" as "Document Type"
-from "ims"."OP_aus_LOC_Belege_8520" T6,
-	(((("journal_accountings" T1 left outer join "LOCOSOFT"."dbo"."employees" T2 on T1."employee_number" = T2."employee_number") left outer join "LOCOSOFT"."dbo"."customers_suppliers" T3 on T3."customer_number" = T1."customer_number") left outer join "document_types" T4 on T4."document_type_in_journal" = T1."document_type") left outer join "invoices" T5 on T5."invtype_invnr" = T1."invoice_number")
-where ((T1."invoice_number" = T6."invoice_number") and (T1."customer_number" = T6."customer_contra_account"))
-and (((((T1."nominal_account_number" IN (1400,1409,1451,1408,1525)) and (T1."clearing_number" = 0)) and (T1."customer_number" IS NOT NULL)) and (not T4."document_type_in_journal" IN ('A','E','U','V','L'))) and (T3."customer_number" IS NOT NULL))
-) D1
--- order by "Customer Number" asc,"Document Type In Journal" asc,"Nominal Account Number" asc
+CREATE VIEW staging.op_aus_loc_belege_nur_8520_imr
+AS
+SELECT "Accounting Date",
+	"Document Type",
+	"Document Number",
+	"Position In Document",
+	"Nominal Account Number",
+	"Customer 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",
+	"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",
+	'1' AS "Rechtseinheit",
+	"Betrieb",
+	"Accounting Date" AS "Bookkeep Date",
+	"Text",
+	'1' AS "Mandant",
+	"Betrag",
+	"Menge",
+	"Nominal Account Number" AS "Acct Nr",
+	"Customer Number_suctomer_supplier",
+	"Is Supplier",
+	"First Name",
+	"Family Name",
+	"Document Type In Journal",
+	"Document Type Description",
+	SUM("Betrag") OVER (PARTITION BY "Customer Number_suctomer_supplier") AS "Debitorsaldo_",
+	"Invoice Type_Invoices",
+	"Invoice Number_Invoices",
+	"Subsidiary_Invoices",
+	"Invoice Date_Invoices",
+	"Hauptbetrieb",
+	"Standort_ori",
+	"Sel Name",
+	"Betrag" AS "OP-Saldo",
+	"Kostenstelle",
+	"Tage",
+	"Staffel",
+	"Kunde",
+	"Beleg",
+	"Employee Number_Employees_journal_accountings",
+	"Name_Employees_journal_accountings",
+	"Anzahl_Stellen_Invoice_Number",
+	"Forderungsart_aus_erste_Stelle_Inv_Number",
+	"Forderungsart",
+	"Standort_aus_2_Stelle_Invoice_Number",
+	"Betrieb" AS "Standort",
+	"Standort_ims_8520"
+FROM (
+	SELECT T3."customer_number" AS "Customer Number_suctomer_supplier",
+		T6."Standort" AS "Standort_ims_8520",
+		('0' + (((T1."subsidiary_to_company_ref")))) AS "Betrieb",
+		CASE 
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((substring(T1."invoice_number", 2, 1)) BETWEEN '1' AND '4')
+					)
+				THEN ('0' + (substring(T1."invoice_number", 2, 1)))
+			ELSE NULL
+			END AS "Standort_aus_2_Stelle_Invoice_Number",
+		CASE 
+			WHEN (T1."dunning_level" = 'Gesperrt')
+				THEN ('Versicherung')
+			ELSE (
+					(
+						CASE 
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('2', '3', '4'))
+									)
+								THEN ('Service')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('5'))
+									)
+								THEN ('Teile')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('6'))
+									)
+								THEN ('Garantie')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('7'))
+									)
+								THEN ('Neuwagen')
+							WHEN (
+									(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+									AND ((left(T1."invoice_number", 1)) IN ('8'))
+									)
+								THEN ('Gebrauchtwagen')
+							WHEN (T1."document_type" = 'R')
+								THEN ('Rent')
+							ELSE ('nicht zuzuordnen')
+							END
+						)
+					)
+			END AS "Forderungsart",
+		CASE 
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('2', '3', '4'))
+					)
+				THEN ('Service')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('5'))
+					)
+				THEN ('Teile')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('6'))
+					)
+				THEN ('Garantie')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('7'))
+					)
+				THEN ('Neuwagen')
+			WHEN (
+					(((len(T1."invoice_number" + 'Z') - 1)) = 7)
+					AND ((left(T1."invoice_number", 1)) IN ('8'))
+					)
+				THEN ('Gebrauchtwagen')
+			WHEN (T1."document_type" = 'R')
+				THEN ('Rent')
+			ELSE ('nicht zuzuordnen')
+			END AS "Forderungsart_aus_erste_Stelle_Inv_Number",
+		(len(T1."invoice_number" + 'Z') - 1) AS "Anzahl_Stellen_Invoice_Number",
+		T2."name" AS "Name_Employees_journal_accountings",
+		T2."employee_number" AS "Employee Number_Employees_journal_accountings",
+		CASE 
+			WHEN (T1."posting_text" IS NOT NULL)
+				THEN ((substring((convert(VARCHAR(50), ((T1."document_number")))), 1, 7)) + ' - ' + T1."posting_text" + ' - ' + (convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date"))))
+			ELSE ((convert(VARCHAR(50), year(T1."document_date")) + '-' + convert(VARCHAR(50), month(T1."document_date")) + '-' + convert(VARCHAR(50), day(T1."document_date"))))
+			END AS "Beleg",
+		CASE 
+			WHEN (T3."first_name" <> ' ')
+				THEN ((left((((T3."customer_number"))), 7)) + ' - ' + T3."first_name" + ' ' + T3."family_name")
+			ELSE ((left((((T3."customer_number"))), 7)) + ' - ' + T3."family_name")
+			END AS "Kunde",
+		CASE 
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 0 AND 14)
+				THEN ('< 2 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 15 AND 28)
+				THEN ('2 - 4 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 29 AND 42)
+				THEN ('4 - 6 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) BETWEEN 43 AND 84)
+				THEN ('6 - 12 Wochen')
+			WHEN (((- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date"))))) > 84)
+				THEN ('> 12 Wochen')
+			ELSE NULL
+			END AS "Staffel",
+		(- 1 * datediff(day, (getdate()), (convert(DATETIME, T1."accounting_date")))) AS "Tage",
+		CASE 
+			WHEN (T5."invoice_type" IN (2, 4, 6, 3))
+				THEN ('3')
+			WHEN (T5."invoice_type" IN (5))
+				THEN ('6')
+			WHEN (T5."invoice_type" IN (7))
+				THEN ('1')
+			WHEN (T5."invoice_type" IN (8))
+				THEN ('2')
+			WHEN (T4."document_type_in_journal" = 'R')
+				THEN ('7')
+			ELSE NULL
+			END AS "Kostenstelle",
+		(
+			CASE 
+				WHEN (T1."debit_or_credit" = 'H')
+					THEN (T1."posted_value" / 100 * - 1)
+				ELSE (T1."posted_value" / 100)
+				END
+			) AS "Betrag",
+		(left((((T2."employee_number"))), 3)) + ' - ' + T2."name" AS "Sel Name",
+		CASE 
+			WHEN (T5."subsidiary" IS NOT NULL)
+				THEN ('0' + (left((((T5."subsidiary"))), 1)))
+			ELSE ('nicht zuzuordnen')
+			END AS "Standort_ori",
+		CASE 
+			WHEN ((('0' + (((T1."subsidiary_to_company_ref"))))) IN ('015                                                                                                                                                                                                                                                            '))
+				THEN ('15')
+			ELSE ('1')
+			END AS "Hauptbetrieb",
+		T5."invoice_date" AS "Invoice Date_Invoices",
+		T5."subsidiary" AS "Subsidiary_Invoices",
+		T5."invoice_number" AS "Invoice Number_Invoices",
+		T5."invoice_type" AS "Invoice Type_Invoices",
+		T4."document_type_description" AS "Document Type Description",
+		T4."document_type_in_journal" AS "Document Type In Journal",
+		T3."family_name" AS "Family Name",
+		T3."first_name" AS "First Name",
+		T3."is_supplier" AS "Is Supplier",
+		T1."nominal_account_number" AS "Nominal Account Number",
+		CASE 
+			WHEN (T1."debit_or_credit" = 'H')
+				THEN (T1."posted_count" / 100 * - 1)
+			ELSE (T1."posted_count" / 100)
+			END AS "Menge",
+		((rtrim((convert(VARCHAR(50), ((T1."document_number")))))) + ' - ' + T1."invoice_number" + ' - ' + T1."posting_text" + '/' + T1."vehicle_reference" + ' - ' + ((left((((T2."employee_number"))))) + ' - ' + T2."name")) AS "Text",
+		T1."accounting_date" AS "Accounting Date",
+		T1."free_form_document_text" AS "Free Form Document Text",
+		T1."free_form_accounting_text" AS "Free Form Accounting Text",
+		T1."previously_used_account_no" AS "Previously Used Account No",
+		T1."skr51_cost_unit" AS "Skr51 Cost Unit",
+		T1."skr51_sales_channel" AS "Skr51 Sales Channel",
+		T1."skr51_cost_center" AS "Skr51 Cost Center",
+		T1."skr51_make" AS "Skr51 Make",
+		T1."skr51_branch" AS "Skr51 Branch",
+		T1."day_of_actual_accounting" AS "Day Of Actual Accounting",
+		T1."days_for_cash_discount" AS "Days For Cash Discount",
+		T1."vat_key" AS "Vat Key",
+		T1."account_statement_page" AS "Account Statement Page",
+		T1."account_statement_number" AS "Account Statement Number",
+		T1."vat_id_number" AS "Vat Id Number",
+		T1."vehicle_reference" AS "Vehicle Reference",
+		T1."posting_text" AS "Posting Text",
+		T1."term_of_payment" AS "Term Of Payment",
+		T1."cash_discount" AS "Cash Discount",
+		T1."journal_line" AS "Journal Line",
+		T1."journal_page" AS "Journal Page",
+		T1."last_dunning_date" AS "Last Dunning Date",
+		T1."dunning_level" AS "Dunning Level",
+		T1."invoice_number" AS "Invoice Number",
+		T1."invoice_date" AS "Invoice Date",
+		T1."employee_number" AS "Employee Number",
+		T1."serial_number_each_month" AS "Serial Number Each Month",
+		T1."account_form_page_line" AS "Account Form Page Line",
+		T1."account_form_page_number" AS "Account Form Page Number",
+		T1."contra_account_text" AS "Contra Account Text",
+		T1."nominal_contra_account" AS "Nominal Contra Account",
+		T1."customer_contra_account" AS "Customer Contra Account",
+		T1."branch_number" AS "Branch Number",
+		T1."posted_count" AS "Posted Count",
+		T1."debit_or_credit" AS "Debit Or Credit",
+		T1."posted_value" AS "Posted Value",
+		T1."document_date" AS "Document Date",
+		T1."clearing_number" AS "Clearing Number",
+		T1."is_balanced" AS "Is Balanced",
+		T1."customer_number" AS "Customer Number",
+		T1."position_in_document" AS "Position In Document",
+		(T1."document_number") AS "Document Number",
+		T1."document_type" AS "Document Type"
+	FROM "ims"."OP_aus_LOC_Belege_8520" T6,
+		(
+			(
+				(
+					(
+						"journal_accountings" T1 LEFT JOIN "LOCOSOFT"."dbo"."employees" T2 ON T1."employee_number" = T2."employee_number"
+						) LEFT JOIN "LOCOSOFT"."dbo"."customers_suppliers" T3 ON T3."customer_number" = T1."customer_number"
+					) LEFT JOIN "document_types" T4 ON T4."document_type_in_journal" = T1."document_type"
+				) LEFT JOIN "invoices" T5 ON T5."invtype_invnr" = T1."invoice_number"
+			)
+	WHERE (
+			(T1."invoice_number" = T6."invoice_number")
+			AND (T1."customer_number" = T6."customer_contra_account")
+			)
+		AND (
+			(
+				(
+					(
+						(T1."nominal_account_number" IN (1400, 1409, 1451, 1408, 1525))
+						AND (T1."clearing_number" = 0)
+						)
+					AND (T1."customer_number" IS NOT NULL)
+					)
+				AND (NOT T4."document_type_in_journal" IN ('A', 'E', 'U', 'V', 'L'))
+				)
+			AND (T3."customer_number" IS NOT NULL)
+			)
+	) D1
+	-- order by "Customer Number" asc,"Document Type In Journal" asc,"Nominal Account Number" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 3 - 193
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.op_aus_loc_belege_ohne_8520_imr.sql


+ 138 - 123
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_abwesenheit_neu_imr.sql

@@ -1,130 +1,145 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.zeit_abwesenheit_neu_imr AS
 
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Absence_Cal" as "Employee Number_Absence_Cal",
-	   "Date_Absence_Cal" as "Date_Absence_Cal",
-	   "Type_Absence_Cal" as "Type_Absence_Cal",
-	   "Is Payed_Absence_Cal" as "Is Payed_Absence_Cal",
-	   "Day Contingent_Absence_Cal" as "Day Contingent_Absence_Cal",
-	   "Reason_Absence_Cal" as "Reason_Absence_Cal",
-	   "Booking Flag_Absence_Cal" as "Booking Flag_Absence_Cal",
-	   "Id_Absence_Reas" as "Id_Absence_Reas",
-	   "Description_Absence_Reas" as "Description_Absence_Reas",
-	   "Is Annual Vacation_Absence_Reas" as "Is Annual Vacation_Absence_Reas",
-	   "Type_Absence_Types" as "Type_Absence_Types",
-	   "Description_Absence_Types" as "Description_Absence_Types",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Dummy_Arbeitszeit_Tag" as "Dummy_Arbeitszeit_Tag",
-	   "Abwesenheit" as "Abwesenheit",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std." as "Ges. Std.",
-	   "Datum" as "Datum",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date_Worktimes",
-	   "Arbeitszeit_Tag" as "Arbeitszeit_Tag"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Absence_Cal",
-	   "Date_Absence_Cal",
-	   "Type_Absence_Cal",
-	   "Is Payed_Absence_Cal",
-	   "Day Contingent_Absence_Cal",
-	   "Reason_Absence_Cal",
-	   "Booking Flag_Absence_Cal",
-	   "Id_Absence_Reas",
-	   "Description_Absence_Reas",
-	   "Is Annual Vacation_Absence_Reas",
-	   "Type_Absence_Types",
-	   "Description_Absence_Types",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   8 as "Dummy_Arbeitszeit_Tag",
-	   "Abwesenheit",
-	   'Abwesenheit' as "Zeitkategorie",
-	   "Zeitkategorie2",
-	   "Abwesenheit" as "Ges. Std.",
-	   "Date_Absence_Cal" as "Datum",
-	   '' as "Order Number",
-	   "Wochentag_Datum",
-	   "Validity Date_Worktimes",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Work Duration_Worktimes" as "Arbeitszeit_Tag",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c39
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   (datepart(weekday, T2."date")) as "Wochentag_Datum",
-	   T2."date" as "Date_Absence_Cal",
-	   (T2."day_contingent" * T5."work_duration") as "Abwesenheit",
-	   T3."id" + ' - ' + T3."description" as "Zeitkategorie2",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T4."description" as "Description_Absence_Types",
-	   T4."type" as "Type_Absence_Types",
-	   T3."is_annual_vacation" as "Is Annual Vacation_Absence_Reas",
-	   T3."description" as "Description_Absence_Reas",
-	   T3."id" as "Id_Absence_Reas",
-	   T2."booking_flag" as "Booking Flag_Absence_Cal",
-	   T2."reason" as "Reason_Absence_Cal",
-	   T2."day_contingent" as "Day Contingent_Absence_Cal",
-	   T2."is_payed" as "Is Payed_Absence_Cal",
-	   T2."type" as "Type_Absence_Cal",
-	   T2."employee_number" as "Employee Number_Absence_Cal",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	(("dbo"."absence_calendar" T2 left outer join "dbo"."absence_reasons" T3 on T2."reason" = T3."id") left outer join "dbo"."absence_types" T4 on T2."type" = T4."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((datepart(weekday, T2."date"))) = T5."dayofweek") and (((T2."date")) <= (getdate())))
-) D2
-) D1
-where ("Validity Date_Worktimes" = c39)
--- order by "Employee Number_Employees" asc
+CREATE VIEW staging.zeit_abwesenheit_neu_imr
+AS
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Absence_Cal" AS "Employee Number_Absence_Cal",
+	"Date_Absence_Cal" AS "Date_Absence_Cal",
+	"Type_Absence_Cal" AS "Type_Absence_Cal",
+	"Is Payed_Absence_Cal" AS "Is Payed_Absence_Cal",
+	"Day Contingent_Absence_Cal" AS "Day Contingent_Absence_Cal",
+	"Reason_Absence_Cal" AS "Reason_Absence_Cal",
+	"Booking Flag_Absence_Cal" AS "Booking Flag_Absence_Cal",
+	"Id_Absence_Reas" AS "Id_Absence_Reas",
+	"Description_Absence_Reas" AS "Description_Absence_Reas",
+	"Is Annual Vacation_Absence_Reas" AS "Is Annual Vacation_Absence_Reas",
+	"Type_Absence_Types" AS "Type_Absence_Types",
+	"Description_Absence_Types" AS "Description_Absence_Types",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Dummy_Arbeitszeit_Tag" AS "Dummy_Arbeitszeit_Tag",
+	"Abwesenheit" AS "Abwesenheit",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std." AS "Ges. Std.",
+	"Datum" AS "Datum",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date_Worktimes",
+	"Arbeitszeit_Tag" AS "Arbeitszeit_Tag"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Absence_Cal",
+		"Date_Absence_Cal",
+		"Type_Absence_Cal",
+		"Is Payed_Absence_Cal",
+		"Day Contingent_Absence_Cal",
+		"Reason_Absence_Cal",
+		"Booking Flag_Absence_Cal",
+		"Id_Absence_Reas",
+		"Description_Absence_Reas",
+		"Is Annual Vacation_Absence_Reas",
+		"Type_Absence_Types",
+		"Description_Absence_Types",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		8 AS "Dummy_Arbeitszeit_Tag",
+		"Abwesenheit",
+		'Abwesenheit' AS "Zeitkategorie",
+		"Zeitkategorie2",
+		"Abwesenheit" AS "Ges. Std.",
+		"Date_Absence_Cal" AS "Datum",
+		'' AS "Order Number",
+		"Wochentag_Datum",
+		"Validity Date_Worktimes",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Work Duration_Worktimes" AS "Arbeitszeit_Tag",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c39
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			(datepart(weekday, T2."date")) AS "Wochentag_Datum",
+			T2."date" AS "Date_Absence_Cal",
+			(T2."day_contingent" * T5."work_duration") AS "Abwesenheit",
+			T3."id" + ' - ' + T3."description" AS "Zeitkategorie2",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T4."description" AS "Description_Absence_Types",
+			T4."type" AS "Type_Absence_Types",
+			T3."is_annual_vacation" AS "Is Annual Vacation_Absence_Reas",
+			T3."description" AS "Description_Absence_Reas",
+			T3."id" AS "Id_Absence_Reas",
+			T2."booking_flag" AS "Booking Flag_Absence_Cal",
+			T2."reason" AS "Reason_Absence_Cal",
+			T2."day_contingent" AS "Day Contingent_Absence_Cal",
+			T2."is_payed" AS "Is Payed_Absence_Cal",
+			T2."type" AS "Type_Absence_Cal",
+			T2."employee_number" AS "Employee Number_Absence_Cal",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				(
+					"dbo"."absence_calendar" T2 LEFT JOIN "dbo"."absence_reasons" T3 ON T2."reason" = T3."id"
+					) LEFT JOIN "dbo"."absence_types" T4 ON T2."type" = T4."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(((datepart(weekday, T2."date"))) = T5."dayofweek")
+				AND (((T2."date")) <= (getdate()))
+				)
+		) D2
+	) D1
+WHERE ("Validity Date_Worktimes" = c39)
+	-- order by "Employee Number_Employees" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 104 - 81
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_ben_std_lg_imr.sql

@@ -1,88 +1,111 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.zeit_ben_std_lg_imr AS
 
-select distinct "Order Number" as "Order Number",
-	   "Labour Type" as "Labour Type",
-	   "Code_Labour_Types" as "Code_Labour_Types",
-	   "Description_Labour_Types" as "Description_Labour_Types",
-	   "Order Number_Labours" as "Order Number_Labours",
-	   "Subsidiary_Labours" as "Subsidiary_Labours",
-	   "Invoice Type_Labours" as "Invoice Type_Labours",
-	   "Mechanic No_Labours" as "Mechanic No_Labours",
-	   "AW/Std." as "AW/Std.",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Employee Number_Employee" as "Employee Number_Employee",
-	   "Name_Employee" as "Name_Employee",
-	   "Monteur" as "Monteur",
-	   "Invoice Date_Invoices" as "Invoice Date_Invoices",
-	   "Datum" as "Datum",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "ben. Std." as "ben. Std.",
-	   "Invoice Number" as "Invoice Number",
-	   "Is Canceled" as "Is Canceled",
-	   "Cancelation Number" as "Cancelation Number",
-	   "Cancelation Date" as "Cancelation Date",
-	   "Start Time" as "Start Time",
-	   "End Time" as "End Time",
-	   MIN("Invoice Date_Invoices") OVER (partition by "Order Number","Mechanic No_Labours") as "Min_Invoice_Date"
-from 
-(select "Order Number",
-	   '' as "Labour Type",
-	   '' as "Code_Labour_Types",
-	   '' as "Description_Labour_Types",
-	   "Order Number_Labours",
-	   "Subsidiary_Labours",
-	   0 as "Invoice Type_Labours",
-	   "Mechanic No_Labours",
-	   10 as "AW/Std.",
-	   'ben. Std.' as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Employee Number_Employee",
-	   "Name_Employee",
-	   "Monteur",
-	   "Invoice Date_Invoices",
-	   "Invoice Date_Invoices" as "Datum",
-	   "Duration Minutes_Times",
-	   "ben. Std.",
-	   0 as "Invoice Number",
-	   "Is Canceled",
-	   "Cancelation Number",
-	   "Cancelation Date",
-	   "Start Time",
-	   "End Time",
-	   MIN("Invoice Date_Invoices") OVER (partition by "Order Number","Mechanic No_Labours") as c26
-from 
-(select T2."mechanic_no" as "Mechanic No_Labours",
-	   T1."order_number" as "Order Number",
-	   T5."end_time" as "End Time",
-	   T5."start_time" as "Start Time",
-	   T4."cancelation_date" as "Cancelation Date",
-	   T4."cancelation_number" as "Cancelation Number",
-	   T4."is_canceled" as "Is Canceled",
-	   (T5."exact_duration_seconds" / 60) / 60 as "ben. Std.",
-	   T5."exact_duration_seconds" / 60 as "Duration Minutes_Times",
-	   T4."invoice_date" as "Invoice Date_Invoices",
-	   (rtrim((((T3."employee_number"))))) + ' - ' + T3."name" as "Monteur",
-	   T3."name" as "Name_Employee",
-	   T3."employee_number" as "Employee Number_Employee",
-	   '0' + (rtrim((((T3."subsidiary"))))) as "Subsidiary_Labours",
-	   T2."order_number" as "Order Number_Labours"
-from "LOCOSOFT"."dbo"."times" T5,
-	("dbo"."invoices" T4 left outer join (("dbo"."order_positions" T1 left outer join "dbo"."labours" T2 on T1."order_number" = T2."order_number") left outer join "dbo"."employees" T3 on T2."mechanic_no" = T3."employee_number") on (T2."invoice_number" = T4."invoice_number") and (T2."invoice_type" = T4."invoice_type"))
-where ((T2."order_number" = T5."order_number") and (T2."mechanic_no" = T5."employee_number"))
-and (((convert(float, T2."time_units"))) <> 0)
-) D2
-) D1
-where (c26 = "Invoice Date_Invoices")
--- order by "Order Number" asc,"Mechanic No_Labours" asc,"Start Time" asc
+CREATE VIEW staging.zeit_ben_std_lg_imr
+AS
+SELECT DISTINCT "Order Number" AS "Order Number",
+	"Labour Type" AS "Labour Type",
+	"Code_Labour_Types" AS "Code_Labour_Types",
+	"Description_Labour_Types" AS "Description_Labour_Types",
+	"Order Number_Labours" AS "Order Number_Labours",
+	"Subsidiary_Labours" AS "Subsidiary_Labours",
+	"Invoice Type_Labours" AS "Invoice Type_Labours",
+	"Mechanic No_Labours" AS "Mechanic No_Labours",
+	"AW/Std." AS "AW/Std.",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Employee Number_Employee" AS "Employee Number_Employee",
+	"Name_Employee" AS "Name_Employee",
+	"Monteur" AS "Monteur",
+	"Invoice Date_Invoices" AS "Invoice Date_Invoices",
+	"Datum" AS "Datum",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"ben. Std." AS "ben. Std.",
+	"Invoice Number" AS "Invoice Number",
+	"Is Canceled" AS "Is Canceled",
+	"Cancelation Number" AS "Cancelation Number",
+	"Cancelation Date" AS "Cancelation Date",
+	"Start Time" AS "Start Time",
+	"End Time" AS "End Time",
+	MIN("Invoice Date_Invoices") OVER (
+		PARTITION BY "Order Number",
+		"Mechanic No_Labours"
+		) AS "Min_Invoice_Date"
+FROM (
+	SELECT "Order Number",
+		'' AS "Labour Type",
+		'' AS "Code_Labour_Types",
+		'' AS "Description_Labour_Types",
+		"Order Number_Labours",
+		"Subsidiary_Labours",
+		0 AS "Invoice Type_Labours",
+		"Mechanic No_Labours",
+		10 AS "AW/Std.",
+		'ben. Std.' AS "Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Employee Number_Employee",
+		"Name_Employee",
+		"Monteur",
+		"Invoice Date_Invoices",
+		"Invoice Date_Invoices" AS "Datum",
+		"Duration Minutes_Times",
+		"ben. Std.",
+		0 AS "Invoice Number",
+		"Is Canceled",
+		"Cancelation Number",
+		"Cancelation Date",
+		"Start Time",
+		"End Time",
+		MIN("Invoice Date_Invoices") OVER (
+			PARTITION BY "Order Number",
+			"Mechanic No_Labours"
+			) AS c26
+	FROM (
+		SELECT T2."mechanic_no" AS "Mechanic No_Labours",
+			T1."order_number" AS "Order Number",
+			T5."end_time" AS "End Time",
+			T5."start_time" AS "Start Time",
+			T4."cancelation_date" AS "Cancelation Date",
+			T4."cancelation_number" AS "Cancelation Number",
+			T4."is_canceled" AS "Is Canceled",
+			(T5."exact_duration_seconds" / 60) / 60 AS "ben. Std.",
+			T5."exact_duration_seconds" / 60 AS "Duration Minutes_Times",
+			T4."invoice_date" AS "Invoice Date_Invoices",
+			(rtrim((((T3."employee_number"))))) + ' - ' + T3."name" AS "Monteur",
+			T3."name" AS "Name_Employee",
+			T3."employee_number" AS "Employee Number_Employee",
+			'0' + (rtrim((((T3."subsidiary"))))) AS "Subsidiary_Labours",
+			T2."order_number" AS "Order Number_Labours"
+		FROM "LOCOSOFT"."dbo"."times" T5,
+			(
+				"dbo"."invoices" T4 LEFT JOIN (
+					(
+						"dbo"."order_positions" T1 LEFT JOIN "dbo"."labours" T2 ON T1."order_number" = T2."order_number"
+						) LEFT JOIN "dbo"."employees" T3 ON T2."mechanic_no" = T3."employee_number"
+					) ON (T2."invoice_number" = T4."invoice_number")
+					AND (T2."invoice_type" = T4."invoice_type")
+				)
+		WHERE (
+				(T2."order_number" = T5."order_number")
+				AND (T2."mechanic_no" = T5."employee_number")
+				)
+			AND (((convert(FLOAT, T2."time_units"))) <> 0)
+		) D2
+	) D1
+WHERE (c26 = "Invoice Date_Invoices")
+	-- order by "Order Number" asc,"Mechanic No_Labours" asc,"Start Time" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 69 - 42
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_neu_imr.sql

@@ -1,48 +1,75 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.zeit_stempelungen_neu_imr
+AS
+SELECT T1."employee_number" AS "Employee Number_Employees",
+	T1."name" AS "Name_Employees",
+	T1."initials" AS "Initials_Employees",
+	T1."mechanic_number" AS "Mechanic Number_Employees",
+	T1."salesman_number" AS "Salesman Number_Employees",
+	T1."employment_date" AS "Employment Date_Employees",
+	T1."termination_date" AS "Termination Date_Employees",
+	T1."leave_date" AS "Leave Date_Employees",
+	T2."employee_number" AS "Employee Number_Times",
+	T2."order_number" AS "Order Number_Times",
+	T2."start_time" AS "Start Time_Times",
+	T2."type" AS "Type_Times",
+	T3."Order Number" AS "Order Positions_Times",
+	T2."end_time" AS "End Time_Times",
+	T2."exact_duration_seconds" / 60 AS "Duration Minutes_Times",
+	T4."type" AS "Type_Time_Types",
+	T4."description" AS "Description_Time_Types",
+	T2."start_time" AS "Datum",
+	'1' AS "Hauptbetrieb",
+	'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+	(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+	CASE 
+		WHEN (T4."type" = 1)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "Anwesenheit",
+	CASE 
+		WHEN (T4."type" = 2)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "produktiv",
+	CASE 
+		WHEN (T4."type" = 1)
+			THEN ((T2."exact_duration_seconds" / 60) / 60)
+		ELSE (0)
+		END AS "unproduktiv",
+	CASE 
+		WHEN (T2."type" = 2)
+			THEN ('produktiv')
+		ELSE ('unproduktiv')
+		END AS "Zeitkategorie",
+	'' AS "Zeitkategorie2",
+	(T2."exact_duration_seconds" / 60) / 60 AS "Ges. Std.",
+	T2."order_number" AS "Order Number",
+	(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+	T1."is_flextime" AS "Is Flextime_Employees",
+	T1."break_time_registration" AS "Break Time Registration_Employees"
+FROM "dbo"."employees" T1,
+	(
+		(
+			"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T4 ON T2."type" = T4."type"
+			) LEFT JOIN "ims"."offene_Auftraege_Ums_ben_AW_fuer_Monteure" T3 ON T3."order_number" = T2."order_number"
+		)
+WHERE (T1."employee_number" = T2."employee_number")
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
 GO
-CREATE VIEW staging.zeit_stempelungen_neu_imr AS
-
-select T1."employee_number" as "Employee Number_Employees",
-	   T1."name" as "Name_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T2."employee_number" as "Employee Number_Times",
-	   T2."order_number" as "Order Number_Times",
-	   T2."start_time" as "Start Time_Times",
-	   T2."type" as "Type_Times",
-	   T3."Order Number" as "Order Positions_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."exact_duration_seconds" / 60 as "Duration Minutes_Times",
-	   T4."type" as "Type_Time_Types",
-	   T4."description" as "Description_Time_Types",
-	   T2."start_time" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   CASE WHEN (T4."type" = 1) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "Anwesenheit",
-	   CASE WHEN (T4."type" = 2) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "produktiv",
-	   CASE WHEN (T4."type" = 1) THEN ((T2."exact_duration_seconds" / 60) / 60) ELSE (0) END as "unproduktiv",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   (T2."exact_duration_seconds" / 60) / 60 as "Ges. Std.",
-	   T2."order_number" as "Order Number",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   T1."break_time_registration" as "Break Time Registration_Employees"
-from "dbo"."employees" T1,
-	(("dbo"."times" T2 left outer join "dbo"."time_types" T4 on T2."type" = T4."type") left outer join "ims"."offene_Auftraege_Ums_ben_AW_fuer_Monteure" T3 on T3."order_number" = T2."order_number")
-where (T1."employee_number" = T2."employee_number")
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 223 - 174
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_pausen_imr.sql

@@ -1,181 +1,230 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.zeit_stempelungen_pausen_imr AS
 
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Times" as "Employee Number_Times",
-	   "Order Number_Times" as "Order Number_Times",
-	   "Start Time_Times" as "Start Time_Times",
-	   "Type_Times" as "Type_Times",
-	   "Order Positions_Times" as "Order Positions_Times",
-	   "End Time_Times" as "End Time_Times",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "Type_Time_Types" as "Type_Time_Types",
-	   "Description_Time_Types" as "Description_Time_Types",
-	   "Datum" as "Datum",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Anwesenheit_" as "Anwesenheit_",
-	   "produktiv_" as "produktiv_",
-	   "unproduktiv_" as "unproduktiv_",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std._" as "Ges. Std._",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Is Flextime_Employees" as "Is Flextime_Employees",
-	   "Break Time Registration_Employees" as "Break Time Registration_Employees",
-	   "Is Latest Record_break_times" as "Is Latest Record_break_times",
-	   "Dayofweek_break_times" as "Dayofweek_break_times",
-	   "Break Start_break_times" as "Break Start_break_times",
-	   "Break End_break_times" as "Break End_break_times",
-	   "Pause" as "Pause",
-	   "Start_Times_Stunde" as "Start_Times_Stunde",
-	   "Start_Times_Minute" as "Start_Times_Minute",
-	   "End_Times_Stunde" as "End_Times_Stunde",
-	   "End_Times_Minute" as "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal" as "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal" as "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal" as "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal" as "End_Times_Dezimal",
-	   "Pausenzeit_Stunde" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date",
-	   "Validity Date_Break_times" as "Validity Date_Break_times",
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as "Max_validity_date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6" as "Pausenzeit_Anw_mehr_als_6",
-	   "Anwesenheit" as "Anwesenheit"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Times",
-	   "Order Number_Times",
-	   "Start Time_Times",
-	   "Type_Times",
-	   "Order Positions_Times",
-	   "End Time_Times",
-	   "Duration Minutes_Times",
-	   "Type_Time_Types",
-	   "Description_Time_Types",
-	   "Start Time_Times" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   "Anwesenheit_",
-	   "produktiv_",
-	   "Anwesenheit_" as "unproduktiv_",
-	   "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Ges. Std._",
-	   "Order Number_Times" as "Order Number",
-	   "Wochentag_Datum",
-	   "Is Flextime_Employees",
-	   "Break Time Registration_Employees",
-	   "Is Latest Record_break_times",
-	   "Dayofweek_break_times",
-	   "Break Start_break_times",
-	   "Break End_break_times",
-	   "Pause",
-	   "Start_Times_Stunde",
-	   "Start_Times_Minute",
-	   "End_Times_Stunde",
-	   "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal",
-	   "Pause" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Validity Date_Worktimes",
-	   "Validity Date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6",
-	   "Pausenzeit_Anw_mehr_als_6" as "Anwesenheit",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c56,
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as c57
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   (CASE WHEN ((CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END) >= 6.1) THEN ((T4."break_end" - T4."break_start") * -1) ELSE (0) END) as "Pausenzeit_Anw_mehr_als_6",
-	   T4."validity_date" as "Validity Date_Break_times",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T4."break_end" - T4."break_start" as "Pause",
-	   ((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) as "End_Times_Dezimal",
-	   ((od_minute(T2."end_time"))) / 60 as "End_Times_Minute_Dezimal",
-	   ((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) as "Start_Times_Dezimal",
-	   ((od_minute(T2."start_time"))) / 60 as "Start_Times_Minute_Dezimal",
-	   (od_minute(T2."end_time")) as "End_Times_Minute",
-	   (od_hour(T2."end_time")) as "End_Times_Stunde",
-	   (od_minute(T2."start_time")) as "Start_Times_Minute",
-	   (od_hour(T2."start_time")) as "Start_Times_Stunde",
-	   T4."break_end" as "Break End_break_times",
-	   T4."break_start" as "Break Start_break_times",
-	   T4."dayofweek" as "Dayofweek_break_times",
-	   T4."is_latest_record" as "Is Latest Record_break_times",
-	   T1."break_time_registration" as "Break Time Registration_Employees",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T2."order_number" as "Order Number_Times",
-	   T2."duration_minutes" / 60 as "Ges. Std._",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END as "Anwesenheit_",
-	   CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END as "produktiv_",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T2."start_time" as "Start Time_Times",
-	   T3."description" as "Description_Time_Types",
-	   T3."type" as "Type_Time_Types",
-	   T2."duration_minutes" as "Duration Minutes_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."order_positions" as "Order Positions_Times",
-	   T2."type" as "Type_Times",
-	   T2."employee_number" as "Employee Number_Times",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_breaktimes" T4,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	("dbo"."times" T2 left outer join "dbo"."time_types" T3 on T2."type" = T3."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T4."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((T2."type" = 1) and (((datepart(weekday, T2."start_time"))) = T4."dayofweek")) and (((datepart(weekday, T2."start_time"))) = T5."dayofweek")) and (T2."start_time" >= convert(datetime, '2017-01-01 00:00:00.000')))
-) D2
-) D1
-where (("Validity Date_Worktimes" = c56) and ("Validity Date_Break_times" = c57))
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+CREATE VIEW staging.zeit_stempelungen_pausen_imr
+AS
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Times" AS "Employee Number_Times",
+	"Order Number_Times" AS "Order Number_Times",
+	"Start Time_Times" AS "Start Time_Times",
+	"Type_Times" AS "Type_Times",
+	"Order Positions_Times" AS "Order Positions_Times",
+	"End Time_Times" AS "End Time_Times",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"Type_Time_Types" AS "Type_Time_Types",
+	"Description_Time_Types" AS "Description_Time_Types",
+	"Datum" AS "Datum",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Anwesenheit_" AS "Anwesenheit_",
+	"produktiv_" AS "produktiv_",
+	"unproduktiv_" AS "unproduktiv_",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std._" AS "Ges. Std._",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Is Flextime_Employees" AS "Is Flextime_Employees",
+	"Break Time Registration_Employees" AS "Break Time Registration_Employees",
+	"Is Latest Record_break_times" AS "Is Latest Record_break_times",
+	"Dayofweek_break_times" AS "Dayofweek_break_times",
+	"Break Start_break_times" AS "Break Start_break_times",
+	"Break End_break_times" AS "Break End_break_times",
+	"Pause" AS "Pause",
+	"Start_Times_Stunde" AS "Start_Times_Stunde",
+	"Start_Times_Minute" AS "Start_Times_Minute",
+	"End_Times_Stunde" AS "End_Times_Stunde",
+	"End_Times_Minute" AS "End_Times_Minute",
+	"Start_Times_Minute_Dezimal" AS "Start_Times_Minute_Dezimal",
+	"Start_Times_Dezimal" AS "Start_Times_Dezimal",
+	"End_Times_Minute_Dezimal" AS "End_Times_Minute_Dezimal",
+	"End_Times_Dezimal" AS "End_Times_Dezimal",
+	"Pausenzeit_Stunde" AS "Pausenzeit_Stunde",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date",
+	"Validity Date_Break_times" AS "Validity Date_Break_times",
+	MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS "Max_validity_date_Break_times",
+	"Pausenzeit_Anw_mehr_als_6" AS "Pausenzeit_Anw_mehr_als_6",
+	"Anwesenheit" AS "Anwesenheit"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Times",
+		"Order Number_Times",
+		"Start Time_Times",
+		"Type_Times",
+		"Order Positions_Times",
+		"End Time_Times",
+		"Duration Minutes_Times",
+		"Type_Time_Types",
+		"Description_Time_Types",
+		"Start Time_Times" AS "Datum",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		"Anwesenheit_",
+		"produktiv_",
+		"Anwesenheit_" AS "unproduktiv_",
+		"Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Ges. Std._",
+		"Order Number_Times" AS "Order Number",
+		"Wochentag_Datum",
+		"Is Flextime_Employees",
+		"Break Time Registration_Employees",
+		"Is Latest Record_break_times",
+		"Dayofweek_break_times",
+		"Break Start_break_times",
+		"Break End_break_times",
+		"Pause",
+		"Start_Times_Stunde",
+		"Start_Times_Minute",
+		"End_Times_Stunde",
+		"End_Times_Minute",
+		"Start_Times_Minute_Dezimal",
+		"Start_Times_Dezimal",
+		"End_Times_Minute_Dezimal",
+		"End_Times_Dezimal",
+		"Pause" AS "Pausenzeit_Stunde",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Validity Date_Worktimes",
+		"Validity Date_Break_times",
+		"Pausenzeit_Anw_mehr_als_6",
+		"Pausenzeit_Anw_mehr_als_6" AS "Anwesenheit",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c56,
+		MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS c57
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			(
+				CASE 
+					WHEN (
+							(
+								CASE 
+									WHEN (T3."type" = 1)
+										THEN (T2."duration_minutes" / 60)
+									ELSE (0)
+									END
+								) >= 6.1
+							)
+						THEN ((T4."break_end" - T4."break_start") * - 1)
+					ELSE (0)
+					END
+				) AS "Pausenzeit_Anw_mehr_als_6",
+			T4."validity_date" AS "Validity Date_Break_times",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T4."break_end" - T4."break_start" AS "Pause",
+			((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) AS "End_Times_Dezimal",
+			((od_minute(T2."end_time"))) / 60 AS "End_Times_Minute_Dezimal",
+			((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) AS "Start_Times_Dezimal",
+			((od_minute(T2."start_time"))) / 60 AS "Start_Times_Minute_Dezimal",
+			(od_minute(T2."end_time")) AS "End_Times_Minute",
+			(od_hour(T2."end_time")) AS "End_Times_Stunde",
+			(od_minute(T2."start_time")) AS "Start_Times_Minute",
+			(od_hour(T2."start_time")) AS "Start_Times_Stunde",
+			T4."break_end" AS "Break End_break_times",
+			T4."break_start" AS "Break Start_break_times",
+			T4."dayofweek" AS "Dayofweek_break_times",
+			T4."is_latest_record" AS "Is Latest Record_break_times",
+			T1."break_time_registration" AS "Break Time Registration_Employees",
+			T1."is_flextime" AS "Is Flextime_Employees",
+			(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+			T2."order_number" AS "Order Number_Times",
+			T2."duration_minutes" / 60 AS "Ges. Std._",
+			CASE 
+				WHEN (T2."type" = 2)
+					THEN ('produktiv')
+				ELSE ('unproduktiv')
+				END AS "Zeitkategorie",
+			CASE 
+				WHEN (T3."type" = 1)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "Anwesenheit_",
+			CASE 
+				WHEN (T3."type" = 2)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "produktiv_",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T2."start_time" AS "Start Time_Times",
+			T3."description" AS "Description_Time_Types",
+			T3."type" AS "Type_Time_Types",
+			T2."duration_minutes" AS "Duration Minutes_Times",
+			T2."end_time" AS "End Time_Times",
+			T2."order_positions" AS "Order Positions_Times",
+			T2."type" AS "Type_Times",
+			T2."employee_number" AS "Employee Number_Times",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_breaktimes" T4,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T3 ON T2."type" = T3."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T4."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(
+					(
+						(T2."type" = 1)
+						AND (((datepart(weekday, T2."start_time"))) = T4."dayofweek")
+						)
+					AND (((datepart(weekday, T2."start_time"))) = T5."dayofweek")
+					)
+				AND (T2."start_time" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+				)
+		) D2
+	) D1
+WHERE (
+		("Validity Date_Worktimes" = c56)
+		AND ("Validity Date_Break_times" = c57)
+		)
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 227 - 177
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_stempelungen_pausen_produktiv_imr.sql

@@ -1,184 +1,234 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
 GO
-CREATE VIEW staging.zeit_stempelungen_pausen_produktiv_imr AS
 
-select "Employee Number_Employees" as "Employee Number_Employees",
-	   "Name_Employees" as "Name_Employees",
-	   "Initials_Employees" as "Initials_Employees",
-	   "Mechanic Number_Employees" as "Mechanic Number_Employees",
-	   "Salesman Number_Employees" as "Salesman Number_Employees",
-	   "Employment Date_Employees" as "Employment Date_Employees",
-	   "Termination Date_Employees" as "Termination Date_Employees",
-	   "Leave Date_Employees" as "Leave Date_Employees",
-	   "Employee Number_Times" as "Employee Number_Times",
-	   "Order Number_Times" as "Order Number_Times",
-	   "Start Time_Times" as "Start Time_Times",
-	   "Type_Times" as "Type_Times",
-	   "Order Positions_Times" as "Order Positions_Times",
-	   "End Time_Times" as "End Time_Times",
-	   "Duration Minutes_Times" as "Duration Minutes_Times",
-	   "Type_Time_Types" as "Type_Time_Types",
-	   "Description_Time_Types" as "Description_Time_Types",
-	   "Datum" as "Datum",
-	   "Hauptbetrieb" as "Hauptbetrieb",
-	   "Standort" as "Standort",
-	   "Monteur" as "Monteur",
-	   "Anwesenheit_" as "Anwesenheit_",
-	   "produktiv_" as "produktiv_",
-	   "unproduktiv_" as "unproduktiv_",
-	   "Zeitkategorie" as "Zeitkategorie",
-	   "Zeitkategorie2" as "Zeitkategorie2",
-	   "Ges. Std._" as "Ges. Std._",
-	   "Order Number" as "Order Number",
-	   "Wochentag_Datum" as "Wochentag_Datum",
-	   "Is Flextime_Employees" as "Is Flextime_Employees",
-	   "Break Time Registration_Employees" as "Break Time Registration_Employees",
-	   "Is Latest Record_break_times" as "Is Latest Record_break_times",
-	   "Dayofweek_break_times" as "Dayofweek_break_times",
-	   "Break Start_break_times" as "Break Start_break_times",
-	   "Break End_break_times" as "Break End_break_times",
-	   "Pause" as "Pause",
-	   "Start_Times_Stunde" as "Start_Times_Stunde",
-	   "Start_Times_Minute" as "Start_Times_Minute",
-	   "End_Times_Stunde" as "End_Times_Stunde",
-	   "End_Times_Minute" as "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal" as "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal" as "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal" as "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal" as "End_Times_Dezimal",
-	   "Pausenzeit_Stunde" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes" as "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes" as "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes" as "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes" as "Worktime End_Worktimes",
-	   "Validity Date_Worktimes" as "Validity Date_Worktimes",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as "Max_Validity_Date",
-	   "Validity Date_Break_times" as "Validity Date_Break_times",
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as "Max_validity_date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6_" as "Pausenzeit_Anw_mehr_als_6_",
-	   "Anwesenheit" as "Anwesenheit",
-	   "Pausenzeit_während_produktiv" as "Pausenzeit_während_produktiv"
-from 
-(select "Employee Number_Employees",
-	   "Name_Employees",
-	   "Initials_Employees",
-	   "Mechanic Number_Employees",
-	   "Salesman Number_Employees",
-	   "Employment Date_Employees",
-	   "Termination Date_Employees",
-	   "Leave Date_Employees",
-	   "Employee Number_Times",
-	   "Order Number_Times",
-	   "Start Time_Times",
-	   "Type_Times",
-	   "Order Positions_Times",
-	   "End Time_Times",
-	   "Duration Minutes_Times",
-	   "Type_Time_Types",
-	   "Description_Time_Types",
-	   "Start Time_Times" as "Datum",
-	   '1' as "Hauptbetrieb",
-	   "Standort",
-	   "Monteur",
-	   "Anwesenheit_",
-	   "produktiv_",
-	   "Anwesenheit_" as "unproduktiv_",
-	   "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   "Ges. Std._",
-	   "Order Number_Times" as "Order Number",
-	   "Wochentag_Datum",
-	   "Is Flextime_Employees",
-	   "Break Time Registration_Employees",
-	   "Is Latest Record_break_times",
-	   "Dayofweek_break_times",
-	   "Break Start_break_times",
-	   "Break End_break_times",
-	   "Pause",
-	   "Start_Times_Stunde",
-	   "Start_Times_Minute",
-	   "End_Times_Stunde",
-	   "End_Times_Minute",
-	   "Start_Times_Minute_Dezimal",
-	   "Start_Times_Dezimal",
-	   "End_Times_Minute_Dezimal",
-	   "End_Times_Dezimal",
-	   "Pause" as "Pausenzeit_Stunde",
-	   "Dayofweek_Worktimes",
-	   "Work Duration_Worktimes",
-	   "Worktime Start_Worktimes",
-	   "Worktime End_Worktimes",
-	   "Validity Date_Worktimes",
-	   "Validity Date_Break_times",
-	   "Pausenzeit_Anw_mehr_als_6_",
-	   "Pausenzeit_Anw_mehr_als_6_" as "Anwesenheit",
-	   "Pausenzeit_während_produktiv",
-	   MAX("Validity Date_Worktimes") OVER (partition by "Employee Number_Employees") as c57,
-	   MAX("Validity Date_Break_times") OVER (partition by "Employee Number_Employees") as c58
-from 
-(select T1."employee_number" as "Employee Number_Employees",
-	   CASE WHEN (((CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END) > 0) and (T4."break_start" BETWEEN (((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60)) AND (((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60)))) THEN ((((T4."break_end" - T4."break_start") * -1))) ELSE (0) END as "Pausenzeit_während_produktiv",
-	   (((T4."break_end" - T4."break_start") * -1)) as "Pausenzeit_Anw_mehr_als_6_",
-	   T4."validity_date" as "Validity Date_Break_times",
-	   T5."validity_date" as "Validity Date_Worktimes",
-	   T5."worktime_end" as "Worktime End_Worktimes",
-	   T5."worktime_start" as "Worktime Start_Worktimes",
-	   T5."work_duration" as "Work Duration_Worktimes",
-	   T5."dayofweek" as "Dayofweek_Worktimes",
-	   T4."break_end" - T4."break_start" as "Pause",
-	   ((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) as "End_Times_Dezimal",
-	   ((od_minute(T2."end_time"))) / 60 as "End_Times_Minute_Dezimal",
-	   ((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) as "Start_Times_Dezimal",
-	   ((od_minute(T2."start_time"))) / 60 as "Start_Times_Minute_Dezimal",
-	   (od_minute(T2."end_time")) as "End_Times_Minute",
-	   (od_hour(T2."end_time")) as "End_Times_Stunde",
-	   (od_minute(T2."start_time")) as "Start_Times_Minute",
-	   (od_hour(T2."start_time")) as "Start_Times_Stunde",
-	   T4."break_end" as "Break End_break_times",
-	   T4."break_start" as "Break Start_break_times",
-	   T4."dayofweek" as "Dayofweek_break_times",
-	   T4."is_latest_record" as "Is Latest Record_break_times",
-	   T1."break_time_registration" as "Break Time Registration_Employees",
-	   T1."is_flextime" as "Is Flextime_Employees",
-	   (datepart(weekday, T2."start_time")) as "Wochentag_Datum",
-	   T2."order_number" as "Order Number_Times",
-	   T2."duration_minutes" / 60 as "Ges. Std._",
-	   CASE WHEN (T2."type" = 2) THEN ('produktiv') ELSE ('unproduktiv') END as "Zeitkategorie",
-	   CASE WHEN (T3."type" = 1) THEN (T2."duration_minutes" / 60) ELSE (0) END as "Anwesenheit_",
-	   CASE WHEN (T3."type" = 2) THEN (T2."duration_minutes" / 60) ELSE (0) END as "produktiv_",
-	   (rtrim((((T1."employee_number"))))) + ' - ' + T1."name" as "Monteur",
-	   '0' + (rtrim((((T1."subsidiary"))))) as "Standort",
-	   T2."start_time" as "Start Time_Times",
-	   T3."description" as "Description_Time_Types",
-	   T3."type" as "Type_Time_Types",
-	   T2."duration_minutes" as "Duration Minutes_Times",
-	   T2."end_time" as "End Time_Times",
-	   T2."order_positions" as "Order Positions_Times",
-	   T2."type" as "Type_Times",
-	   T2."employee_number" as "Employee Number_Times",
-	   T1."leave_date" as "Leave Date_Employees",
-	   T1."termination_date" as "Termination Date_Employees",
-	   T1."employment_date" as "Employment Date_Employees",
-	   T1."salesman_number" as "Salesman Number_Employees",
-	   T1."mechanic_number" as "Mechanic Number_Employees",
-	   T1."initials" as "Initials_Employees",
-	   T1."name" as "Name_Employees"
-from "dbo"."employees" T1,
-	"LOCOSOFT"."dbo"."employees_breaktimes" T4,
-	"LOCOSOFT"."dbo"."employees_worktimes" T5,
-	("dbo"."times" T2 left outer join "dbo"."time_types" T3 on T2."type" = T3."type")
-where (T1."employee_number" = T2."employee_number") and (T1."employee_number" = T4."employee_number") and (T1."employee_number" = T5."employee_number")
-and ((((T2."type" = 2) and (((datepart(weekday, T2."start_time"))) = T4."dayofweek")) and (((datepart(weekday, T2."start_time"))) = T5."dayofweek")) and (T2."start_time" >= convert(datetime, '2017-01-01 00:00:00.000')))
-) D2
-) D1
-where (("Validity Date_Worktimes" = c57) and ("Validity Date_Break_times" = c58))
--- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
+CREATE VIEW staging.zeit_stempelungen_pausen_produktiv_imr
+AS
+SELECT "Employee Number_Employees" AS "Employee Number_Employees",
+	"Name_Employees" AS "Name_Employees",
+	"Initials_Employees" AS "Initials_Employees",
+	"Mechanic Number_Employees" AS "Mechanic Number_Employees",
+	"Salesman Number_Employees" AS "Salesman Number_Employees",
+	"Employment Date_Employees" AS "Employment Date_Employees",
+	"Termination Date_Employees" AS "Termination Date_Employees",
+	"Leave Date_Employees" AS "Leave Date_Employees",
+	"Employee Number_Times" AS "Employee Number_Times",
+	"Order Number_Times" AS "Order Number_Times",
+	"Start Time_Times" AS "Start Time_Times",
+	"Type_Times" AS "Type_Times",
+	"Order Positions_Times" AS "Order Positions_Times",
+	"End Time_Times" AS "End Time_Times",
+	"Duration Minutes_Times" AS "Duration Minutes_Times",
+	"Type_Time_Types" AS "Type_Time_Types",
+	"Description_Time_Types" AS "Description_Time_Types",
+	"Datum" AS "Datum",
+	"Hauptbetrieb" AS "Hauptbetrieb",
+	"Standort" AS "Standort",
+	"Monteur" AS "Monteur",
+	"Anwesenheit_" AS "Anwesenheit_",
+	"produktiv_" AS "produktiv_",
+	"unproduktiv_" AS "unproduktiv_",
+	"Zeitkategorie" AS "Zeitkategorie",
+	"Zeitkategorie2" AS "Zeitkategorie2",
+	"Ges. Std._" AS "Ges. Std._",
+	"Order Number" AS "Order Number",
+	"Wochentag_Datum" AS "Wochentag_Datum",
+	"Is Flextime_Employees" AS "Is Flextime_Employees",
+	"Break Time Registration_Employees" AS "Break Time Registration_Employees",
+	"Is Latest Record_break_times" AS "Is Latest Record_break_times",
+	"Dayofweek_break_times" AS "Dayofweek_break_times",
+	"Break Start_break_times" AS "Break Start_break_times",
+	"Break End_break_times" AS "Break End_break_times",
+	"Pause" AS "Pause",
+	"Start_Times_Stunde" AS "Start_Times_Stunde",
+	"Start_Times_Minute" AS "Start_Times_Minute",
+	"End_Times_Stunde" AS "End_Times_Stunde",
+	"End_Times_Minute" AS "End_Times_Minute",
+	"Start_Times_Minute_Dezimal" AS "Start_Times_Minute_Dezimal",
+	"Start_Times_Dezimal" AS "Start_Times_Dezimal",
+	"End_Times_Minute_Dezimal" AS "End_Times_Minute_Dezimal",
+	"End_Times_Dezimal" AS "End_Times_Dezimal",
+	"Pausenzeit_Stunde" AS "Pausenzeit_Stunde",
+	"Dayofweek_Worktimes" AS "Dayofweek_Worktimes",
+	"Work Duration_Worktimes" AS "Work Duration_Worktimes",
+	"Worktime Start_Worktimes" AS "Worktime Start_Worktimes",
+	"Worktime End_Worktimes" AS "Worktime End_Worktimes",
+	"Validity Date_Worktimes" AS "Validity Date_Worktimes",
+	MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS "Max_Validity_Date",
+	"Validity Date_Break_times" AS "Validity Date_Break_times",
+	MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS "Max_validity_date_Break_times",
+	"Pausenzeit_Anw_mehr_als_6_" AS "Pausenzeit_Anw_mehr_als_6_",
+	"Anwesenheit" AS "Anwesenheit",
+	"Pausenzeit_w�hrend_produktiv" AS "Pausenzeit_w�hrend_produktiv"
+FROM (
+	SELECT "Employee Number_Employees",
+		"Name_Employees",
+		"Initials_Employees",
+		"Mechanic Number_Employees",
+		"Salesman Number_Employees",
+		"Employment Date_Employees",
+		"Termination Date_Employees",
+		"Leave Date_Employees",
+		"Employee Number_Times",
+		"Order Number_Times",
+		"Start Time_Times",
+		"Type_Times",
+		"Order Positions_Times",
+		"End Time_Times",
+		"Duration Minutes_Times",
+		"Type_Time_Types",
+		"Description_Time_Types",
+		"Start Time_Times" AS "Datum",
+		'1' AS "Hauptbetrieb",
+		"Standort",
+		"Monteur",
+		"Anwesenheit_",
+		"produktiv_",
+		"Anwesenheit_" AS "unproduktiv_",
+		"Zeitkategorie",
+		'' AS "Zeitkategorie2",
+		"Ges. Std._",
+		"Order Number_Times" AS "Order Number",
+		"Wochentag_Datum",
+		"Is Flextime_Employees",
+		"Break Time Registration_Employees",
+		"Is Latest Record_break_times",
+		"Dayofweek_break_times",
+		"Break Start_break_times",
+		"Break End_break_times",
+		"Pause",
+		"Start_Times_Stunde",
+		"Start_Times_Minute",
+		"End_Times_Stunde",
+		"End_Times_Minute",
+		"Start_Times_Minute_Dezimal",
+		"Start_Times_Dezimal",
+		"End_Times_Minute_Dezimal",
+		"End_Times_Dezimal",
+		"Pause" AS "Pausenzeit_Stunde",
+		"Dayofweek_Worktimes",
+		"Work Duration_Worktimes",
+		"Worktime Start_Worktimes",
+		"Worktime End_Worktimes",
+		"Validity Date_Worktimes",
+		"Validity Date_Break_times",
+		"Pausenzeit_Anw_mehr_als_6_",
+		"Pausenzeit_Anw_mehr_als_6_" AS "Anwesenheit",
+		"Pausenzeit_w�hrend_produktiv",
+		MAX("Validity Date_Worktimes") OVER (PARTITION BY "Employee Number_Employees") AS c57,
+		MAX("Validity Date_Break_times") OVER (PARTITION BY "Employee Number_Employees") AS c58
+	FROM (
+		SELECT T1."employee_number" AS "Employee Number_Employees",
+			CASE 
+				WHEN (
+						(
+							(
+								CASE 
+									WHEN (T3."type" = 2)
+										THEN (T2."duration_minutes" / 60)
+									ELSE (0)
+									END
+								) > 0
+							)
+						AND (T4."break_start" BETWEEN (((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60)) AND (((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60)))
+						)
+					THEN ((((T4."break_end" - T4."break_start") * - 1)))
+				ELSE (0)
+				END AS "Pausenzeit_w�hrend_produktiv",
+			(((T4."break_end" - T4."break_start") * - 1)) AS "Pausenzeit_Anw_mehr_als_6_",
+			T4."validity_date" AS "Validity Date_Break_times",
+			T5."validity_date" AS "Validity Date_Worktimes",
+			T5."worktime_end" AS "Worktime End_Worktimes",
+			T5."worktime_start" AS "Worktime Start_Worktimes",
+			T5."work_duration" AS "Work Duration_Worktimes",
+			T5."dayofweek" AS "Dayofweek_Worktimes",
+			T4."break_end" - T4."break_start" AS "Pause",
+			((od_hour(T2."end_time"))) + (((od_minute(T2."end_time"))) / 60) AS "End_Times_Dezimal",
+			((od_minute(T2."end_time"))) / 60 AS "End_Times_Minute_Dezimal",
+			((od_hour(T2."start_time"))) + (((od_minute(T2."start_time"))) / 60) AS "Start_Times_Dezimal",
+			((od_minute(T2."start_time"))) / 60 AS "Start_Times_Minute_Dezimal",
+			(od_minute(T2."end_time")) AS "End_Times_Minute",
+			(od_hour(T2."end_time")) AS "End_Times_Stunde",
+			(od_minute(T2."start_time")) AS "Start_Times_Minute",
+			(od_hour(T2."start_time")) AS "Start_Times_Stunde",
+			T4."break_end" AS "Break End_break_times",
+			T4."break_start" AS "Break Start_break_times",
+			T4."dayofweek" AS "Dayofweek_break_times",
+			T4."is_latest_record" AS "Is Latest Record_break_times",
+			T1."break_time_registration" AS "Break Time Registration_Employees",
+			T1."is_flextime" AS "Is Flextime_Employees",
+			(datepart(weekday, T2."start_time")) AS "Wochentag_Datum",
+			T2."order_number" AS "Order Number_Times",
+			T2."duration_minutes" / 60 AS "Ges. Std._",
+			CASE 
+				WHEN (T2."type" = 2)
+					THEN ('produktiv')
+				ELSE ('unproduktiv')
+				END AS "Zeitkategorie",
+			CASE 
+				WHEN (T3."type" = 1)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "Anwesenheit_",
+			CASE 
+				WHEN (T3."type" = 2)
+					THEN (T2."duration_minutes" / 60)
+				ELSE (0)
+				END AS "produktiv_",
+			(rtrim((((T1."employee_number"))))) + ' - ' + T1."name" AS "Monteur",
+			'0' + (rtrim((((T1."subsidiary"))))) AS "Standort",
+			T2."start_time" AS "Start Time_Times",
+			T3."description" AS "Description_Time_Types",
+			T3."type" AS "Type_Time_Types",
+			T2."duration_minutes" AS "Duration Minutes_Times",
+			T2."end_time" AS "End Time_Times",
+			T2."order_positions" AS "Order Positions_Times",
+			T2."type" AS "Type_Times",
+			T2."employee_number" AS "Employee Number_Times",
+			T1."leave_date" AS "Leave Date_Employees",
+			T1."termination_date" AS "Termination Date_Employees",
+			T1."employment_date" AS "Employment Date_Employees",
+			T1."salesman_number" AS "Salesman Number_Employees",
+			T1."mechanic_number" AS "Mechanic Number_Employees",
+			T1."initials" AS "Initials_Employees",
+			T1."name" AS "Name_Employees"
+		FROM "dbo"."employees" T1,
+			"LOCOSOFT"."dbo"."employees_breaktimes" T4,
+			"LOCOSOFT"."dbo"."employees_worktimes" T5,
+			(
+				"dbo"."times" T2 LEFT JOIN "dbo"."time_types" T3 ON T2."type" = T3."type"
+				)
+		WHERE (T1."employee_number" = T2."employee_number")
+			AND (T1."employee_number" = T4."employee_number")
+			AND (T1."employee_number" = T5."employee_number")
+			AND (
+				(
+					(
+						(T2."type" = 2)
+						AND (((datepart(weekday, T2."start_time"))) = T4."dayofweek")
+						)
+					AND (((datepart(weekday, T2."start_time"))) = T5."dayofweek")
+					)
+				AND (T2."start_time" >= convert(DATETIME, '2017-01-01 00:00:00.000'))
+				)
+		) D2
+	) D1
+WHERE (
+		("Validity Date_Worktimes" = c57)
+		AND ("Validity Date_Break_times" = c58)
+		)
+	-- order by "Employee Number_Employees" asc,"Start Time_Times" asc,"Order Number_Times" asc
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET ANSI_NULLS OFF 
+
+SET ANSI_NULLS OFF
 GO
 
-GO
+
+GO
+
+

+ 59 - 43
System/LOCOSOFT/SQL/schema/LOCOSOFT/views_imr/staging.zeit_verk_std_imr.sql

@@ -1,49 +1,65 @@
-SET QUOTED_IDENTIFIER ON 
+SET QUOTED_IDENTIFIER ON
 GO
-SET ANSI_NULLS ON 
+
+SET ANSI_NULLS ON
+GO
+
+CREATE VIEW staging.zeit_verk_std_imr
+AS
+SELECT T1."order_number" AS "Order Number",
+	T1."order_position" AS "Order Position",
+	T1."labour_type" AS "Labour Type",
+	T2."code" AS "Code_Labour_Types",
+	T2."description" AS "Description_Labour_Types",
+	T3."order_number" AS "Order Number_Labours",
+	T3."order_position" AS "Order Position_Labours",
+	T3."order_position_line" AS "Order Position Line_Labours",
+	'0' + (rtrim((((T4."subsidiary"))))) AS "Subsidiary_Labours",
+	T3."is_invoiced" AS "Is Invoiced_Labours",
+	T3."invoice_type" AS "Invoice Type_Labours",
+	T3."invoice_number" AS "Invoice Number_Labours",
+	T3."employee_no" AS "Employee No_Labours",
+	T3."mechanic_no" AS "Mechanic No_Labours",
+	T3."labour_operation_id" AS "Labour Operation Id_Labours",
+	T3."is_nominal" AS "Is Nominal_Labours",
+	T3."net_price_in_order" AS "Net Price In Order_Labours",
+	T3."rebate_percent" AS "Rebate Percent_Labours",
+	T3."goodwill_percent" AS "Goodwill Percent_Labours",
+	T3."charge_type" AS "Charge Type_Labours",
+	T3."text_line" AS "Text Line_Labours",
+	10 AS "AW/Std.",
+	((convert(FLOAT, T3."time_units"))) / 10 AS "verk. Std.",
+	'verk. Std.' AS "Zeitkategorie",
+	'' AS "Zeitkategorie2",
+	T4."employee_number" AS "Employee Number_Employee",
+	T4."name" AS "Name_Employee",
+	(rtrim((((T4."employee_number"))))) + ' - ' + T4."name" AS "Monteur",
+	T5."invoice_number" AS "Invoice Number_Invoices",
+	T5."invoice_date" AS "Invoice Date_Invoices",
+	T5."invoice_date" AS "Datum",
+	T3."time_units" AS "Time Units_Labours",
+	(convert(FLOAT, T3."time_units")) AS "Time_Units_Zahl"
+FROM (
+	"dbo"."invoices" T5 LEFT JOIN (
+		(
+			(
+				"dbo"."order_positions" T1 LEFT JOIN "dbo"."labour_types" T2 ON T1."labour_type" = T2."code"
+				) LEFT JOIN "dbo"."labours" T3 ON T1."order_number" = T3."order_number"
+			) LEFT JOIN "dbo"."employees" T4 ON T3."mechanic_no" = T4."employee_number"
+		) ON (T3."invoice_number" = T5."invoice_number")
+		AND (T3."invoice_type" = T5."invoice_type")
+	)
+WHERE (((convert(FLOAT, T3."time_units"))) <> 0)
+	-- order by "Order Number" asc,"Order Position" asc
 GO
-CREATE VIEW staging.zeit_verk_std_imr AS
-
-select T1."order_number" as "Order Number",
-	   T1."order_position" as "Order Position",
-	   T1."labour_type" as "Labour Type",
-	   T2."code" as "Code_Labour_Types",
-	   T2."description" as "Description_Labour_Types",
-	   T3."order_number" as "Order Number_Labours",
-	   T3."order_position" as "Order Position_Labours",
-	   T3."order_position_line" as "Order Position Line_Labours",
-	   '0' + (rtrim((((T4."subsidiary"))))) as "Subsidiary_Labours",
-	   T3."is_invoiced" as "Is Invoiced_Labours",
-	   T3."invoice_type" as "Invoice Type_Labours",
-	   T3."invoice_number" as "Invoice Number_Labours",
-	   T3."employee_no" as "Employee No_Labours",
-	   T3."mechanic_no" as "Mechanic No_Labours",
-	   T3."labour_operation_id" as "Labour Operation Id_Labours",
-	   T3."is_nominal" as "Is Nominal_Labours",
-	   T3."net_price_in_order" as "Net Price In Order_Labours",
-	   T3."rebate_percent" as "Rebate Percent_Labours",
-	   T3."goodwill_percent" as "Goodwill Percent_Labours",
-	   T3."charge_type" as "Charge Type_Labours",
-	   T3."text_line" as "Text Line_Labours",
-	   10 as "AW/Std.",
-	   ((convert(float, T3."time_units"))) / 10 as "verk. Std.",
-	   'verk. Std.' as "Zeitkategorie",
-	   '' as "Zeitkategorie2",
-	   T4."employee_number" as "Employee Number_Employee",
-	   T4."name" as "Name_Employee",
-	   (rtrim((((T4."employee_number"))))) + ' - ' + T4."name" as "Monteur",
-	   T5."invoice_number" as "Invoice Number_Invoices",
-	   T5."invoice_date" as "Invoice Date_Invoices",
-	   T5."invoice_date" as "Datum",
-	   T3."time_units" as "Time Units_Labours",
-	   (convert(float, T3."time_units")) as "Time_Units_Zahl"
-from ("dbo"."invoices" T5 left outer join ((("dbo"."order_positions" T1 left outer join "dbo"."labour_types" T2 on T1."labour_type" = T2."code") left outer join "dbo"."labours" T3 on T1."order_number" = T3."order_number") left outer join "dbo"."employees" T4 on T3."mechanic_no" = T4."employee_number") on (T3."invoice_number" = T5."invoice_number") and (T3."invoice_type" = T5."invoice_type"))
-where (((convert(float, T3."time_units"))) <> 0)
--- order by "Order Number" asc,"Order Position" asc
+
+SET QUOTED_IDENTIFIER OFF
 GO
-SET QUOTED_IDENTIFIER OFF 
+
+SET ANSI_NULLS OFF
 GO
-SET ANSI_NULLS OFF 
+
+
 GO
 
-GO
+

+ 3 - 0
Tasks/LOCOSOFT_imr_csv.bat

@@ -49,6 +49,9 @@
  call impromptu.bat zeit\Zeit_verk_Std.imr csv
  call impromptu.bat zeit\Zeit_ben_Std_LG.imr csv 
  
+ call sqlformat.bat %PORTAL%\System\LOCOSOFT\IQD\*
+ call sqlformat.bat %PORTAL%\System\LOCOSOFT\SQL\schema\LOCOSOFT\views_imr\*
+ 
  move /Y %PORTAL%\System\LOCOSOFT\IQD\Belege\*.csv %PORTAL%\System\LOCOSOFT\Export\
  move /Y %PORTAL%\System\LOCOSOFT\IQD\nw\*.csv %PORTAL%\System\LOCOSOFT\Export\
  move /Y %PORTAL%\System\LOCOSOFT\IQD\serv_teile\*.csv %PORTAL%\System\LOCOSOFT\Export\

BIN
Tasks/scripts/SqlFormatter.exe


+ 3 - 0
Tasks/scripts/sqlformat.bat

@@ -0,0 +1,3 @@
+@call "%~dp0config.bat" 0 > nul
+
+sqlformatter.exe %1 /tc /eil- /ebc- /r /b-

Vissa filer visades inte eftersom för många filer har ändrats