-----Original Message-----
Hi Alex,
PMFBI...I am sure Ted and Steve are highly
competent to answer this, but, as I read this
thread, I think you might be confused with
how a subquery works....
You had a query like
SELECT
InvoiceID,
HoursWorked,
HourlyRate,
HoursWorked*HourlyRate As HourlyAmount
FROM
[tblBilling Invoice-Work Hours];
and, I believe, you thought it would be
great to also show in your query the
total hourly amount for each InvoiceID..
so you decided to use a subquery to
compute this in an additional field.
There are several things that are not easy
to understand at first with subqueries....
1) it needs to be enclosed in parentheses
2) it needs to provide one "value" for each
record of the main query
3) if the same source (table/query) is used
in a subquery, you will need to give the table
(in your case) an alias within the subquery
In your case, you will need a "correlated"
subquery. Try to imagine the process slowed
down as the query formulates each "line"
of your query result, record-by-record.
Your query is "saying"
get a record from my table,
return the InvoiceID, HoursWorked, HourlyRate
calculate the product of HoursWorked*HourlyRate
then, also, for this specific InvoiceID, find the total
hourly amount
If you were given a specific InvoiceID (say = xxxx),
you probably would have no trouble returning the
total hourly amount:
SELECT SUM(HoursWorked*HourlyRate)
FROM [tblBilling Invoice-Work Hours]
WHERE InvoiceID = xxxx;
to "correlate" the above query back to the main
query, we will have to give our subquery table an
alias ( say "t"), then in the WHERE clause say we
want our "alias" t.InvoiceID to be equal to the
"main query" [tblBilling Invoice-Work Hours].InvoiceID
So...when you type your subquery in a field row,
enclosing it in parentheses, returning only one value,
aliasing the source because it is used in the main
query, and "correlating" InvoiceID's, it will look like
(all on one line, though this will probably word-wrap):
HourlyTotal: (SELECT SUM(t.HoursWorked*t.HourlyRate)
FROM [tblBilling Invoice-Work Hours] As t
WHERE t.InvoiceID = [tblBilling Invoice-Work Hours].InvoiceID)
If you were to then go into SQL View, it might look like:
SELECT
InvoiceID,
HoursWorked,
HourlyRate,
HoursWorked*HourlyRate As HourlyAmount,
(SELECT
SUM(t.HoursWorked*t.HourlyRate)
FROM
[tblBilling Invoice-Work Hours] As t
WHERE
t.InvoiceID = [tblBilling Invoice-Work Hours].InvoiceID)
As HourlyTotal
FROM
[tblBilling Invoice-Work Hours];
Apologies again for butting in.
Please respond back if I have misunderstood.
Good luck,
Gary Walter
I tried what you suggested and it worked great, however it
does not perform the result I need.
I have a query that contains the following fields:
InvoiceID HoursWorked HourlyRate and a field source
containg the expression:
HourlyAmount:[HoursWorked]*[HourlyRate] which returns the
correct value.
What I need this query to do is total the HourlyAmounts per
InvoiceID.
So what I entered in the query is the following:
HourlyTotal: SELECT InvoiceID, SUM (HoursWorked*HourlyRate)
FROM tblBilling Invoice-Work Hours GROUP BY InvoiceID.
I hope this helps. As I stated in a previous newsgroup
message before this one, I am taking Access On-Line
sessions through ElementK to help me build a custom
Database for my company.
Thanks for your suggestions!
-----Original Message-----
Hi Alex,
The query sql that you posted looks like the sql for a
full query, not a subquery. But, the error message that
you posted makes it sound like you may have posted the
sql as the field source in the query builder. If this is
the case, use the toolbar to switch from design view to
sql view, then delete whatever text you see and paste
your sql syntax there. But, also, it looks like your
table name is "tblBilling Invoice-WorkHours". If so, you
need to enclose it in square brackets such as [tblBilling
Invoice-WorkHours].
Post back if this doesn't resolve your problem.
-Ted Allen
-----Original Message-----
I am entering this following command in a query and I am
getting the following warning:
"The syntax of the subquery in this expression in
incorrect. Check the subquery's syntax and enclose the
subquery in parentheses.
I am entering the following expresstion:
SELECT InvoiceID, SUM(HoursWorked*HourlyRate)
FROM tblBilling Invoice-WorkHours
GROUP BY InvoiceID
Can someone help me with the correct syntax?
Thanks!
.
.
.