fm_aha_report.sql 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. select
  2. chassis_number as FAGNR,
  3. make_code as FTYPE,
  4. posting_date as REDAT,
  5. posting_date as OPDAT,
  6. due_date as FLDAT,
  7. account_number as O500_KTONR,
  8. document_number as O500_BELEGNR1,
  9. entry_number as Reserve_1,
  10. transact_sum as REBEB,
  11. transact_outstanding as O500_HW2BETR_OFFEN,
  12. transact_sum as O500_HW2BUCHGBETR,
  13. customer_number as KNDNR,
  14. concat(customer_number,' - ', replace(replace(customer_name, " ", " "), "'", "´")) as KNAME,
  15. customer_phone as KTELF,
  16. customer_address as KSTRA,
  17. customer_zipcode as KDPLZ,
  18. customer_city as KDORT,
  19. salesman_user as KDBNR,
  20. salesman_number as KDBERA,
  21. salesman_name as KDBNAME,
  22. verkaeufer as Stufe5,
  23. reminder_level as Reserve_2,
  24. case when transact_sum > 0 then 'R' else 'G' end as Reserve_3,
  25. mahnstufe as O500_MAHNSTUFE,
  26. mahnstufe2 as Next_MAHNSTUFE,
  27. branch_code as Stufe3,
  28. filiale_nummer as FFIRM,
  29. 'Forderungen DMS' as Stufe1,
  30. kostenstelle_text as Stufe2,
  31. 'n.v.' as Stufe4,
  32. branch_code as Betrieb,
  33. shipment_date,
  34. customer_number_leasing,
  35. customer_name_leasing,
  36. text1,
  37. text2
  38. from fm_aha_import as t1
  39. INNER JOIN ( SELECT document_number, chassis_number, due_date, entry_number, MAX(reminder_level) as reminder_level
  40. FROM fm_aha_import
  41. GROUP BY 1, 2, 3, 4 ) AS t2
  42. USING(document_number, chassis_number, due_date, entry_number, reminder_level)