473,782 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2533
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.Depart ment, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.Ord erID) ON TypeList.Produc tType =
qryOrdLines.Pro ductType
WHERE (((Order.OrdBat chID)=[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.Depart ment, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.Ord erID) ON TypeList.Produc tType =
qryOrdLines.Pro ductType
WHERE (((Order.OrdBat chID)=[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.c omwrote:
dancol...@gmail .com wrote:
Those are the results of the following query:
SELECT Typelist.Depart ment, Order.OrdTotal
FROM TypeList INNER JOIN ([Order] INNER JOIN qryOrdLines ON
Order.OrderID = qryOrdLines.Ord erID) ON TypeList.Produc tType =
qryOrdLines.Pro ductType
WHERE (((Order.OrdBat chID)=[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
2576
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 java: 325 gcc: 348 Python with Psyco: 1317 Pure Python using sum: 2312
3
7341
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, x.Type, x.Fraud, c.CUST_FN + ' ' + c.CUST_LN AS CustFullName, c.ATM_CKCD_NBR, x.TotalLoss, x.Queue, x.Status, c.QUEUE AS Expr1, x.CHECK_ACT_NBR, c.CUST_LN, c.SSN, c.CREATE_DT FROM ( SELECT TOP 9999999 cl.CASE_NBR, cl.SSN, cl.CREATE_DT,
4
4685
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 numMarried FROM tblClients WHERE mID = 1), (SELECT Count(*) AS numSingle FROM tblClients WHERE mID = 2), (SELECT Count(*) AS numDivorced FROM tblClients WHERE mID = 3),
2
1367
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 those stocks during special times of the day. I want to make a query that will show my total profits each day (sum of subform 1), my profits from subform 2 (sum of subform 2), and the difference between the two. I can't seem to make the difference...
6
17160
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 this can be done? I've tried setting the default value of the text fields on the form to be equal to ! using Access' expression
6
11850
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. do { while(reader.read) { x += y;
7
15004
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 code I have (I am using Python): def generate (rows, cols): # This just prints the coordinates and the number that goes in it, It also prints the matrix in a square import random m = {} for r in range(rows): for c in...
3
3561
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, based on the shelf req column the item number column has more than one row for the same item number therefore my sum of all orders is summing them based on shelf req which i need, what i cant get is how to keep this info plus get the sum of all...
0
1719
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 boxes below the list box that add sum values in the query columns using Nz() Function. Example "openpoqtytotal = openpoqtytotal + Nz(Forms!!backschedule.Column(4, i), 0)" My Problem , The Nz() Function works fine for summing up all values in the...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.