Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. CREATE TABLE [dbo].[RawDataTable] (
  2. [Id] INT IDENTITY (1, 1) NOT NULL,
  3. [CheckDate] DATE NULL,
  4. [PropNum] INT NOT NULL,
  5. [PropSeqNum] INT NULL,
  6. [PropName] NVARCHAR (100) NOT NULL,
  7. [ProductionMonth] INT NULL,
  8. [Product] INT NOT NULL,
  9. [LeaseVolume] DECIMAL (18, 2) NOT NULL,
  10. [Price] DECIMAL (18, 2) NOT NULL,
  11. [LeaseGrossValue] DECIMAL (18, 2) NOT NULL,
  12. [LeaseTaxes] DECIMAL (18, 2) NULL,
  13. [LeaseOtherDeductions] DECIMAL (18, 2) NOT NULL,
  14. [LeaseNetValue] DECIMAL (18, 2) NOT NULL,
  15. [DisbursementDecimal] DECIMAL (18) NULL,
  16. [InterestType] NVARCHAR (10) NULL,
  17. [InterestGrossValue] DECIMAL (18, 2) NOT NULL,
  18. [InterestTaxes] DECIMAL (18, 2) NOT NULL,
  19. [IntrestOtherDeductions] DECIMAL (18, 2) NOT NULL,
  20. [InterestNetValue] DECIMAL (18, 2) NOT NULL,
  21. PRIMARY KEY CLUSTERED ([Id] ASC)
  22. );
  23.  
  24. CREATE PROCEDURE [dbo].[EraseDuplicates]
  25. @TVP dbo.TVP READONLY
  26. AS
  27. SELECT * INTO dbo.#Temp FROM @TVP
  28.  
  29. UPDATE T1
  30. SET T1.LeaseVolume = 0,
  31. T1.Price = 0,
  32. T1.LeaseGrossValue = 0,
  33. T1.LeaseTaxes = 0,
  34. T1.LeaseOtherDeductions = 0,
  35. T1.LeaseNetValue = 0
  36. FROM
  37. (SELECT *
  38. FROM dbo.#Temp
  39. WHERE Product >= 400 OR
  40. Id NOT IN
  41. (SELECT MIN(Id)
  42. FROM dbo.#Temp
  43. GROUP BY CheckDate, PropNum, PropSeqNum, PropName, ProductionMonth, Product)) AS T1
  44.  
  45. INSERT INTO RawDataTable
  46. SELECT *
  47. FROM #Temp
  48. RETURN 0
  49.  
  50. CREATE PROCEDURE [dbo].[CalculateTotals]
  51. @startMonth int,
  52. @endMonth int
  53. AS
  54. DECLARE @date DATE =
  55. (SELECT EOMONTH(MAX(CheckDate))
  56. FROM RawDataTable
  57. WHERE MONTH(CheckDate) >= @startMonth AND
  58. MONTH(CheckDate) <= @endMonth)
  59.  
  60. INSERT INTO RawDataTable
  61. (
  62. CheckDate,
  63. PropNum,
  64. PropSeqNum,
  65. PropName,
  66. ProductionMonth,
  67. Product,
  68. LeaseVolume,
  69. Price,
  70. LeaseGrossValue,
  71. LeaseTaxes,
  72. LeaseOtherDeductions,
  73. LeaseNetValue,
  74. DisbursementDecimal,
  75. InterestType,
  76. InterestGrossValue,
  77. InterestTaxes,
  78. IntrestOtherDeductions,
  79. InterestNetValue
  80. )
  81. SELECT
  82. CheckDate = @date,
  83. PropNum,
  84. PropSeqNum = NULL,
  85. PropName,
  86. ProductionMonth = NULL,
  87. Product,
  88. SUM(LeaseVolume) AS LeaseVolume,
  89. SUM(Price) AS Price,
  90. SUM(LeaseGrossValue) AS LeaseGrossValue,
  91. SUM(LeaseTaxes) AS LeaseTaxes,
  92. SUM(LeaseOtherDeductions) AS LeaseOtherDeductions,
  93. SUM(LeaseNetValue) AS LeaseNetValue,
  94. DisbursementDecimal = NULL,
  95. InterestType = NULL,
  96. SUM(InterestGrossValue) AS InterestGrossValue,
  97. SUM(InterestTaxes) AS InterestTaxes,
  98. SUM(IntrestOtherDeductions) AS IntrestOtherDeductions,
  99. SUM(InterestNetValue) AS InterestNetValue
  100. FROM RawDataTable
  101. WHERE MONTH(CheckDate) >= @startMonth AND
  102. MONTH(CheckDate) <= @endMonth
  103. GROUP BY PropNum, PropName, Product
  104.  
  105. RETURN 0
  106.  
  107. CheckDate PropNum PropSeqNum PropName ProductionMonth Product LeaseVolume Price LeaseGrossValue LeaseTaxes LeaseOtherDeductions LeaseNetValue DisbursementDecimal InterestType InterestGrossValue InterestTaxes IntrestOtherDeductions InterestNetValue
  108. 1/25/2015 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0 35615.61 0.0026932 RI 01 108.38 -12.46 0 95.92
  109. 1/25/2015 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0 35615.61 0.0050669 RI 02 203.91 -23.45 0 180.46
  110.  
  111. CheckDate PropNum PropSeqNum PropName ProductionMonth Product LeaseVolume Price LeaseGrossValue LeaseTaxes LeaseOtherDeductions LeaseNetValue DisbursementDecimal InterestType InterestGrossValue InterestTaxes IntrestOtherDeductions InterestNetValue
  112. 1/25/2015 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0 35615.61 0.0026932 RI 01 108.38 -12.46 0 95.92
  113. 1/25/2015 100004 25 A 122014 100 0.00 0.00 0.00 0.00 0 0.00 0.0050669 RI 02 203.91 -23.45 0 180.46
  114. Qtr1Total 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0 35615.61 0.0026932 RI 01 312.29 -35.91 0 276.38
  115.  
  116. 1/25/2015 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0.00 35615.61 0 RI 01 108.38 -12.46 0.00 95.92
  117. 1/25/2015 100004 25 A 122014 100 774.96 51.93 40243.64 -4628.03 0.00 35615.61 0 RI 01 108.38 -12.46 0.00 95.92
  118. 1/25/2015 100004 25 A 122014 100 0.00 0.00 0.00 0.00 0.00 0.00 0 RI 02 203.91 -23.45 0.00 180.46
  119. 1/25/2015 100004 25 A 122014 100 0.00 0.00 0.00 0.00 0.00 0.00 0 RI 02 203.91 -23.45 0.00 180.46
  120. 1/31/2015 100004 A 122014 100 1549.92 103.86 80487.28 -9256.06 0.00 71231.22 624.58 -71.82 0.00 552.76
  121. 1/31/2015 100004 A 122014 100 3099.84 207.72 160974.56 -18512.12 0.00 142462.44 1249.16 -143.64 0.00 1105.52
  122. 1/31/2015 100004 A 122014 100 6199.68 415.44 321949.12 -37024.24 0.00 284924.88 2498.32 -287.28 0.00 2211.04
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement