mirror of
https://github.com/acedanger/budget-database.git
synced 2025-12-06 07:00:14 -08:00
54 lines
1.1 KiB
Transact-SQL
54 lines
1.1 KiB
Transact-SQL
use Leo
|
|
go
|
|
|
|
alter PROCEDURE dbo.proc_MonthlyIncomeExpense (
|
|
@p_Year smallint
|
|
)
|
|
AS
|
|
|
|
with credit as (
|
|
SELECT
|
|
det.TrxDate
|
|
, ctg.CtgDescription
|
|
, det.TrxAmount
|
|
, YR=year(det.TrxDate)
|
|
, MO=month(det.TrxDate)
|
|
FROM
|
|
dbo.BUDGET_CATEGORY ctg
|
|
JOIN BUDGET_DETAIL det ON ctg.CtgDescription = det.TrxDescription
|
|
WHERE
|
|
ctg.CtgCredit = 1
|
|
and year(det.TrxDate) = @p_Year
|
|
)
|
|
, debit as (
|
|
select
|
|
det.TrxDescription
|
|
, det.TrxDate
|
|
, det.TrxAmount
|
|
, YR=year(det.TrxDate)
|
|
, MO=month(det.TrxDate)
|
|
from
|
|
dbo.BUDGET_DETAIL det
|
|
left join dbo.BUDGET_CATEGORY ctg on ctg.CtgDescription = det.TrxDescription
|
|
where
|
|
year(det.TrxDate) = @p_Year
|
|
and det.TrxDescription <> 'Initial Balance'
|
|
and det.TrxAmount <> 0
|
|
and isnull(CtgCredit, 0) = 0
|
|
)
|
|
|
|
select
|
|
crd.YR
|
|
, crd.MO
|
|
, EXPENSES=dbt.TTL
|
|
, INCOME=crd.TTL
|
|
, TTL=crd.TTL+dbt.TTL
|
|
from (
|
|
select YR, MO, TTL=sum(credit.TrxAmount) from credit group by YR, MO
|
|
) crd
|
|
join (
|
|
select YR, MO, TTL=sum(debit.TrxAmount) from debit group by YR, MO
|
|
) dbt on crd.YR = dbt.YR and crd.MO = dbt.MO
|
|
|
|
return
|
|
go |