Brian:
The Orderform is modeled after the NW. It contains a subform for products
just like the NW. The ordertotal is actually the total of the product +
delivery fee, which we use to quote to the customer for an approximate price
of their order. The products are somewhat irrelavant to me as we do not
stock anything. We merely pick whatever product a customer may order up from
a/any store, pay for it, and then tack on our delivery fee. We collect the
money from the customer at the door. I keep a table of known ordered products
with their last known price as a rough quide in quotes to a customer. The
OrderTotal field is primarily for the orderform. There is No data being
stored in that field in the table. Why then do I have such a field? I don't
know. But on the form, which is where I need it to, the orderstotal works
fine.
If the OrderTotal is an unbound computed *control* on a form, or a control
bound to a computed column in the form's underlying query, that's fine, but
in your response to John you showed it as a column of currency data type in
the OrdersTbl table, which it should not be. But don't get too hung up on
the form; that's only a window on the underlying data. It’s how the data is
arranged, the 'logical model', which is the key. The forms and reports will
fall into place easily if the logical model is right.
I'm not sure about this. The delivery charges and the proportions that go to
the driver and the company often vary from order to order and are necessary,
on the orderfrom as well as a table to keep track of respective earnings. Why
do I need a charge table when the employee&Order tables currently has all
those fields?
The short answer is that this is how its done most easily and efficiently in
a relational database. Its not the presence of these multiple charge columns
in controls on the form that's the issue, it’s having multiple columns for
the various different types of charges in the table. By having the charges in
a separate table related with a foreign key OrderID column the Orders form
can then have a charges subform in which each charge amount is entered in a
single column and the relevant charge type selected in another column, with
complete flexibility over the number and type of charges applied per order.
When aggregating the values you then work with a single column of currency
data. The total amount per order for instance is the Sum of the column's
values grouped by OrderID, the average amount per day for instance is the Avg
of the column grouped by order date (from the referenced Orders table), and
so on. If you want aggregated values for each driver then you group by
EmployeeID (again from the referenced Orders table).
Yes they do contain names. As I earlier mentioned, they are a separate
problem I was planning on addressing later after I fiqured out the Breakdown
of Calculations part. Basically my problem is if I link them up with the
employeeID, only one employee gets credited with the order. ie: OrderTakenBy
and DespatchedBy and Dispatched To(which is currently set up to employeeId).
3 fields, 3 diff names but only one receives all the data. In other words, 3
emplyees but only 1 employee number shows up for all/every records.
Complicated to explain, thats why I thought I'd tackle it later.
The point here is that the OrderTakenBy, DespatchedBy and DispatchedTo
columns should each contain an EmployeeID value, not a name because of the
non-uniqueness of names (remember my two Maggie Taylors – you might have only
one of her now but the second could come along any day!). When referencing
another table, Employees here, you should always reference its primary key,
not a non-key column. Take Maggie, if I referenced her by name in two rows
in a table, whether it’s the same Maggie in each or not, and then joined that
table to Employees on the name column(s) the query would return 4 rows and if
I summed any values each would be double counted, which might make my figures
look good, but would not be the truth. If I reference her by her EmployeeID,
however, then only two rows are returned, the figures are correct and I don't
have the District Auditor asking awkward questions.
Most if not all of our orders are initially taken by hand and later entered
into the database. We enter in the time/date the order was taken, which may
have been a day or two earlier. We need to enter them both separately. I will
be using the time of day and not necessarily the date for future frequent
calculations/predictions of busy time periods.
You can still have the time and date in one column. For computations based
on the time of day you can return the time of day from the single date/time
value with the TimeValue function. The issue is not an unduly significant
one however.
This is my initial question.
What are the queries I need to calculate these fields?
Lets take a simple one as an example, which hopefully will illustrate the
benefits of the Charges table with a single Amount column, the
TotalEarningsForCallsCompleted, which I assume to be the sum of all charges
for all orders per employee:
SELECT FirstName, LastName,
SUM(Amount) As TotalEarnings
FROM Employees INNER JOIN
(Orders INNER JOIN Charges
ON Orders.OrderID = Charges.OrderID)
ON Employees.EmployeeID = Orders.EmployeeID
GROUP BY FirstName, LastName;
As you'll see this is a simple summing of the Amount column, whereas with
multiple columns the values from each would have to be added then summed.
That would not be too difficult in this case, but when it comes to more
complex aggregations then things could get very messy.
Ken Sheridan
Stafford, England