Multiple Rows Return from VBA Query

psulions83

Board Regular
Joined
Nov 16, 2015
Messages
126
Hi,

I have the below query in Access and it seems I am getting a line for each line in the tables. I only want a sum for the entire table which in return would give me one line back. Can anyone help? I have tried different variations in the group by section.

Code:
SELECT     SUM(A.QUANTITY) AS QUANTITY,
     (SUM(A.QUANTITY) / SUM(B.QUANTITY)) * B.AMOUNT AS AMOUNT,
     "2_SALES" AS COMPONENT,
     "ACCESS" AS SOURCE
FROM Tbl4 A,
   Tbl7 B
WHERE A.PRODUCT = B.PRODUCT
GROUP BY A.PRODUCT=B.PRODUCT, A.QUANTITY, B.QUANTITY, B.AMOUNT
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What happens if you drop the GROUP BY clause?
 
Upvote 0
It gives me an error that says “your query does not include the specified expression “then it lists the line here. Sum function” as part of an aggregate function”
 
Upvote 0
IIRC, you must Group By when doing aggregate functions on records. If you want to filter after grouping, you use a Having clause.
Seems that there are several things that don't look right in your sql.
- if you alias a field, refer to it thereafter by its alias; don't repeat the table/field reference later ( A.QUANTITY, B.AMOUNT, etc. )
- looks like table names have spaces (Tbl4 A, Tbl7 B)
- pretty sure you cannot do comparisons in a Group By clause (that's where Having comes in)

As for retrieving too many records, this happens when you group by more than one field, especially when any fields can belong to one or more of those groups. You may have to break this into more than one query, where each query returns only the record(s) you require. Then you'd join these in a final query.

Not sure what you're trying to do or what the data looks like. Maybe the DSum function would be more useful? You can always create a data set in Excel (centre all data) and paste it in a reply. Makes a quick and dirty table in your post. At least it used to. Haven't tried it in a while.
 
Upvote 0
What are you trying to do here?

"2_SALES" AS COMPONENT,
"ACCESS" AS SOURCE
 
Upvote 0
What are you trying to do here?

"2_SALES" AS COMPONENT,
"ACCESS" AS SOURCE

I am just providing a name and location for the data that is brought in from the code. This worked perfectly until I tried to do the math on the " (SUM(A.QUANTITY) / SUM(B.QUANTITY)) * B.AMOUNT". I want 4 columns to be provided with one output line.
 
Upvote 0
As far as I can see all this,

"2_SALES" AS COMPONENT,
"ACCESS" AS SOURCE

will do is add 2 text fields, one with the value '2_SALES' and the other 'ACCESS.
 
Upvote 0
Thats correct. Its just giving me text for those two fields. I have other unions with different sqls pulling data as well. The sql gives me 4 columns. QUANTITY, AMOUNT, COMPONENT, SOURCE
 
Upvote 0
My math is between multiple tables....maybe I need an inner join?

(TABLE 1 QUANTITY / TABLE 2 QUANTITY) * TABLE 2 AMOUNT. I want this done on each line where the PRODUCT is equal between TABLE 1 and TABLE 2. Then, I want all of those results summer for one total.
 
Upvote 0
Code:
SELECT 
  SUM(A.QUANTITY) AS QUANTITY,
  (SUM(A.QUANTITY) / SUM(B.QUANTITY)) * B.AMOUNT AS AMOUNT,
  "2_SALES" AS COMPONENT,
  "ACCESS" AS SOURCE
FROM 
  Tbl4 A 
    inner join 
      Tbl7 B
    on 
      A.PRODUCT = B.PRODUCT
GROUP BY 
  A.PRODUCT
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,957
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top