status_tag.sql 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. SELECT sm.datum, sm.kunde, sm.aufgabe, sm.anzahl,
  2. IFNULL(s7.anzahl, 'X') AS anzahl_7,
  3. IFNULL(s6.anzahl, 'X') AS anzahl_6,
  4. IFNULL(s5.anzahl, 'X') AS anzahl_5,
  5. IFNULL(s4.anzahl, 'X') AS anzahl_4,
  6. IFNULL(s3.anzahl, 'X') AS anzahl_3,
  7. IFNULL(s2.anzahl, 'X') AS anzahl_2,
  8. IFNULL(s1.anzahl, 'X') AS anzahl_1
  9. FROM `status_meldung` sm
  10. LEFT JOIN status_meldung AS s7 ON s7.datum = date_add(sm.datum, INTERVAL -7 DAY) AND s7.kunde = sm.kunde AND s7.aufgabe = sm.aufgabe
  11. LEFT JOIN status_meldung AS s6 ON s6.datum = date_add(sm.datum, INTERVAL -6 DAY) AND s6.kunde = sm.kunde AND s6.aufgabe = sm.aufgabe
  12. LEFT JOIN status_meldung AS s5 ON s5.datum = date_add(sm.datum, INTERVAL -5 DAY) AND s5.kunde = sm.kunde AND s5.aufgabe = sm.aufgabe
  13. LEFT JOIN status_meldung AS s4 ON s4.datum = date_add(sm.datum, INTERVAL -4 DAY) AND s4.kunde = sm.kunde AND s4.aufgabe = sm.aufgabe
  14. LEFT JOIN status_meldung AS s3 ON s3.datum = date_add(sm.datum, INTERVAL -3 DAY) AND s3.kunde = sm.kunde AND s3.aufgabe = sm.aufgabe
  15. LEFT JOIN status_meldung AS s2 ON s2.datum = date_add(sm.datum, INTERVAL -2 DAY) AND s2.kunde = sm.kunde AND s2.aufgabe = sm.aufgabe
  16. LEFT JOIN status_meldung AS s1 ON s1.datum = date_add(sm.datum, INTERVAL -1 DAY) AND s1.kunde = sm.kunde AND s1.aufgabe = sm.aufgabe
  17. WHERE sm.datum = '2017-06-06'
  18. SELECT DISTINCT a.datum, a.wochentag, a.feiertag, k.kunde, ka.aufgabe, k.system, k.bundesland, ka.start_soll,
  19. case
  20. when a.wochentag = 'Mo' and ka.ende_mo IS NOT NULL then ka.ende_mo
  21. when a.wochentag = 'Di' and ka.ende_di IS NOT NULL then ka.ende_di
  22. when a.wochentag = 'Mi' and ka.ende_mi IS NOT NULL then ka.ende_mi
  23. when a.wochentag = 'Do' and ka.ende_do IS NOT NULL then ka.ende_do
  24. when a.wochentag = 'Fr' and ka.ende_fr IS NOT NULL then ka.ende_fr
  25. when a.wochentag = 'Sa' and ka.ende_sa IS NOT NULL then ka.ende_sa
  26. when a.wochentag = 'So' and ka.ende_so IS NOT NULL then ka.ende_so
  27. else ka.ende_soll
  28. end as 'ende_soll',
  29. ka.woche,
  30. case
  31. when ka.woche = 'Mo-Fr' then a.mofr
  32. when ka.woche = 'Mo-Sa' then a.mosa
  33. when ka.woche = 'Mo-So' then a.moso
  34. when ka.woche = 'Di-So' then a.diso
  35. else 1
  36. end
  37. * ka.aktiv * if(a.datum >= ka.erster_status, 1, 0) as 'anstehend'
  38. FROM kunden k
  39. INNER JOIN arbeitstage a USING (bundesland)
  40. INNER JOIN kunden_aufgabe ka USING (kunde)
  41. WHERE a.datum <= date_add(now(), INTERVAL 1 DAY)
  42. AND a.datum >= date_add(now(), INTERVAL -20 DAY)
  43. LEFT JOIN status_meldung sm USING (datum, kunde)
  44. LEFT JOIN status_kommentar sk ON sm.kommentar_id = sk.id
  45. LEFT JOIN status_kommentar as sk2 ON a.datum = sk2.datum AND k.kunde = sk2.kunde
  46. WHERE {$datum} AND {$datum_bis} AND {$kunde} AND a.datum <= now() AND k.erster_status <= a.datum
  47. AND k.aktiv = 1
  48. ORDER BY a.datum, k.ende_soll
  49. end * if(sk.status <= 1, 0, 1) * if(sm.anzahl > 0 or (sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), INTERVAL 1 HOUR)), 1, 0) as fehler,
  50. if(sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), INTERVAL 1 HOUR), 1, 0) as fehlend,