Advertisement
Anyname_Donotcare

cumulative sum

Dec 3rd, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. WITH CTE AS
  2. (
  3. SELECT
  4. MIN(a.NetSales) AS NetSales ,MIN(a.SalesCost) AS SalesCost,MIN(a.SellingAndDistributionExpenses) AS SellingAndDistributionExpenses,
  5. MIN(a.GeneralAndAdministrativeExpenses) AS GeneralAndAdministrativeExpenses,MIN(a.DedicatedDebt) AS DedicatedDebt,
  6. MIN(a.Loss) AS Loss ,MIN(a.Indemnity) AS Indemnity,MIN(a.ExcludingIntangibleAssets) AS ExcludingIntangibleAssets,
  7. MIN(a.MinorityRights) AS MinorityRights,MIN(a.Zakat) AS Zakat ,MIN(a.IncomeTax) AS IncomeTax,
  8. MIN(a.ShareOfSubsidiaries) AS ShareOfSubsidiaries ,MIN(a.GainsOnInvestmentSale) AS GainsOnInvestmentSale,
  9. MIN(a.ReservedForLowInvestment) AS ReservedForLowInvestment,MIN(a.FinanceCost) AS FinanceCost,
  10. MIN(a.OtherRevenuesAndExpenses) AS OtherRevenuesAndExpenses, --income
  11. MIN(a.AmendmentsConsumptionExpenses) AS AmendmentsConsumptionExpenses,
  12. MIN(a.AmendmentsFireflightersExpenses) AS AmendmentsFireflightersExpenses,--cash
  13. MIN(a.CashAndEquivalents) AS CashAndEquivalents,MIN(a.ShortTermInvestments) AS ShortTermInvestments,
  14. MIN(a.TradingInvestments) AS TradingInvestments,MIN(a.TradeReceivables) AS TradeReceivables,
  15. MIN(a.OreStockpile) AS OreStockpile,MIN(a.ProductionInventory) AS ProductionInventory,
  16. MIN(a.PartsInventory) AS PartsInventory,MIN(a.PrePayments) AS PrePayments,
  17. MIN(a.LiabilitiesFromRelatedParties) AS LiabilitiesFromRelatedParties,
  18. MIN(a.CurrentPortionOfLongTermLoans) AS CurrentPortionOfLongTermLoans,
  19. MIN(a.CreditorBank) AS CreditorBank,MIN(a.TradeAndPayables) AS TradeAndPayables,
  20. MIN(a.ProfitsUnderDistribution) AS ProfitsUnderDistribution,
  21. MIN(a.OwedToContractors) AS OwedToContractors,MIN(a.PayableZakat) AS PayableZakat,
  22. MIN(a.CreditorOversubscribed) AS CreditorOversubscribed,MIN(a.AccruedExpenses) AS AccruedExpenses,
  23. MIN(a.CreditorDividend) AS CreditorDividend,MIN(a.RelatedParties) AS RelatedParties,
  24. MIN(a.DeferredFinancingCosts) AS DeferredFinancingCosts,MIN(a.PropertyAndEquipment) AS PropertyAndEquipment,
  25. MIN(a.AdvancePaymentsToContractors) AS AdvancePaymentsToContractors,
  26. MIN(a.InvestmentSecurities) AS InvestmentSecurities,MIN(a.InvestmentsInAssociates) AS InvestmentsInAssociates,
  27. MIN(a.DeferredTaxes) AS DeferredTaxes,MIN(a.ProjectsUnderImplementation) AS ProjectsUnderImplementation,
  28. MIN(a.IntangibleAssets) AS IntangibleAssets,MIN(a.Investments) AS Investments,
  29. MIN(a.LiabilitiesOfDebtorWithRelatedParties) AS LiabilitiesOfDebtorWithRelatedParties,
  30. MIN(a.UnControlledPropertyRightsFirst) AS UnControlledPropertyRightsFirst,MIN(a.EquityCapital) AS EquityCapital,
  31. MIN(a.Premium) AS Premium,MIN(a.StatutoryReserve) AS StatutoryReserve,MIN(a.GeneralReserve) AS GeneralReserve,
  32. MIN(a.ProfitsToDitribute) AS ProfitsToDitribute, MIN(a.RetainedEarnings) AS RetainedEarnings,
  33. MIN(a.CurrencyDifferences) AS CurrencyDifferences,MIN(a.FairValueReserve) AS FairValueReserve,
  34. MIN(a.UnrealizedGains) AS UnrealizedGains,--finance
  35. MIN(f.LocalSales) AS LocalSales,MIN(f.Exporting) AS Exporting ,f.QuantityDate, -------------
  36. a.[Year],--General
  37. d.period,d.PeriodTypeId,--Period
  38. c.CompanyId,c.CompanyName,
  39. '' AS Tot1,'' AS Tot2,'' AS Tot3,'' AS Tot4,'' AS Tot5,'' AS Tot6,'' AS Tot7,'' AS Tot8,'' AS Tot9,'' AS Tot10,
  40. '' AS Tot11,'' AS Tot12,'' AS Tot13
  41. FROM finance.FinanceList a INNER JOIN finance.Company c
  42. ON a.CompanyId = c.CompanyId
  43. INNER JOIN finance.ListPeriod d
  44. ON d.FinanceListId = a.FinanceListId
  45.  
  46. INNER JOIN finance.Quantity f ----------
  47. ON f.CompanyId = a.CompanyId ----------
  48. AND YEAR(f.QuantityDate) = a.[Year]
  49. WHERE a.[Year] IN (2016,2015,2014) AND d.PeriodTypeId =2
  50.  
  51. AND f.CategoryId = 1 AND YEAR(f.QuantityDate) in(2016,2015) AND MONTH(f.QuantityDate) IN(6,3)-- The user select second quarter
  52. GROUP BY a.[Year],d.period,d.PeriodTypeId,c.CompanyId,c.CompanyName,f.QuantityDate
  53. )
  54. SELECT
  55. ((ISNULL(d.SalesCost,0)*-1)/(ISNULL(d.LocalSales,0)+ISNULL(d.Exporting,0)+
  56. (SELECT ISNULL(e.LocalSales,0)+ISNULL(e.Exporting,0)
  57. FROM CTE e
  58. WHERE e.[Year] = d.[Year]
  59. AND e.period =2
  60. AND e.PeriodTypeId =2
  61. AND e.CompanyId = d.CompanyId
  62. AND MONTH(e.QuantityDate)=3))) AS Tot1,
  63.  
  64. d.CompanyId,
  65. d.CompanyName,
  66. d.[Year]
  67. ,d.period
  68. ,d.PeriodTypeId
  69. ,d.QuantityDate
  70.  
  71.  
  72. FROM CTE d
  73. INNER JOIN CTE t
  74. ON d.[Year] = t.[Year]
  75. AND d.period = t.period
  76. AND d.PeriodTypeId = t.PeriodTypeId
  77. AND d.CompanyId = t.CompanyId
  78. AND d.QuantityDate =t.QuantityDate
  79. WHERE d.[Year] IN (2016,2015) AND d.PeriodTypeId =2 AND d.Period =2
  80. AND YEAR(d.QuantityDate)IN (2016,2015) AND MONTH(d.QuantityDate)=6
  81. ORDER BY d.CompanyId, d.[Year] DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement