Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-10-2008, 04:55 PM
Keychain
 
Posts: n/a
Default Re: best practice: storing credit/debits

"Keychain" <someone@somewhere.com> wrote in message
news:ZpmdnUZj29vA8k7anZ2dnUVZ_jCdnZ2d@comcast.com. ..
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:E8OdnRTiUNNBMFjanZ2dnUVZ_r2nnZ2d@comcast.com. ..
>> Keychain wrote:
>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>> news:T9KdnXZO-7lxR1nanZ2dnUVZ_hKdnZ2d@comcast.com...
>>>> Keychain wrote:
>>>>> I need to create a table that records monetary-like (non-ecommerce)
>>>>> transactions.
>>>>>
>>>>> The structure looks like this:
>>>>>
>>>>> tid, uid, amount, balance, type, dateline
>>>>>
>>>>> The data looks like this:
>>>>>
>>>>> 1, 1, -10, -10, debit, 1202651856
>>>>> 2, 1, 3, -7, credit, 1202652000
>>>>>
>>>>> A running balance is kept, which makes it easy to pull the newest
>>>>> record for the current balance.
>>>>>
>>>>
>>>> Don't keep a running balance. Just total the amounts for an account.
>>>> If the table gets too big, do it like the banks do - have a starting
>>>> balance for the month and total from there.
>>>>
>>>>> The debit/credit amounts are stored as positive and negative values.
>>>>>
>>>>
>>>> That's how I would do it.
>>>>
>>>>> Is there anything flawed about this approach?
>>>>>
>>>>
>>>> Just ensure you are encrypting any sensitive information (i.e. if this
>>>> were an ecommerce database, encrypt the credit card info).
>>>>
>>>>> Should the balance be kept in a separate table? Exp. table structure:
>>>>> uid, balance
>>>>>
>>>>
>>>> Nope. See above.
>>>>
>>>>> Should the amounts all be stored as absolute positive values?
>>>>>
>>>>
>>>> Definitely NOT.
>>>>
>>>>> What about having separate debit and credit columns and skipping the
>>>>> type indicator? This would mean one of the columns for each record
>>>>> would always be zero.
>>>>>
>>>>
>>>> Nope. And why do you need a type indicator? You can tell which one is
>>>> is based on the amount being positive or negative.
>>>>
>>>>> Any other thoughts/suggestions?
>>>>>
>>>>> Thanks in advance.
>>>>>
>>>>
>>>> KISS. Don't add more data than you need, and definitely don't
>>>> duplicate data. Try reading up on "database normalization". Google
>>>> will give you several good hits for tutorials.
>>>>
>>>> --
>>>> ==================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstucklex@attglobal.net
>>>> ==================
>>>>
>>>
>>> If it wasn't clear in my original message, this is NOT a real ecommerce
>>> site so security (from a sensitive financial data perspective) isn't an
>>> issue.
>>>
>>> I will continue to store the amounts in one column as positive/negative
>>> numbers.
>>>
>>> So the balance column isn't necessary. I can simply Sum() the amount
>>> column, but I am concerned about performance down the road. How would
>>> "starting balance for the month" work programmatically and data
>>> structure wise?
>>>
>>> It's true, the amount will indicate debit/credit. However I do need a
>>> type column to distinguish the type of credit/debit for reporting
>>> purposes.
>>>
>>> Thanks to everyone who responded.
>>>

>>
>> Do it like the banks do. On a regular basis, archive the existing
>> transactions and start with a new balance.
>>
>> Or, for instance, you might archive all transactions over six months old
>> and put in a starting balance as of the date.
>>
>> For instance, if you want to archive everything before 1 September, 2007:
>>
>> 1. Start a transaction (using INNODB database)
>> 2. Sum all the transactions up through 31 August and remember.
>> 3. Copy all transactions through 31 August to your archive table
>> 4. Delete all transactions through 31 August
>> 5. Insert a transaction dated 1 September with the balance from Step 2
>> 6. Commit the transaction
>>
>> How many rows are you planning to have in the table, anyway?
>>
>> And yes, I know you said it wasn't a real e-commerce site - that was more
>> for those who are doing e-commerce sites and reading this thread.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstucklex@attglobal.net
>> ==================
>>

>
> I stumbled across another challenge today. I need to create a ledger type
> report, which will include debit, credit and balance columns. The report
> will use pagination with one or more pages. The trick is how to display
> the running balance. As per the advice given here, I eliminated the
> running balance from the transaction table. I know a running total can be
> calculated per row in the query, but what with a range of rows using
> LIMIT?


After giving this matter some additional thought, it seems like the best way
to go about having a running balance with pagination is to use a separate
query to retrieve the beginning balance at the start of each page. For
example (assuming each page showed 20 records) page 1 retrieves records
1-20, page 2 retrieves records 1-40 and so on. The running balance would be
calculated as the page is being built. This method adds one additional query
per page, which isn't bad.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-10-2008, 04:55 PM
Michael Austin
 
Posts: n/a
Default Re: best practice: storing credit/debits

Keychain wrote:
> "Keychain" <someone@somewhere.com> wrote in message
> news:ZpmdnUZj29vA8k7anZ2dnUVZ_jCdnZ2d@comcast.com. ..
>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>> news:E8OdnRTiUNNBMFjanZ2dnUVZ_r2nnZ2d@comcast.com. ..
>>> Keychain wrote:
>>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>>> news:T9KdnXZO-7lxR1nanZ2dnUVZ_hKdnZ2d@comcast.com...
>>>>> Keychain wrote:
>>>>>> I need to create a table that records monetary-like
>>>>>> (non-ecommerce) transactions.
>>>>>>
>>>>>> The structure looks like this:
>>>>>>
>>>>>> tid, uid, amount, balance, type, dateline
>>>>>>
>>>>>> The data looks like this:
>>>>>>
>>>>>> 1, 1, -10, -10, debit, 1202651856
>>>>>> 2, 1, 3, -7, credit, 1202652000
>>>>>>
>>>>>> A running balance is kept, which makes it easy to pull the newest
>>>>>> record for the current balance.
>>>>>>
>>>>>
>>>>> Don't keep a running balance. Just total the amounts for an
>>>>> account. If the table gets too big, do it like the banks do - have
>>>>> a starting balance for the month and total from there.
>>>>>
>>>>>> The debit/credit amounts are stored as positive and negative values.
>>>>>>
>>>>>
>>>>> That's how I would do it.
>>>>>
>>>>>> Is there anything flawed about this approach?
>>>>>>
>>>>>
>>>>> Just ensure you are encrypting any sensitive information (i.e. if
>>>>> this were an ecommerce database, encrypt the credit card info).
>>>>>
>>>>>> Should the balance be kept in a separate table? Exp. table
>>>>>> structure: uid, balance
>>>>>>
>>>>>
>>>>> Nope. See above.
>>>>>
>>>>>> Should the amounts all be stored as absolute positive values?
>>>>>>
>>>>>
>>>>> Definitely NOT.
>>>>>
>>>>>> What about having separate debit and credit columns and skipping
>>>>>> the type indicator? This would mean one of the columns for each
>>>>>> record would always be zero.
>>>>>>
>>>>>
>>>>> Nope. And why do you need a type indicator? You can tell which
>>>>> one is is based on the amount being positive or negative.
>>>>>
>>>>>> Any other thoughts/suggestions?
>>>>>>
>>>>>> Thanks in advance.
>>>>>>
>>>>>
>>>>> KISS. Don't add more data than you need, and definitely don't
>>>>> duplicate data. Try reading up on "database normalization".
>>>>> Google will give you several good hits for tutorials.
>>>>>
>>>>> --
>>>>> ==================
>>>>> Remove the "x" from my email address
>>>>> Jerry Stuckle
>>>>> JDS Computer Training Corp.
>>>>> jstucklex@attglobal.net
>>>>> ==================
>>>>>
>>>>
>>>> If it wasn't clear in my original message, this is NOT a real
>>>> ecommerce site so security (from a sensitive financial data
>>>> perspective) isn't an issue.
>>>>
>>>> I will continue to store the amounts in one column as
>>>> positive/negative numbers.
>>>>
>>>> So the balance column isn't necessary. I can simply Sum() the amount
>>>> column, but I am concerned about performance down the road. How
>>>> would "starting balance for the month" work programmatically and
>>>> data structure wise?
>>>>
>>>> It's true, the amount will indicate debit/credit. However I do need
>>>> a type column to distinguish the type of credit/debit for reporting
>>>> purposes.
>>>>
>>>> Thanks to everyone who responded.
>>>>
>>>
>>> Do it like the banks do. On a regular basis, archive the existing
>>> transactions and start with a new balance.
>>>
>>> Or, for instance, you might archive all transactions over six months
>>> old and put in a starting balance as of the date.
>>>
>>> For instance, if you want to archive everything before 1 September,
>>> 2007:
>>>
>>> 1. Start a transaction (using INNODB database)
>>> 2. Sum all the transactions up through 31 August and remember.
>>> 3. Copy all transactions through 31 August to your archive table
>>> 4. Delete all transactions through 31 August
>>> 5. Insert a transaction dated 1 September with the balance from Step 2
>>> 6. Commit the transaction
>>>
>>> How many rows are you planning to have in the table, anyway?
>>>
>>> And yes, I know you said it wasn't a real e-commerce site - that was
>>> more for those who are doing e-commerce sites and reading this thread.
>>>
>>> --
>>> ==================
>>> Remove the "x" from my email address
>>> Jerry Stuckle
>>> JDS Computer Training Corp.
>>> jstucklex@attglobal.net
>>> ==================
>>>

>>
>> I stumbled across another challenge today. I need to create a ledger
>> type report, which will include debit, credit and balance columns. The
>> report will use pagination with one or more pages. The trick is how to
>> display the running balance. As per the advice given here, I
>> eliminated the running balance from the transaction table. I know a
>> running total can be calculated per row in the query, but what with a
>> range of rows using LIMIT?

>
> After giving this matter some additional thought, it seems like the best
> way to go about having a running balance with pagination is to use a
> separate query to retrieve the beginning balance at the start of each
> page. For example (assuming each page showed 20 records) page 1
> retrieves records 1-20, page 2 retrieves records 1-40 and so on. The
> running balance would be calculated as the page is being built. This
> method adds one additional query per page, which isn't bad.


It also sounds like you really need to do what should have been done in
the first place. Sit down with the "customer" and determine all of the
absolute requirements for the project. This is a lost art in today's
business where RAD (rapid application development outweighs multiple
ReRAD (re doing the same application over and over because someone can't
make up their mind or truly do not know what they want).

What you really need is a report-writer not just the SQL.
Report-writers use multiple sql-statements to "build" the report to suit
the requirements (Think billing/invoice statements from your vendors or
that "pretty-looking" report for the bean-counters/execs).

Oracle incorporated a lot of the page formating syntax to build those
sorts of reports and pagination etc... and I am sure some companies
still use those reports, but I do not see that sort of formating within
the MySQL engine(s) - I have not read the docs cover-to-cover yet...
but, I also could not find those sort of things using google or the
search feature on the MySQL site either...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-10-2008, 04:55 PM
Keychain
 
Posts: n/a
Default Re: best practice: storing credit/debits

"Michael Austin" <maustin@firstdbasource.com> wrote in message
news:MnXAj.25487$J41.21661@newssvr14.news.prodigy. net...
> Keychain wrote:
>> "Keychain" <someone@somewhere.com> wrote in message
>> news:ZpmdnUZj29vA8k7anZ2dnUVZ_jCdnZ2d@comcast.com. ..
>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>> news:E8OdnRTiUNNBMFjanZ2dnUVZ_r2nnZ2d@comcast.com. ..
>>>> Keychain wrote:
>>>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>>>> news:T9KdnXZO-7lxR1nanZ2dnUVZ_hKdnZ2d@comcast.com...
>>>>>> Keychain wrote:
>>>>>>> I need to create a table that records monetary-like (non-ecommerce)
>>>>>>> transactions.
>>>>>>>
>>>>>>> The structure looks like this:
>>>>>>>
>>>>>>> tid, uid, amount, balance, type, dateline
>>>>>>>
>>>>>>> The data looks like this:
>>>>>>>
>>>>>>> 1, 1, -10, -10, debit, 1202651856
>>>>>>> 2, 1, 3, -7, credit, 1202652000
>>>>>>>
>>>>>>> A running balance is kept, which makes it easy to pull the newest
>>>>>>> record for the current balance.
>>>>>>>
>>>>>>
>>>>>> Don't keep a running balance. Just total the amounts for an account.
>>>>>> If the table gets too big, do it like the banks do - have a starting
>>>>>> balance for the month and total from there.
>>>>>>
>>>>>>> The debit/credit amounts are stored as positive and negative values.
>>>>>>>
>>>>>>
>>>>>> That's how I would do it.
>>>>>>
>>>>>>> Is there anything flawed about this approach?
>>>>>>>
>>>>>>
>>>>>> Just ensure you are encrypting any sensitive information (i.e. if
>>>>>> this were an ecommerce database, encrypt the credit card info).
>>>>>>
>>>>>>> Should the balance be kept in a separate table? Exp. table
>>>>>>> structure: uid, balance
>>>>>>>
>>>>>>
>>>>>> Nope. See above.
>>>>>>
>>>>>>> Should the amounts all be stored as absolute positive values?
>>>>>>>
>>>>>>
>>>>>> Definitely NOT.
>>>>>>
>>>>>>> What about having separate debit and credit columns and skipping the
>>>>>>> type indicator? This would mean one of the columns for each record
>>>>>>> would always be zero.
>>>>>>>
>>>>>>
>>>>>> Nope. And why do you need a type indicator? You can tell which one
>>>>>> is is based on the amount being positive or negative.
>>>>>>
>>>>>>> Any other thoughts/suggestions?
>>>>>>>
>>>>>>> Thanks in advance.
>>>>>>>
>>>>>>
>>>>>> KISS. Don't add more data than you need, and definitely don't
>>>>>> duplicate data. Try reading up on "database normalization". Google
>>>>>> will give you several good hits for tutorials.
>>>>>>
>>>>>> --
>>>>>> ==================
>>>>>> Remove the "x" from my email address
>>>>>> Jerry Stuckle
>>>>>> JDS Computer Training Corp.
>>>>>> jstucklex@attglobal.net
>>>>>> ==================
>>>>>>
>>>>>
>>>>> If it wasn't clear in my original message, this is NOT a real
>>>>> ecommerce site so security (from a sensitive financial data
>>>>> perspective) isn't an issue.
>>>>>
>>>>> I will continue to store the amounts in one column as
>>>>> positive/negative numbers.
>>>>>
>>>>> So the balance column isn't necessary. I can simply Sum() the amount
>>>>> column, but I am concerned about performance down the road. How would
>>>>> "starting balance for the month" work programmatically and data
>>>>> structure wise?
>>>>>
>>>>> It's true, the amount will indicate debit/credit. However I do need a
>>>>> type column to distinguish the type of credit/debit for reporting
>>>>> purposes.
>>>>>
>>>>> Thanks to everyone who responded.
>>>>>
>>>>
>>>> Do it like the banks do. On a regular basis, archive the existing
>>>> transactions and start with a new balance.
>>>>
>>>> Or, for instance, you might archive all transactions over six months
>>>> old and put in a starting balance as of the date.
>>>>
>>>> For instance, if you want to archive everything before 1 September,
>>>> 2007:
>>>>
>>>> 1. Start a transaction (using INNODB database)
>>>> 2. Sum all the transactions up through 31 August and remember.
>>>> 3. Copy all transactions through 31 August to your archive table
>>>> 4. Delete all transactions through 31 August
>>>> 5. Insert a transaction dated 1 September with the balance from Step 2
>>>> 6. Commit the transaction
>>>>
>>>> How many rows are you planning to have in the table, anyway?
>>>>
>>>> And yes, I know you said it wasn't a real e-commerce site - that was
>>>> more for those who are doing e-commerce sites and reading this thread.
>>>>
>>>> --
>>>> ==================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstucklex@attglobal.net
>>>> ==================
>>>>
>>>
>>> I stumbled across another challenge today. I need to create a ledger
>>> type report, which will include debit, credit and balance columns. The
>>> report will use pagination with one or more pages. The trick is how to
>>> display the running balance. As per the advice given here, I eliminated
>>> the running balance from the transaction table. I know a running total
>>> can be calculated per row in the query, but what with a range of rows
>>> using LIMIT?

>>
>> After giving this matter some additional thought, it seems like the best
>> way to go about having a running balance with pagination is to use a
>> separate query to retrieve the beginning balance at the start of each
>> page. For example (assuming each page showed 20 records) page 1 retrieves
>> records 1-20, page 2 retrieves records 1-40 and so on. The running
>> balance would be calculated as the page is being built. This method adds
>> one additional query per page, which isn't bad.

>
> It also sounds like you really need to do what should have been done in
> the first place. Sit down with the "customer" and determine all of the
> absolute requirements for the project. This is a lost art in today's
> business where RAD (rapid application development outweighs multiple ReRAD
> (re doing the same application over and over because someone can't make up
> their mind or truly do not know what they want).
>
> What you really need is a report-writer not just the SQL. Report-writers
> use multiple sql-statements to "build" the report to suit the requirements
> (Think billing/invoice statements from your vendors or that
> "pretty-looking" report for the bean-counters/execs).
>
> Oracle incorporated a lot of the page formating syntax to build those
> sorts of reports and pagination etc... and I am sure some companies still
> use those reports, but I do not see that sort of formating within the
> MySQL engine(s) - I have not read the docs cover-to-cover yet... but, I
> also could not find those sort of things using google or the search
> feature on the MySQL site either...


Thanks for the response, but I don't need a lecture about how to scope a
project. Your answer has absolutely nothing to do with my question (or the
thread.) Perhaps you should take your own advice and take the time to
educate yourself before jumping to conclusions.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-10-2008, 04:55 PM
Jerry Stuckle
 
Posts: n/a
Default Re: best practice: storing credit/debits

Keychain wrote:
> "Michael Austin" <maustin@firstdbasource.com> wrote in message
> news:MnXAj.25487$J41.21661@newssvr14.news.prodigy. net...
>> Keychain wrote:
>>> "Keychain" <someone@somewhere.com> wrote in message
>>> news:ZpmdnUZj29vA8k7anZ2dnUVZ_jCdnZ2d@comcast.com. ..
>>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>>> news:E8OdnRTiUNNBMFjanZ2dnUVZ_r2nnZ2d@comcast.com. ..
>>>>> Keychain wrote:
>>>>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>>>>> news:T9KdnXZO-7lxR1nanZ2dnUVZ_hKdnZ2d@comcast.com...
>>>>>>> Keychain wrote:
>>>>>>>> I need to create a table that records monetary-like
>>>>>>>> (non-ecommerce) transactions.
>>>>>>>>
>>>>>>>> The structure looks like this:
>>>>>>>>
>>>>>>>> tid, uid, amount, balance, type, dateline
>>>>>>>>
>>>>>>>> The data looks like this:
>>>>>>>>
>>>>>>>> 1, 1, -10, -10, debit, 1202651856
>>>>>>>> 2, 1, 3, -7, credit, 1202652000
>>>>>>>>
>>>>>>>> A running balance is kept, which makes it easy to pull the
>>>>>>>> newest record for the current balance.
>>>>>>>>
>>>>>>>
>>>>>>> Don't keep a running balance. Just total the amounts for an
>>>>>>> account. If the table gets too big, do it like the banks do -
>>>>>>> have a starting balance for the month and total from there.
>>>>>>>
>>>>>>>> The debit/credit amounts are stored as positive and negative
>>>>>>>> values.
>>>>>>>>
>>>>>>>
>>>>>>> That's how I would do it.
>>>>>>>
>>>>>>>> Is there anything flawed about this approach?
>>>>>>>>
>>>>>>>
>>>>>>> Just ensure you are encrypting any sensitive information (i.e. if
>>>>>>> this were an ecommerce database, encrypt the credit card info).
>>>>>>>
>>>>>>>> Should the balance be kept in a separate table? Exp. table
>>>>>>>> structure: uid, balance
>>>>>>>>
>>>>>>>
>>>>>>> Nope. See above.
>>>>>>>
>>>>>>>> Should the amounts all be stored as absolute positive values?
>>>>>>>>
>>>>>>>
>>>>>>> Definitely NOT.
>>>>>>>
>>>>>>>> What about having separate debit and credit columns and skipping
>>>>>>>> the type indicator? This would mean one of the columns for each
>>>>>>>> record would always be zero.
>>>>>>>>
>>>>>>>
>>>>>>> Nope. And why do you need a type indicator? You can tell which
>>>>>>> one is is based on the amount being positive or negative.
>>>>>>>
>>>>>>>> Any other thoughts/suggestions?
>>>>>>>>
>>>>>>>> Thanks in advance.
>>>>>>>>
>>>>>>>
>>>>>>> KISS. Don't add more data than you need, and definitely don't
>>>>>>> duplicate data. Try reading up on "database normalization".
>>>>>>> Google will give you several good hits for tutorials.
>>>>>>>
>>>>>>> --
>>>>>>> ==================
>>>>>>> Remove the "x" from my email address
>>>>>>> Jerry Stuckle
>>>>>>> JDS Computer Training Corp.
>>>>>>> jstucklex@attglobal.net
>>>>>>> ==================
>>>>>>>
>>>>>>
>>>>>> If it wasn't clear in my original message, this is NOT a real
>>>>>> ecommerce site so security (from a sensitive financial data
>>>>>> perspective) isn't an issue.
>>>>>>
>>>>>> I will continue to store the amounts in one column as
>>>>>> positive/negative numbers.
>>>>>>
>>>>>> So the balance column isn't necessary. I can simply Sum() the
>>>>>> amount column, but I am concerned about performance down the road.
>>>>>> How would "starting balance for the month" work programmatically
>>>>>> and data structure wise?
>>>>>>
>>>>>> It's true, the amount will indicate debit/credit. However I do
>>>>>> need a type column to distinguish the type of credit/debit for
>>>>>> reporting purposes.
>>>>>>
>>>>>> Thanks to everyone who responded.
>>>>>>
>>>>>
>>>>> Do it like the banks do. On a regular basis, archive the existing
>>>>> transactions and start with a new balance.
>>>>>
>>>>> Or, for instance, you might archive all transactions over six
>>>>> months old and put in a starting balance as of the date.
>>>>>
>>>>> For instance, if you want to archive everything before 1 September,
>>>>> 2007:
>>>>>
>>>>> 1. Start a transaction (using INNODB database)
>>>>> 2. Sum all the transactions up through 31 August and remember.
>>>>> 3. Copy all transactions through 31 August to your archive table
>>>>> 4. Delete all transactions through 31 August
>>>>> 5. Insert a transaction dated 1 September with the balance from Step 2
>>>>> 6. Commit the transaction
>>>>>
>>>>> How many rows are you planning to have in the table, anyway?
>>>>>
>>>>> And yes, I know you said it wasn't a real e-commerce site - that
>>>>> was more for those who are doing e-commerce sites and reading this
>>>>> thread.
>>>>>
>>>>> --
>>>>> ==================
>>>>> Remove the "x" from my email address
>>>>> Jerry Stuckle
>>>>> JDS Computer Training Corp.
>>>>> jstucklex@attglobal.net
>>>>> ==================
>>>>>
>>>>
>>>> I stumbled across another challenge today. I need to create a ledger
>>>> type report, which will include debit, credit and balance columns.
>>>> The report will use pagination with one or more pages. The trick is
>>>> how to display the running balance. As per the advice given here, I
>>>> eliminated the running balance from the transaction table. I know a
>>>> running total can be calculated per row in the query, but what with
>>>> a range of rows using LIMIT?
>>>
>>> After giving this matter some additional thought, it seems like the
>>> best way to go about having a running balance with pagination is to
>>> use a separate query to retrieve the beginning balance at the start
>>> of each page. For example (assuming each page showed 20 records) page
>>> 1 retrieves records 1-20, page 2 retrieves records 1-40 and so on.
>>> The running balance would be calculated as the page is being built.
>>> This method adds one additional query per page, which isn't bad.

>>
>> It also sounds like you really need to do what should have been done
>> in the first place. Sit down with the "customer" and determine all of
>> the absolute requirements for the project. This is a lost art in
>> today's business where RAD (rapid application development outweighs
>> multiple ReRAD (re doing the same application over and over because
>> someone can't make up their mind or truly do not know what they want).
>>
>> What you really need is a report-writer not just the SQL.
>> Report-writers use multiple sql-statements to "build" the report to
>> suit the requirements (Think billing/invoice statements from your
>> vendors or that "pretty-looking" report for the bean-counters/execs).
>>
>> Oracle incorporated a lot of the page formating syntax to build those
>> sorts of reports and pagination etc... and I am sure some companies
>> still use those reports, but I do not see that sort of formating
>> within the MySQL engine(s) - I have not read the docs cover-to-cover
>> yet... but, I also could not find those sort of things using google
>> or the search feature on the MySQL site either...

>
> Thanks for the response, but I don't need a lecture about how to scope a
> project. Your answer has absolutely nothing to do with my question (or
> the thread.) Perhaps you should take your own advice and take the time
> to educate yourself before jumping to conclusions.
>
>


Actually, I think Michael is quite correct. These are the types of
questions which make a difference in a project. In some cases, it
changes the database design; it can even affect the selection of the
RDBMS you should be using.

I also agree with him that now you need a report writer. I'd be looking
at another language to write the report and not try to do it all in SQL.

You probably could do it all in SQL, but it wouldn't be easy.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-28-2008, 04:40 AM
Keychain
 
Posts: n/a
Default Re: best practice: storing credit/debits

"Keychain" <someone@somewhere.com> wrote in message
news:t5WdnbKr-pzjhEnanZ2dnUVZ_uevnZ2d@comcast.com...
> "Keychain" <someone@somewhere.com> wrote in message
> news:ZpmdnUZj29vA8k7anZ2dnUVZ_jCdnZ2d@comcast.com. ..
>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>> news:E8OdnRTiUNNBMFjanZ2dnUVZ_r2nnZ2d@comcast.com. ..
>>> Keychain wrote:
>>>> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
>>>> news:T9KdnXZO-7lxR1nanZ2dnUVZ_hKdnZ2d@comcast.com...
>>>>> Keychain wrote:
>>>>>> I need to create a table that records monetary-like (non-ecommerce)
>>>>>> transactions.
>>>>>>
>>>>>> The structure looks like this:
>>>>>>
>>>>>> tid, uid, amount, balance, type, dateline
>>>>>>
>>>>>> The data looks like this:
>>>>>>
>>>>>> 1, 1, -10, -10, debit, 1202651856
>>>>>> 2, 1, 3, -7, credit, 1202652000
>>>>>>
>>>>>> A running balance is kept, which makes it easy to pull the newest
>>>>>> record for the current balance.
>>>>>>
>>>>>
>>>>> Don't keep a running balance. Just total the amounts for an account.
>>>>> If the table gets too big, do it like the banks do - have a starting
>>>>> balance for the month and total from there.
>>>>>
>>>>>> The debit/credit amounts are stored as positive and negative values.
>>>>>>
>>>>>
>>>>> That's how I would do it.
>>>>>
>>>>>> Is there anything flawed about this approach?
>>>>>>
>>>>>
>>>>> Just ensure you are encrypting any sensitive information (i.e. if this
>>>>> were an ecommerce database, encrypt the credit card info).
>>>>>
>>>>>> Should the balance be kept in a separate table? Exp. table structure:
>>>>>> uid, balance
>>>>>>
>>>>>
>>>>> Nope. See above.
>>>>>
>>>>>> Should the amounts all be stored as absolute positive values?
>>>>>>
>>>>>
>>>>> Definitely NOT.
>>>>>
>>>>>> What about having separate debit and credit columns and skipping the
>>>>>> type indicator? This would mean one of the columns for each record
>>>>>> would always be zero.
>>>>>>
>>>>>
>>>>> Nope. And why do you need a type indicator? You can tell which one
>>>>> is is based on the amount being positive or negative.
>>>>>
>>>>>> Any other thoughts/suggestions?
>>>>>>
>>>>>> Thanks in advance.
>>>>>>
>>>>>
>>>>> KISS. Don't add more data than you need, and definitely don't
>>>>> duplicate data. Try reading up on "database normalization". Google
>>>>> will give you several good hits for tutorials.
>>>>>
>>>>> --
>>>>> ==================
>>>>> Remove the "x" from my email address
>>>>> Jerry Stuckle
>>>>> JDS Computer Training Corp.
>>>>> jstucklex@attglobal.net
>>>>> ==================
>>>>>
>>>>
>>>> If it wasn't clear in my original message, this is NOT a real ecommerce
>>>> site so security (from a sensitive financial data perspective) isn't an
>>>> issue.
>>>>
>>>> I will continue to store the amounts in one column as positive/negative
>>>> numbers.
>>>>
>>>> So the balance column isn't necessary. I can simply Sum() the amount
>>>> column, but I am concerned about performance down the road. How would
>>>> "starting balance for the month" work programmatically and data
>>>> structure wise?
>>>>
>>>> It's true, the amount will indicate debit/credit. However I do need a
>>>> type column to distinguish the type of credit/debit for reporting
>>>> purposes.
>>>>
>>>> Thanks to everyone who responded.
>>>>
>>>
>>> Do it like the banks do. On a regular basis, archive the existing
>>> transactions and start with a new balance.
>>>
>>> Or, for instance, you might archive all transactions over six months old
>>> and put in a starting balance as of the date.
>>>
>>> For instance, if you want to archive everything before 1 September,
>>> 2007:
>>>
>>> 1. Start a transaction (using INNODB database)
>>> 2. Sum all the transactions up through 31 August and remember.
>>> 3. Copy all transactions through 31 August to your archive table
>>> 4. Delete all transactions through 31 August
>>> 5. Insert a transaction dated 1 September with the balance from Step 2
>>> 6. Commit the transaction
>>>
>>> How many rows are you planning to have in the table, anyway?
>>>
>>> And yes, I know you said it wasn't a real e-commerce site - that was
>>> more for those who are doing e-commerce sites and reading this thread.
>>>
>>> --
>>> ==================
>>> Remove the "x" from my email address
>>> Jerry Stuckle
>>> JDS Computer Training Corp.
>>> jstucklex@attglobal.net
>>> ==================
>>>

>>
>> I stumbled across another challenge today. I need to create a ledger type
>> report, which will include debit, credit and balance columns. The report
>> will use pagination with one or more pages. The trick is how to display
>> the running balance. As per the advice given here, I eliminated the
>> running balance from the transaction table. I know a running total can be
>> calculated per row in the query, but what with a range of rows using
>> LIMIT?

>
> After giving this matter some additional thought, it seems like the best
> way to go about having a running balance with pagination is to use a
> separate query to retrieve the beginning balance at the start of each
> page. For example (assuming each page showed 20 records) page 1 retrieves
> records 1-20, page 2 retrieves records 1-40 and so on. The running balance
> would be calculated as the page is being built. This method adds one
> additional query per page, which isn't bad.


A quick follow-up.

It turns out the BEST way to go about this is calculate a running balance
using a user-defined variable in a temporary table query. It's then easy to
return a range of records for pagination.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:26 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372