load.current_date_prognose_operativ.sql 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. SET QUOTED_IDENTIFIER ON
  2. GO
  3. SET ANSI_NULLS ON
  4. GO
  5. CREATE VIEW [load].[current_date_prognose_operativ]
  6. AS
  7. SELECT "current_date_Prognose_operativ"."Bundeslaender Id" AS "Bundeslaender_Id",
  8. "current_date_Prognose_operativ"."Invoice Date" AS "Invoice_Date",
  9. "current_date_Prognose_operativ"."Wochentage Id" AS "Wochentage_Id",
  10. "current_date_Prognose_operativ"."Arbeitstag Mofr" AS "Arbeitstag_Mofr",
  11. "current_date_Prognose_operativ"."Zaehler Mofr" AS "Zaehler_Mofr",
  12. "current_date_Prognose_operativ"."Summe Mofr" AS "Summe_Mofr",
  13. "current_date_Prognose_operativ"."Arbeitstag Mosa" AS "Arbeitstag_Mosa",
  14. "current_date_Prognose_operativ"."Zaehler Mosa" AS "Zaehler_Mosa",
  15. "current_date_Prognose_operativ"."Summe Mosa" AS "Summe_Mosa",
  16. "current_date_Prognose_operativ"."Feiertage Id" AS "Feiertage_Id",
  17. "current_date_Prognose_operativ"."Jahr" AS "Jahr",
  18. "current_date_Prognose_operativ"."Arbeitstag Nr Jahr" AS "Arbeitstag_Nr_Jahr",
  19. "current_date_Prognose_operativ"."Gesamt Arbeitstage" AS "Gesamt_Arbeitstage"
  20. FROM "GC"."data"."current_date_Prognose_operativ" "current_date_Prognose_operativ"
  21. WHERE "current_date_Prognose_operativ"."Invoice Date" < convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)
  22. AND "current_date_Prognose_operativ"."Bundeslaender Id" = 10
  23. AND (
  24. "current_date_Prognose_operativ"."Arbeitstag Mosa" = 1
  25. OR CASE
  26. WHEN DATEDIFF(DY, DATEADD(DY, 3, DATEADD(YY, 1, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)))), convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)) + ((DATEPART(DW, DATEADD(DY, 3, DATEADD(YY, 1, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112))))) + 5 - @@DATEFIRST) % 7 + 7) % 7 >= 0
  27. THEN 1
  28. WHEN DATEDIFF(DY, DATEADD(DY, 3, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112))), convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)) + ((DATEPART(DW, DATEADD(DY, 3, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)))) + 5 - @@DATEFIRST) % 7 + 7) % 7 >= 0
  29. THEN (DATEDIFF(DY, DATEADD(DY, 3, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112))), convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)) + ((DATEPART(DW, DATEADD(DY, 3, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)))) + 5 - @@DATEFIRST) % 7 + 7) % 7) / 7 + 1
  30. ELSE (DATEDIFF(DY, DATEADD(DY, 3, DATEADD(YY, - 1, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)))), convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112)) + ((DATEPART(DW, DATEADD(DY, 3, DATEADD(YY, - 1, DATENAME(YY, convert(DATETIME, convert(CHAR(8), current_timestamp, 112), 112))))) + 5 - @@DATEFIRST) % 7 + 7) % 7) / 7 + 1
  31. END = 1
  32. )
  33. GO
  34. SET QUOTED_IDENTIFIER OFF
  35. GO
  36. SET ANSI_NULLS OFF
  37. GO
  38. GO