Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Subquery to calculate Monthly Available Amount

$
0
0

Hi folks,

I have written a query that gives me monthly budget (which is nothing but total budget/12).

select H.Budheadid,Budgetname,BudDetailID,totalBudget,(totalBudget/12) 
as TotalBudgetJan, Year=DATEPART(Year, periodStart),
(totalBudget/12) as TotalBudgetFeb,(totalBudget/12) as TotalBudgetMar,
(totalBudget/12) as TotalBudgetApril,(totalBudget/12) as TotalBudgetMay
,(totalBudget/12) as TotalBudgetJune,(totalBudget/12) as TotalBudgetjuly,
(totalBudget/12) as TotalBudgetAug,
(totalBudget/12) as TotalBudgetSep,(totalBudget/12) as TotalBudgetoct
,(totalBudget/12) as TotalBudgetnov,(totalBudget/12) as TotalBudgetdecember from 
[BudgetDetail] D inner join BudgetHeader H  on H.Budheadid = D.Budheadid

I want to also find Monthly available amount. the formula is below

TotalAvailJan = TotalBudgetJan - AmountUsedInJan (This amount we get from PODetail table using PODate, if the amount is applied in Jan we have to subtracted it from TotalBudgetJan  to get TotalAvailJan  )

Similarly I have to do this for all 12 months.

Can someone please help.

Table Structure

BudgetDetail (BudDetailID,BudHeadID,periodstart,periodend,TotalBudget,AmountPO,TotalAmountRemaining)    

BudgetHeader (BudHeadID,BudgetName)                        

PODetail (POID,BudHeadID,POAmount,PODATE)

Thanks in advance

Regards


Viewing all articles
Browse latest Browse all 23857

Trending Articles