Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH CTE AS
- (
- SELECT
- MIN(a.NetSales) AS NetSales ,MIN(a.SalesCost) AS SalesCost,MIN(a.SellingAndDistributionExpenses) AS SellingAndDistributionExpenses,
- MIN(a.GeneralAndAdministrativeExpenses) AS GeneralAndAdministrativeExpenses,MIN(a.DedicatedDebt) AS DedicatedDebt,
- MIN(a.Loss) AS Loss ,MIN(a.Indemnity) AS Indemnity,MIN(a.ExcludingIntangibleAssets) AS ExcludingIntangibleAssets,
- MIN(a.MinorityRights) AS MinorityRights,MIN(a.Zakat) AS Zakat ,MIN(a.IncomeTax) AS IncomeTax,
- MIN(a.ShareOfSubsidiaries) AS ShareOfSubsidiaries ,MIN(a.GainsOnInvestmentSale) AS GainsOnInvestmentSale,
- MIN(a.ReservedForLowInvestment) AS ReservedForLowInvestment,MIN(a.FinanceCost) AS FinanceCost,
- MIN(a.OtherRevenuesAndExpenses) AS OtherRevenuesAndExpenses, --income
- MIN(a.AmendmentsConsumptionExpenses) AS AmendmentsConsumptionExpenses,
- MIN(a.AmendmentsFireflightersExpenses) AS AmendmentsFireflightersExpenses,--cash
- MIN(a.CashAndEquivalents) AS CashAndEquivalents,MIN(a.ShortTermInvestments) AS ShortTermInvestments,
- MIN(a.TradingInvestments) AS TradingInvestments,MIN(a.TradeReceivables) AS TradeReceivables,
- MIN(a.OreStockpile) AS OreStockpile,MIN(a.ProductionInventory) AS ProductionInventory,
- MIN(a.PartsInventory) AS PartsInventory,MIN(a.PrePayments) AS PrePayments,
- MIN(a.LiabilitiesFromRelatedParties) AS LiabilitiesFromRelatedParties,
- MIN(a.CurrentPortionOfLongTermLoans) AS CurrentPortionOfLongTermLoans,
- MIN(a.CreditorBank) AS CreditorBank,MIN(a.TradeAndPayables) AS TradeAndPayables,
- MIN(a.ProfitsUnderDistribution) AS ProfitsUnderDistribution,
- MIN(a.OwedToContractors) AS OwedToContractors,MIN(a.PayableZakat) AS PayableZakat,
- MIN(a.CreditorOversubscribed) AS CreditorOversubscribed,MIN(a.AccruedExpenses) AS AccruedExpenses,
- MIN(a.CreditorDividend) AS CreditorDividend,MIN(a.RelatedParties) AS RelatedParties,
- MIN(a.DeferredFinancingCosts) AS DeferredFinancingCosts,MIN(a.PropertyAndEquipment) AS PropertyAndEquipment,
- MIN(a.AdvancePaymentsToContractors) AS AdvancePaymentsToContractors,
- MIN(a.InvestmentSecurities) AS InvestmentSecurities,MIN(a.InvestmentsInAssociates) AS InvestmentsInAssociates,
- MIN(a.DeferredTaxes) AS DeferredTaxes,MIN(a.ProjectsUnderImplementation) AS ProjectsUnderImplementation,
- MIN(a.IntangibleAssets) AS IntangibleAssets,MIN(a.Investments) AS Investments,
- MIN(a.LiabilitiesOfDebtorWithRelatedParties) AS LiabilitiesOfDebtorWithRelatedParties,
- MIN(a.UnControlledPropertyRightsFirst) AS UnControlledPropertyRightsFirst,MIN(a.EquityCapital) AS EquityCapital,
- MIN(a.Premium) AS Premium,MIN(a.StatutoryReserve) AS StatutoryReserve,MIN(a.GeneralReserve) AS GeneralReserve,
- MIN(a.ProfitsToDitribute) AS ProfitsToDitribute, MIN(a.RetainedEarnings) AS RetainedEarnings,
- MIN(a.CurrencyDifferences) AS CurrencyDifferences,MIN(a.FairValueReserve) AS FairValueReserve,
- MIN(a.UnrealizedGains) AS UnrealizedGains,--finance
- MIN(f.LocalSales) AS LocalSales,MIN(f.Exporting) AS Exporting ,f.QuantityDate, -------------
- a.[Year],--General
- d.period,d.PeriodTypeId,--Period
- c.CompanyId,c.CompanyName,
- '' AS Tot1,'' AS Tot2,'' AS Tot3,'' AS Tot4,'' AS Tot5,'' AS Tot6,'' AS Tot7,'' AS Tot8,'' AS Tot9,'' AS Tot10,
- '' AS Tot11,'' AS Tot12,'' AS Tot13
- FROM finance.FinanceList a INNER JOIN finance.Company c
- ON a.CompanyId = c.CompanyId
- INNER JOIN finance.ListPeriod d
- ON d.FinanceListId = a.FinanceListId
- INNER JOIN finance.Quantity f ----------
- ON f.CompanyId = a.CompanyId ----------
- AND YEAR(f.QuantityDate) = a.[Year]
- WHERE a.[Year] IN (2016,2015,2014) AND d.PeriodTypeId =2
- AND f.CategoryId = 1 AND YEAR(f.QuantityDate) in(2016,2015) AND MONTH(f.QuantityDate) IN(6,3)-- The user select second quarter
- GROUP BY a.[Year],d.period,d.PeriodTypeId,c.CompanyId,c.CompanyName,f.QuantityDate
- )
- SELECT
- ((ISNULL(d.SalesCost,0)*-1)/(ISNULL(d.LocalSales,0)+ISNULL(d.Exporting,0)+
- (SELECT ISNULL(e.LocalSales,0)+ISNULL(e.Exporting,0)
- FROM CTE e
- WHERE e.[Year] = d.[Year]
- AND e.period =2
- AND e.PeriodTypeId =2
- AND e.CompanyId = d.CompanyId
- AND MONTH(e.QuantityDate)=3))) AS Tot1,
- d.CompanyId,
- d.CompanyName,
- d.[Year]
- ,d.period
- ,d.PeriodTypeId
- ,d.QuantityDate
- FROM CTE d
- INNER JOIN CTE t
- ON d.[Year] = t.[Year]
- AND d.period = t.period
- AND d.PeriodTypeId = t.PeriodTypeId
- AND d.CompanyId = t.CompanyId
- AND d.QuantityDate =t.QuantityDate
- WHERE d.[Year] IN (2016,2015) AND d.PeriodTypeId =2 AND d.Period =2
- AND YEAR(d.QuantityDate)IN (2016,2015) AND MONTH(d.QuantityDate)=6
- ORDER BY d.CompanyId, d.[Year] DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement