Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. USE XXX
  2. GO
  3.  
  4. /****** Object: StoredProcedure Script Date: 8/17/2017 7:46:23 AM ******/
  5. SET ANSI_NULLS ON
  6. GO
  7. SET QUOTED_IDENTIFIER ON
  8. GO
  9.  
  10. ALTER PROCEDURE [XXX].[XXX]
  11.    @equipment_tag varchar(50),
  12. @postfix varchar(50),
  13. @time_from datetime2(0),
  14. @time_to datetime2(0)
  15. AS
  16. BEGIN
  17. DECLARE @equipment_id smallint,
  18. @equipment_level_tag varchar(100);
  19.  
  20. DECLARE @machineTable TABLE (Value varchar(50));
  21. set @equipment_level_tag = (SELECT equipment_level_tag FROM [XXX].[XXX] where equipment_tag = @equipment_tag)
  22. SET @equipment_id = (SELECT id FROM Equipment.Equipment WHERE tag = @equipment_tag);
  23. If @equipment_level_tag = 'FACTORY'
  24. Begin
  25. INSERT INTO @machineTable select parent_tag
  26. FROM [XXX].[XXX] where equipment_tag like '%_packer' AND structure_tag = 'UTILITIES'
  27. End
  28. Else If @equipment_level_tag = 'DEPARTMENT'
  29. Begin
  30. INSERT INTO @machineTable SELECT parent_tag
  31. FROM [XXX].[XXX] WHERE parent_id in (SELECT [equipment_id]
  32. FROM [XXX].[XXX] where parent_id in ((SELECT [equipment_id]
  33. FROM [XXX].[XXX] where parent_id = 2))) and equipment_tag like '%_packer' AND structure_tag = 'UTILITIES'
  34. End
  35. Else If @equipment_level_tag = 'CELL'
  36. Begin
  37. INSERT INTO @machineTable select parent_tag
  38. FROM [XXX].[XXX] WHERE parent_id in (SELECT [equipment_id]
  39. FROM [XXX].[XXX] where parent_id = @equipment_id) and equipment_tag like '%_packer' AND structure_tag = 'UTILITIES'
  40. End
  41. Else If @equipment_level_tag = 'WORK_CENTER'
  42. Begin
  43. INSERT INTO @machineTable select parent_tag
  44. FROM [XXX].[XXX] where parent_id = @equipment_id and equipment_tag like '%_packer' AND structure_tag = 'UTILITIES'
  45. End
  46. Else If @equipment_level_tag = 'EQUIPMENT'
  47. Begin
  48. INSERT INTO @machineTable select parent_tag
  49. FROM [XXX].[XXX] where equipment_id = @equipment_id;
  50. END
  51.  
  52. SELECT place_id,name,CONVERT(datetime2(0), SWITCHOFFSET(CONVERT(datetimeoffset,time_from), DATENAME(TzOffset, SYSDATETIMEOFFSET()))),data FROM
  53. mes_machines_statistics
  54. WHERE name = 'OEE_'+@postfix AND time_from >= @time_from AND time_from < @time_to
  55. and place_id in (SELECT idx FROM _machines WHERE name like '%_packer' and line_idx in (SELECT * FROM @machineTable))
  56.  
  57. If @postfix = 'HOUR'
  58. BEGIN
  59. SELECT DATEADD(hh, DATEDIFF(hh, 0, CONVERT(datetime2(0), SWITCHOFFSET(CONVERT(datetimeoffset,time_from), DATENAME(TzOffset, SYSDATETIMEOFFSET())))),0) AS time_consume,
  60. SUM(consumption*multiplicator) as consume
  61. FROM [XXX].[XXX] where equipment_id = @equipment_id AND time_from >= @time_from AND time_from < @time_to
  62. GROUP BY DATEADD(hh, DATEDIFF(hh, 0, CONVERT(datetime2(0), SWITCHOFFSET(CONVERT(datetimeoffset,time_from), DATENAME(TzOffset, SYSDATETIMEOFFSET())))),0)
  63. ORDER BY 1
  64. END
  65. Else If @postfix = 'SHIFT'
  66. BEGIN
  67. WITH Shift_Consumption_CTE (shift_id,shift_start,consume)
  68. AS (
  69. select t1.shift_id ,t2.shift_start, SUM(t1.consumption*t1.multiplicator)
  70. from [XXX].[XXX].[XXX] t1
  71. JOIN [XXX].[XXX].[XXX] t2 on t2.id = t1.shift_id
  72. where equipment_id = @equipment_id AND CAST(shift_start AS DATETIME2(0)) >= @time_from
  73. AND CAST(shift_end AS DATETIME2(0)) <= @time_to
  74. GROUP BY
  75. t1.shift_id,t2.shift_start
  76. )
  77. SELECT CONVERT(datetime2(0), SWITCHOFFSET(CONVERT(datetimeoffset,shift_start), DATENAME(TzOffset, SYSDATETIMEOFFSET()))),consume from Shift_Consumption_CTE
  78. END
  79. Else If @postfix = 'DAY'
  80. BEGIN
  81. WITH Daily_Consumption_CTE (prod_day_start,consume)
  82. AS (
  83. select t2.prod_day_start,SUM(t1.consumption*t1.multiplicator)
  84. from [XXX].[XXX].[XXX] t1
  85. join [XXX].[XXX].[XXX] t2 on t2.id = t1.shift_id
  86. where equipment_id = @equipment_id AND prod_day >= CAST(@time_from AS DATE)
  87. AND prod_day <= CAST(@time_to AS DATE)
  88. GROUP BY
  89. t2.prod_day_start
  90. )
  91. SELECT CONVERT(datetime2(0), SWITCHOFFSET(CONVERT(datetimeoffset,prod_day_start), DATENAME(TzOffset, SYSDATETIMEOFFSET()))),consume from Daily_Consumption_CTE
  92. END
  93. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement