473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Proper Query Summing

So I have an invoicing database based on two main forms: Orders and
OrderLines. Orders has fields like:

OrderID
BillingMethod
OrderDate
CreditCard
CCExp
OrdSubTotal
ShippingCharge
SalesTax
OrdTotal

OrderLines has fields like:

LineID
ProductType
Product
Price
Quantity
LineSubtotal
OrderID (relates back to OrderID in orders)

The ProductType field is a combo box based on a table called TypeList
that lists all the different types of products we offer, along with
what Department sells each product. Each Department only sells their
own type of Products, so you won't ever see an invoice with products
from multiple departments.

So on the Orders form, users can enter the basic order info and in the
OrderLines subform they enter as many different products as they want
on the invoice.

The Order table is linked to a separate Batch table so that all the
invoices for a particular day are entered into that day's invoice
batch.

Access 101, right? My problem comes with a report I want to run.

At the end of the day, I want a report based on a query that will show
me how much money was brought in by each Department.

So I set up my query with the Orders, OrderLines, and TypeList tables.
I relate the OrderIDs between the two Order Tables, and the
ProductTypeIDs between the OrderLines and TypeList tables.

I set up a where statement so only Orders from the current day's batch
appear in the query. This works fine. The problem is, I tell it to
Group By the Department field in the TypeList table, and Sum the
OrdTotal field from the Orders table. However, it's clearly
calculating many of the orders TWICE. When I turn off the query's
TOTALS option, I get a Select query that looks something like this:

Department OrdTotal
Database Subscription $995.00
Online $49.95
Online $49.95
Research $151.69
Research $151.69
Research $151.69
Online $49.95
Online $49.95
Online $49.95
Research $200.44
Research $200.44
Research $200.44
Research $200.44
Research $200.44
Research $134.64
Research $134.64
Research $134.64

Clearly it doesn't like the fact that I'm grouping by a value in one
table and trying to sum the values in another table.

So how SHOULD I be doing this?

Thanks!

Mar 24 '07 #1
4 2509
da*******@gmail.com wrote:
So I have an invoicing database based on two main forms: Orders and
OrderLines. Orders has fields like:

OrderID
BillingMethod
OrderDate
CreditCard
CCExp
OrdSubTotal
ShippingCharge
SalesTax
OrdTotal

OrderLines has fields like:

LineID
ProductType
Product
Price
Quantity
LineSubtotal
OrderID (relates back to OrderID in orders)

The ProductType field is a combo box based on a table called TypeList
that lists all the different types of products we offer, along with
what Department sells each product. Each Department only sells their
own type of Products, so you won't ever see an invoice with products
from multiple departments.

So on the Orders form, users can enter the basic order info and in the
OrderLines subform they enter as many different products as they want
on the invoice.

The Order table is linked to a separate Batch table so that all the
invoices for a particular day are entered into that day's invoice
batch.

Access 101, right? My problem comes with a report I want to run.

At the end of the day, I want a report based on a query that will show
me how much money was brought in by each Department.

So I set up my query with the Orders, OrderLines, and TypeList tables.
I relate the OrderIDs between the two Order Tables, and the
ProductTypeIDs between the OrderLines and TypeList tables.

I set up a where statement so only Orders from the current day's batch
appear in the query. This works fine. The problem is, I tell it to
Group By the Department field in the TypeList table, and Sum the
OrdTotal field from the Orders table. However, it's clearly
calculating many of the orders TWICE. When I turn off the query's
TOTALS option, I get a Select query that looks something like this:

Department OrdTotal
Database Subscription $995.00
Online $49.95
Online $49.95
Research $151.69
Research $151.69
Research $151.69
Online $49.95
Online $49.95
Online $49.95
Research $200.44
Research $200.44
Research $200.44
Research $200.44
Research $200.44
Research $134.64
Research $134.64
Research $134.64

Clearly it doesn't like the fact that I'm grouping by a value in one
table and trying to sum the values in another table.
Although you gave a detailed description of your problem it wasn't an
adequate description.

What is the above? Detail? Is it correct? Do you want to see detail?
Or summary in the report? Or both?

If you use a nonTotals query are the results correct? Or are there
duplicates?

If you build a report you can set it up to be a summary report or detail
report.

I think you'd be better off creating a Select query than a Totals query.

If you have incorrect results, perhaps you've designed the query wrong.
But then, we don't see the SQL statement so we'd just be guessing.
>
So how SHOULD I be doing this?

Thanks!
Mar 24 '07 #2
Those are the results of the following query:

SELECT Typelist.Department, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.OrderID) ON TypeList.ProductType =
qryOrdLines.ProductType
WHERE (((Order.OrdBatchID)=[Forms]![Batch]![OrdBatchID]));

The results represent six actual invoices (six records in the Orders
table), but as you can see, because Department is tied to a field in
the OrderLINES table, the OrdTotal field from each of the six invoices
is being repeated for each record in the OrderLines table.

There are a couple of less-than-perfect solutions, as I see it:

1) Instead of using the OrdTotal value from the Orders table, just sum
the SubTotal values from the OrdLines table. The problem is, this
leaves out shipping and sales tax. I could list shipping and sales tax
as actual line items on the invoice, but that could screw a lot of
things up.

2) I could tie each invoice to a Department, rather than each line
item to a Department, but there are occasions where multiple
departments contribute to one invoice.

I hope this helps clarify things. Thanks!

Mar 24 '07 #3
da*******@gmail.com wrote:
Those are the results of the following query:

SELECT Typelist.Department, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.OrderID) ON TypeList.ProductType =
qryOrdLines.ProductType
WHERE (((Order.OrdBatchID)=[Forms]![Batch]![OrdBatchID]));

The results represent six actual invoices (six records in the Orders
table), but as you can see, because Department is tied to a field in
the OrderLINES table, the OrdTotal field from each of the six invoices
is being repeated for each record in the OrderLines table.

There are a couple of less-than-perfect solutions, as I see it:

1) Instead of using the OrdTotal value from the Orders table, just sum
the SubTotal values from the OrdLines table. The problem is, this
leaves out shipping and sales tax. I could list shipping and sales tax
as actual line items on the invoice, but that could screw a lot of
things up.

2) I could tie each invoice to a Department, rather than each line
item to a Department, but there are occasions where multiple
departments contribute to one invoice.

I hope this helps clarify things. Thanks!
I understand why you'd get multiple departments now.

Since your shipping and tax is rolled up per order and you can have
multiple departments in one order I'd say you are SOL. I suppose you
want to present the report by each department too.

It's easy enough using your scenario to get the tax. But the shipping
calc may not be as easy. I suppose you could get a percentage of the
shipping per department/orderitem and calc it out but due to rounding
you may be a few cents off.

I would do what I can to create fields for your report. And then do
your calculations inside the report. I really believe you are trying to
do too much in one query. Another avenue to explore is to divide the
"functions" of your query into sub queries. For example, create a query
that sums up values by department/product. Then link your Order file to
that query instead of the orderitems table.


Mar 24 '07 #4
On Mar 24, 6:52 pm, salad <o...@vinegar.comwrote:
dancol...@gmail.com wrote:
Those are the results of the following query:
SELECT Typelist.Department, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.OrderID) ON TypeList.ProductType =
qryOrdLines.ProductType
WHERE (((Order.OrdBatchID)=[Forms]![Batch]![OrdBatchID]));
The results represent six actual invoices (six records in the Orders
table), but as you can see, because Department is tied to a field in
the OrderLINES table, the OrdTotal field from each of the six invoices
is being repeated for each record in the OrderLines table.
There are a couple of less-than-perfect solutions, as I see it:
1) Instead of using the OrdTotal value from the Orders table, just sum
the SubTotal values from the OrdLines table. The problem is, this
leaves out shipping and sales tax. I could list shipping and sales tax
as actual line items on the invoice, but that could screw a lot of
things up.
2) I could tie each invoice to a Department, rather than each line
item to a Department, but there are occasions where multiple
departments contribute to one invoice.
I hope this helps clarify things. Thanks!

I understand why you'd get multiple departments now.

Since your shipping and tax is rolled up per order and you can have
multiple departments in one order I'd say you are SOL. I suppose you
want to present the report by each department too.

It's easy enough using your scenario to get the tax. But the shipping
calc may not be as easy. I suppose you could get a percentage of the
shipping per department/orderitem and calc it out but due to rounding
you may be a few cents off.

I would do what I can to create fields for your report. And then do
your calculations inside the report. I really believe you are trying to
do too much in one query. Another avenue to explore is to divide the
"functions" of your query into sub queries. For example, create a query
that sums up values by department/product. Then link your Order file to
that query instead of the orderitems table.
Okay, I figured it out. I created separate queries. One gets the
Subtotal for each invoice by summing the OrdLines subtotal field -
because it's not based on the Orders table, it is able to properly
group by Department. The second query is based entirely on the Orders
table, and sums the shipping, tax, and gives the final total. The
third query joins this two together. The final report gives the
subtotals and departments in the Details, and the shipping, tax, and
final totals in the footer.

Thanks!

Mar 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
3
by: David | last post by:
Consider this SQL Query: ----------------------------------------------------------------- SELECT c.CASE_NBR, DATEDIFF(d, c.CREATE_DT, GETDATE()) AS Age, c.AFFD_RCVD, c.PRV_CRD_ISS, x.RegE,...
4
by: Chad Reid | last post by:
Hello, I have a bit of code that obviously doesn't work, but I need help creating a query that would have the same effect as if this query was working. SELECT * FROM (SELECT Count(*) AS...
2
by: Josh Felker | last post by:
Hey everyone. I have a daily log form with my investing info. In that form I have 2 subforms, the first shows my profits on all the stocks I traded that day, the second shows my profits on...
6
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
6
by: lakshmi | last post by:
Hi all I'm trying to traverse through the results from a query that returns more than 1 row. The data reader reads only the first row. The following code doesn't work. Let me know what's wrong....
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
3
by: NewlytoSQL | last post by:
Hi all, im fairly new to SQL and im stuck half way through a query, im using DB2 here is what im tryng to do. i have a query that brings back an item number , shelf req, sum of all orders columns,...
0
VbaNewbee
by: VbaNewbee | last post by:
I have a form with a few filters. Once the user clicks "search button", the code first evaluates my filters, then shows the query results in a List Box" titled backschedule. I have a few text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.