views:

35

answers:

2

(Sorry about the vagueness of the title; I can't think how to really say what I'm looking for without writing a book.)

So in our app, we allow users to change key pieces of data. I'm keeping records of who changed what when in a log schema, but now the problem presents itself: how do I best represent that data in a view for reporting?

An example will help: a customer's data (say, billing address) changed on 4/4/09. Let's say that today, 10/19/09, I want to see all of their 2009 orders, before and after the change. I also want each order to display the billing address that was current as of the date of the order.

So I have 4 tables:

Orders (with order data) Customers (with current customer data) CustomerOrders (linking the two) CustomerChange (which holds the date of the change, who made the change (employee id), what the old billing address was, and what they changed it to)

How do I best structure a view to be used by reporting so that the proper address is returned? Or am I better served by creating a reporting database and denormalizing the data there, which is what the reports group is requesting?

A: 

There is no need for a separate DB if this is the only thing you are going to do. You could just create a de-normalized table/cube...and populate and retrieve from it. If your data is voluminous apply proper indexes on this table.

Shankar Ramachandran
Good points, but I figured out a way to do what I was looking for. By using CTEs in the view and filtering by a MIN(DateDiff) that's still >= 0, I was able to pull in the appropriate data from the different tables.Performance testing indicates it's kind of a wash between the denormalized data and the view; I wasn't really expecting THAT!Since reporting (and their mgmt, who is the true burr under my saddle) still want denormalized data, though, I think I'm going to segregate it out into a separate instance to minimize the impact of having a HUGE denormalized table on an application db.
Valkyrie
A: 

Personally I would design this so you don't need the change table for the report. It is a bad practice to store an order without all the data as of the date of the order stored in a table. You lookup the address from the address table and store it with the order (same for partnumbers and company names and anything that changes over time.) You never get information on an order by joining to customer, address, part numbers, price tables etc.

Audit tables are more for fixing bad changes or looking up who made them than for reporting.

HLGEM