Mitarbeiter_NASA.sql 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. WITH
  2. "Jahre"
  3. AS (SELECT DISTINCT ([T1].[number] + 2000) AS "Jahr"
  4. FROM [master].[dbo].[spt_values] [T1]
  5. WHERE ([T1].[number] + 2000) BETWEEN '2020' AND year(getdate())
  6. AND [T1].[type] = 'P')
  7. , "Monate"
  8. AS (SELECT DISTINCT [T1].[number] AS "Monat"
  9. FROM [master].[dbo].[spt_values] [T1]
  10. WHERE ([T1].[number] BETWEEN 1 AND 12)
  11. AND [T1].[type] = 'P')
  12. , "Periode"
  13. AS (SELECT [Jahr] * 100 + [Monat] AS [Periode]
  14. , datefromparts([Jahr], [Monat], 1) AS [Datum]
  15. FROM [Jahre]
  16. CROSS JOIN [Monate])
  17. , "Punch_min_max"
  18. AS (SELECT [PROFILE_CODE]
  19. , min([START_DATE_TIME]) AS [START_DATE]
  20. , max([END_DATE_TIME]) AS [END_DATE]
  21. FROM [dbo].[PUNCH]
  22. WHERE left([DONE_FOR_DEPARTMENT], 2) IN ('03')
  23. GROUP BY [PROFILE_CODE])
  24. , "Punch_produktiv"
  25. AS (SELECT DISTINCT [PROFILE_CODE]
  26. , year([START_DATE_TIME]) * 100 + month([START_DATE_TIME]) AS [Periode]
  27. , 1 AS [produktiv]
  28. FROM [dbo].[PUNCH]
  29. WHERE [ACTIVITY_CODE] = '1060')
  30. , "Mitarbeiter_aktiv"
  31. AS (SELECT [T3].[PERSON_ID]
  32. , [T3].[JOB_START_DATE]
  33. , [T3].[JOB_END_DATE]
  34. , [T3].[PROFESSION_GROUP_ID]
  35. , [T3].[WORK_LEADER_GROUP_ID]
  36. , [T3].[EMPLOYEE_GROUP]
  37. , [T2].*
  38. FROM [dbo].[PROFILE] [T1]
  39. INNER JOIN [dbo].[EMPLOYEE] [T3] ON [T1].[PERSON_ID] = [T3].[PERSON_ID]
  40. LEFT JOIN [Punch_min_max] [T2] ON [T1].[PROFILE_CODE] = [T2].[PROFILE_CODE])
  41. , "Mitarbeiter_Periode"
  42. AS (SELECT *
  43. FROM [Mitarbeiter_aktiv] [T1]
  44. CROSS JOIN [Periode] [T2]
  45. WHERE [T2].[Datum] BETWEEN [T1].[START_DATE] AND [T1].[END_DATE])
  46. SELECT [T1].*
  47. , isnull([T2].[produktiv], 0) AS [produktiv]
  48. --INTO Mitarbeiter_NASA
  49. FROM "Mitarbeiter_Periode" [T1]
  50. LEFT JOIN "Punch_produktiv" [T2] ON [T1].[PROFILE_CODE] = [T2].[PROFILE_CODE] AND [T1].[Periode] = [T2].[Periode]