SQL

First, We will declare a table and insert values on it.

declare @ledger table (dateEntry datetime, description varchar(max), amount money, debit bit)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2015-12-31 00:00:00.000' AS DateTime), N'Payroll', 0, 30000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-01 12:00:00.000' AS DateTime), N'New Year''s Family Treat', 1, 3000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-01 13:00:00.000' AS DateTime), N'Savings', 1, 10000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-03 07:00:00.000' AS DateTime), N'Tithes and Offering', 1, 3000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-03 12:00:00.000' AS DateTime), N'Lunchdate with Family', 1, 500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-04 06:45:00.000' AS DateTime), N'15 Days Household Allowance', 1, 5000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-04 07:00:00.000' AS DateTime), N'15 Days Daily Expense Allowance', 1, 3000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-04 07:15:00.000' AS DateTime), N'Diesel Full Tank', 1, 1695.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-08 08:00:00.000' AS DateTime), N'Smart', 1, 1500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-08 08:01:00.000' AS DateTime), N'Globe', 1, 500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-08 08:02:00.000' AS DateTime), N'Sky Cable / Internet', 1, 450.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-08 08:03:00.000' AS DateTime), N'Manila Water', 1, 345.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-10 12:00:00.000' AS DateTime), N'Lunchdate with Family', 1, 500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-15 00:00:00.000' AS DateTime), N'Payroll', 0, 30000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-16 08:00:00.000' AS DateTime), N'15 Days Household Allowance', 1, 5000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-16 08:05:00.000' AS DateTime), N'Savings', 1, 10000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-17 07:00:00.000' AS DateTime), N'Tithes and Offering', 1, 3000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-17 12:00:00.000' AS DateTime), N'Lunchdate with Family', 1, 500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-18 07:00:00.000' AS DateTime), N'15 Days Daily Expense Allowance', 1, 3000.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-18 07:10:00.000' AS DateTime), N'Meralco', 1, 4895.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-18 07:15:00.000' AS DateTime), N'Diesel Full Tank', 1, 1600.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-24 12:00:00.000' AS DateTime), N'Lunchdate with Family', 1, 500.0000)
INSERT INTO @ledger ([dateEntry], [description], [debit], [amount]) VALUES (CAST(N'2016-01-25 07:00:00.000' AS DateTime), N'PLDT', 1, 1299.0000)

Now that we have our test table named @ledger, we need to set the start and end date.

declare
     @startDate datetime = '01/01/2016'
    ,@endDate datetime = '01/31/2016'

set @startDate = CAST(CONVERT(char(10), @startDate, 101) + ' 00:00:00' AS datetime)
set @endDate = CAST(CONVERT(char(10), @endDate, 101) + ' 23:59:59' AS datetime)

Let's declare our temporary ledger table

declare @ledger_temp table (id bigint identity(1,1), dateEntry datetime, event varchar(50),
        description varchar(50), dr money, cr money)

Let's get our starting balance

declare @maxDate datetime,
        @startingBalance money

set @maxDate = (
    select MAX(dateEntry)
    from @ledger
    where dateEntry < @startDate)

if @maxDate is null
    begin
        set @maxDate = @startDate
    end

select  @startingBalance = amount from @ledger
    where dateEntry = @maxDate

if @startingBalance is null
    begin
        set @startingBalance = 0
    end

Let's populate our temporary ledger table

insert into @ledger_temp
    select dateEntry, event, description, dr, cr from
    (
        select
            ts.dateEntry as 'dateEntry',
            (    case ts.debit
                    when 1 then 'Payment'
                    when 0 then 'Income'       
                end) as 'event',
            ts.description as 'description',
            (case ts.debit when 1 then ts.amount end) as dr,
            (case ts.debit when 0 then ts.amount end) as cr
        from
            @ledger ts
        where
            ts.dateEntry >= @startDate
            and ts.dateEntry <= @endDate
    ) aa
    order by dateEntry

Finally, we will now be able to compute our running balance

select   
        @maxDate  as 'dateEntry'
        ,'Starting Balance' as 'event'
        ,'' as 'Description'
        ,0 as dr
        ,0 as cr
        ,@startingBalance as runningTotal
    union
    select   
        tt1.dateEntry, tt1.event, coalesce (tt1.description, '') as description
        , tt1.dr
        , tt1.cr
        , RunningTotal = SUM(   
            case when tt2.dr > 0 then
                tt2.dr * -1
            else
                tt2.cr
            end) + @startingBalance
    from
        @ledger_temp as tt1
    inner join
        @ledger_temp as tt2
        on tt2.dateEntry <= tt1.dateEntry   
    group by
        tt1.dateEntry
        , tt1.event
        , tt1.description
        , tt1.dr
        , tt1.cr