SQL For Programmers – New Question

tp_vol3_007 I got this question last week that I thought would be worth discussing on the blog.

I would like to create a table that lists contributions by date, amount, and designation. In some cases one gift was split two (or more) ways. For example, a contribution of $1000 was given, and $500 went to the scholarship fund and $500 to athletics. The gift date would be the same for both gifts, but they must be recorded separately in order to split the gift. In my new table, I want to list them in rows like this:
date| amount| designation1, designation2, designation3, etc.

Would it be best to use a cursor or a stored procedure. I’ve never done anything like this before, so I’m not sure where to begin.

Um… using cursors WOULD be in a stored procedure, right?  But I think we are getting ahead of ourselves.

When I get these kinds of questions, I always have to put together the hidden pieces.  So I’m sorry if some of what I cover isn’t part of the question, but it is at least part of the answer.

First, let’s take a look at how you would store this kind of data in a database.  You should have two tables.  The first would be the transaction information:

Field Description
ID Unique transaction ID, probably auto incrementing integer.
Date Date the gift was given

The second table would hold the split information

Field Description
ID Unique split ID, also probably auto incrementing integer
TranactionID Links the record to the transaction row (above)
Amount The amount of the split
Description Description of the split


I’m not sure if the person who originally asked me the question was meaning to place the transaction and split information all in the same row in the same table or not, but I’ve seen enough new developers try to do this to at least make me aware that this is a possibility.

The reason you don’t want to put it in there, even if you could pre-determine the maximum number of splits, is because at some point in the future, there is a very real likelihood that the number will grow.  My rule is, “if there is N of something, where N > 1, then there will be N + 1 of that same thing.”  Therefore, you always write your code to handle multiple items as soon as you know there will be more than one.  It takes no longer to code and will more likely than not save you time in the future.

For the very same reason, you don’t want to display it as though it all came out of one row, either.  You’d either need to dynamically create your display to show multiple columns as the maximum number grew, or you’d need to modify the code as it grew.  Neither is a very good solution.

So how would you display this information, then?

As a master detail report

Date   Total
           Amount    Description

Date Total
Amount Description
05/06/09 $100.00  
  $50.00 Fund One
  $50.00 Fund Two
05/07/09 $75.00  
  $75.00 Some place else


Store procedure or cursor?  You should be able to produce a report like this using two store procedures–one to get the list of transactions and another to get the list of splits for a specific transaction.

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail