Category/Sub-Category Totals by Date

Share your cool Custom SQL reports or Lua scripts with other mmex users

Category/Sub-Category Totals by Date

Postby Nikolay » Wed Mar 18, 2009 6:54 am

Summary of Category/Sub-Category Values by Dates
Please, change dates in the midle and the bottom of the script under "/* 1 Set your date range in the line below here and in the end of the script: */" line.
Date format is 'YYYY-MM-DD'.
It's script have been checked by me and by bobwill and seem working fine.

Code: Select all
 select (select categname from category_v1 where categid=t.categid) as Category,
 case when t.subcategid=-1 then ' ' else ifnull((select s.subcategname from subcategory_v1 s where s.subcategid=t.subcategid),'Category Total:' ) end as Subcategory,
 round(total,2) as Total
  from (
   select 1 as sequence,  categid, subcategid,
                sum(trx.transamount*cu.baseconvrate) as Total
     from (
     select ca.transdate as transdate,
     ca.categid as categid,
     ca.subcategid as subcategid,
     ca.accountid as accountid,
     ca.transamount*(case when ca.transcode='Withdrawal' then -1.00 else 1.00 end) as transamount
     FROM checkingaccount_v1 ca
     where ca.status not in ('D','V') and ca.transcode not in ('Transfer') and ca.categid!=-1
     union all
     select  (select transdate from checkingaccount_v1 where transid=st.transid) as transdate,
     st.categid as categid,
     st.subcategid as subcategid,
     (select accountid from checkingaccount_v1 where transid=st.transid) as accountid,
     st.splittransamount *(case when (select transcode from checkingaccount_v1 where transid=st.transid)='Withdrawal' then -1.00 else 1.00 end) as transamount
     from splittransactions_v1 st
     where (select status from checkingaccount_v1 where transid=st.transid) not in ('D','V')
     and (select transcode from checkingaccount_v1 where transid=st.transid) != ('Transfer')) trx, accountlist_v1 a, currencyformats_v1 cu
     where a.accountid=trx.accountid and a.currencyid=cu.currencyid and
    /*a.status='Open' and*/ a.accounttype='Checking' and
    /* 1 Set your date range in the line below here and in the end of the script:     */
 trx.transdate between '2009-01-01' and '2009-12-31'
     group by trx.categid, trx.subcategid
                union all
      select
      2 as sequence, trx2.categid, trx2.subcategid,
                sum(trx2.transamount*cu.baseconvrate) as Total
                from (select ca.transdate as transdate, ca.categid as categid,
                1111 as subcategid, ca.accountid as accountid,
                ca.transamount*(case when ca.transcode='Withdrawal' then -1.00 else 1.00 end) as transamount
                FROM checkingaccount_v1 ca
                where ca.status not in ('D','V')
            and ca.transcode!='Transfer' and ca.categid!=-1
                union all
                select (select transdate from checkingaccount_v1 where transid=st.transid) as transdate,
                st.categid as categid,
                1111 as subcategid,
                (select accountid from checkingaccount_v1 where transid=st.transid) as accountid,
                st.splittransamount*(case when (select transcode from checkingaccount_v1 where transid=st.transid)='Withdrawal' then -1.00 else 1.00 end) as transamount
                from splittransactions_v1 st
                where (select status from checkingaccount_v1 where transid=st.transid) not in ('D','V') and (select transcode from checkingaccount_v1 where transid=st.transid) not in ('Transfer')) trx2, accountlist_v1 a, currencyformats_v1 cu
                where a.accountid=trx2.accountid and a.currencyid=cu.currencyid and /*a.status='Open' and */ a.accounttype='Checking' and
                /* 2 Put your date range in the line below:     */
                trx2.transdate between '2009-01-01' and '2009-12-31'
group by trx2.categid) t
order by category, t.sequence, Subcategory
Last edited by Nikolay on Fri Mar 20, 2009 5:24 am, edited 7 times in total.
Nikolay
MMEX Developer
 
Posts: 1173
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia

Re: Category/Sub-Category Totals by Date

Postby madhan » Wed Mar 18, 2009 10:51 am

This is a great script. I am considering making this an official report. So user would just enter two dates in a dialog and the data is shown.
madhan
Site Admin
 
Posts: 248
Joined: Sun Nov 30, 2008 2:06 pm

Re: Category/Sub-Category Totals by Date

Postby Nikolay » Thu Mar 19, 2009 5:15 am

The script has been updated. Now exchange rate is used for foreign currencies. And accounts with status "Closed" will be ignored.
Nikolay
MMEX Developer
 
Posts: 1173
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia

Re: Category/Sub-Category Totals by Date

Postby phil » Mon Apr 20, 2009 10:55 am

Thanks nikolay! This script has solved my problem and made me very :D .

Excellent work!
phil
New MMEX User
 
Posts: 4
Joined: Mon Feb 02, 2009 1:17 pm

Re: Category/Sub-Category Totals by Date

Postby Nikolay » Thu Jun 11, 2009 1:58 am

The script has been updated. Closed accounts now will NOT be ignored.
Nikolay
MMEX Developer
 
Posts: 1173
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia


Return to Custom SQL and Lua Scripts

Who is online

Users browsing this forum: bxevimuks, d3p7j8p2, lrwwuwqao and 1 guest

cron