Auftraege_NASA_gruppiert.sql 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. WITH
  2. [Auftraege]
  3. AS (SELECT [T1].[INVOICE_DATE]
  4. , [T1].[ORDER_NUMBER]
  5. , [T1].[STATUS]
  6. , [T1].[CUSTOMER_GROUP]
  7. , [T1].[DEPARTMENT]
  8. , [T2].[ORDER_LINETYPE]
  9. , [T2].[LINES_NET_VALUE]
  10. , [T2].[REDUCTION_AMOUNT]
  11. , [T2].[REPAIR_GROUP]
  12. , [T2].[INV_TIME]
  13. , [T2].[PROD_CODE]
  14. FROM [dbo].[ORDER_HEADER] [T1]
  15. LEFT OUTER JOIN [dbo].[ORDER_LINE] [T2] ON [T1].[ORDER_NUMBER] = [T2].[ORDER_NUMBER]
  16. WHERE [T1].[STATUS] IN ('34', '35', '36', '37', '39', '47', '49', '50', '51')
  17. AND [T1].[INVOICE_DATE] >= convert(datetime, '2023-01-01 00:00:00.000')
  18. AND left([T1].[DEPARTMENT], 2) IN ('03'))
  19. , [Auftraege_Auftragsart]
  20. AS (SELECT [INVOICE_DATE]
  21. , [ORDER_NUMBER]
  22. , [STATUS]
  23. , [CUSTOMER_GROUP]
  24. , [DEPARTMENT]
  25. , [ORDER_LINETYPE]
  26. , [LINES_NET_VALUE]
  27. , [REDUCTION_AMOUNT]
  28. , rtrim([REPAIR_GROUP]) AS [REPAIR_GROUP]
  29. , [INV_TIME]
  30. , [PROD_CODE]
  31. , year([INVOICE_DATE]) * 100 + month([INVOICE_DATE]) AS [Periode]
  32. , substring([DEPARTMENT], 4, 1) AS [Kostenstelle]
  33. , CASE
  34. WHEN ([STATUS] BETWEEN '30' AND '39') THEN 'Theke'
  35. WHEN (([LINES_NET_VALUE] = .00) AND ([REDUCTION_AMOUNT] <> .00)) THEN 'intern'
  36. WHEN ([CUSTOMER_GROUP] LIKE '6%') THEN 'Garantie'
  37. WHEN ([CUSTOMER_GROUP] LIKE '9%') THEN 'intern'
  38. WHEN (left([CUSTOMER_GROUP], 1) BETWEEN 'A' AND 'Z') THEN 'intern'
  39. WHEN ([CUSTOMER_GROUP] IN ('00')) THEN 'intern'
  40. ELSE 'extern' END AS [AuftragsArt]
  41. FROM [Auftraege] [T1])
  42. , [Auftraege_Auftragstyp]
  43. AS (SELECT *
  44. , CASE
  45. WHEN ([Kostenstelle] IN ('3') OR [AuftragsArt] = 'Theke') THEN 'Sonstiges'
  46. WHEN ([REPAIR_GROUP] NOT IN ('2', '3', '4')) THEN 'Inspektion'
  47. WHEN ([REPAIR_GROUP] IN ('2')) THEN 'Karosseriearbeit'
  48. WHEN ([REPAIR_GROUP] IN ('4')) THEN 'Lackierung'
  49. ELSE 'Sonstiges' END AS [AuftragsTyp]
  50. FROM [Auftraege_Auftragsart])
  51. , [Auftraege_Linetype]
  52. AS (SELECT [Periode]
  53. , [AuftragsArt]
  54. , CASE
  55. WHEN ([AuftragsArt] IN ('extern')) THEN '1'
  56. WHEN ([AuftragsArt] IN ('Garantie')) THEN '2'
  57. WHEN ([AuftragsArt] IN ('intern')) THEN '3'
  58. WHEN ([AuftragsArt] IN ('Theke')) THEN '4'
  59. END AS [AuftragsartId]
  60. , [AuftragsTyp]
  61. , CASE
  62. WHEN ([AuftragsTyp] IN ('Inspektion')) THEN '1'
  63. WHEN ([AuftragsTyp] IN ('Karosseriearbeit')) THEN '2'
  64. WHEN ([AuftragsTyp] IN ('Lackierung')) THEN '3'
  65. WHEN ([AuftragsTyp] IN (N'Verschleißteile')) THEN '4'
  66. ELSE '5' END AS [AuftragsTypId]
  67. , [ORDER_NUMBER]
  68. , iif([ORDER_LINETYPE] = '1', [LINES_NET_VALUE], 0.00) AS [Teile]
  69. , iif([ORDER_LINETYPE] = '4', iif([LINES_NET_VALUE] <> 0.00, [LINES_NET_VALUE], [REDUCTION_AMOUNT]), 0.00) AS [Lohn]
  70. , iif([ORDER_LINETYPE] = '3', [LINES_NET_VALUE], 0.00) AS [Sonstige]
  71. , iif([ORDER_LINETYPE] NOT IN ('1', '3', '4'), [LINES_NET_VALUE], 0.00) AS [Rest]
  72. FROM [Auftraege_Auftragstyp])
  73. , [Auftraege_gruppiert]
  74. AS (SELECT [Periode]
  75. , [AuftragsArt]
  76. , [AuftragsartId]
  77. , [AuftragsTyp]
  78. , [AuftragsTypId]
  79. , [ORDER_NUMBER]
  80. , sum([Teile]) AS [TeileUmsatz]
  81. , sum([Lohn]) AS [LohnUmsatz]
  82. , sum([Sonstige]) AS [SonstigeUmsatz]
  83. , sum([Teile] + [Lohn] + [Sonstige]) AS [GesamtUmsatz]
  84. , sum([Rest]) AS [RestUmsatz]
  85. , 1 AS [AnzahlAuftraege]
  86. FROM [Auftraege_Linetype]
  87. GROUP BY [Periode]
  88. , [AuftragsArt]
  89. , [AuftragsartId]
  90. , [AuftragsTyp]
  91. , [AuftragsTypId]
  92. , [ORDER_NUMBER])
  93. , [Auftraege_kumuliert]
  94. AS (SELECT [Periode]
  95. , [AuftragsArt]
  96. , [AuftragsartId]
  97. , [AuftragsTyp]
  98. , [AuftragsTypId]
  99. , sum([TeileUmsatz]) AS [TeileUmsatz]
  100. , sum([LohnUmsatz]) AS [LohnUmsatz]
  101. , sum([SonstigeUmsatz]) AS [SonstigeUmsatz]
  102. , sum([GesamtUmsatz]) AS [GesamtUmsatz]
  103. , sum([RestUmsatz]) AS [RestUmsatz]
  104. , sum([AnzahlAuftraege]) AS [AnzahlAuftraege]
  105. FROM [Auftraege_gruppiert]
  106. GROUP BY [Periode]
  107. , [AuftragsArt]
  108. , [AuftragsartId]
  109. , [AuftragsTyp]
  110. , [AuftragsTypId])
  111. SELECT *
  112. --INTO [Auftraege_NASA_gruppiert]
  113. FROM [Auftraege_kumuliert]
  114. ORDER BY [Periode], [AuftragsArtId], [AuftragsTypId]