Chad said:
Hello, I have a query that I want to use for a graph in a report but I cant
get the query right. I want to Sum FOOTAGE and / by TotalUT and then * by 8
hours. What am I doing wrong? Here is the SQL... Thanks!
SELECT tblMain.[EMPLOYEE NAME], [EMPLOYEE TIME]-[TotalDT] AS TotalUT,
Sum(tblMain.[EMPLOYEE TIME]) AS [SumOfEMPLOYEE TIME], [DT Reason 1]+[DT
MAINTENANCE]+[DT Reason 2] AS TotalDT, Sum(tblMain.[DT Reason 1]) AS [SumOfDT
Reason 1], Sum(tblMain.[DT Reason 2]) AS [SumOfDT Reason 2], Sum(tblMain.[DT
MAINTENANCE]) AS [SumOfDT MAINTENANCE],
Sum([FOOTAGE])/(Sum([TotalUT])*8) AS
Average
FROM tblMain
WHERE (((tblMain.[DAYS DATE]) Between [Start Date: (mm/dd/yy)] And [Stop
Date: (mm/dd/yy)]))
GROUP BY tblMain.[EMPLOYEE NAME];
Chad,
You have said your can't get the query right, but you haven't said
what is actually going wrong.
You have not shared your database schema.
The following is strictly my best guess and may be totally wrong.
Here's your query straightened up.
SELECT tblMain.[EMPLOYEE NAME]
,[EMPLOYEE TIME]-[TotalDT] AS TotalUT
,Sum(tblMain.[EMPLOYEE TIME]) AS [SumOfEMPLOYEE TIME]
,[DT Reason 1]+[DT MAINTENANCE]+[DT Reason 2] AS TotalDT
,Sum(tblMain.[DT Reason 1]) AS [SumOfDT Reason 1]
,Sum(tblMain.[DT Reason 2]) AS [SumOfDT Reason 2]
,Sum(tblMain.[DT MAINTENANCE]) AS [SumOfDT MAINTENANCE]
,Sum([FOOTAGE])/(Sum([TotalUT])*8) AS Average
FROM tblMain
WHERE (((tblMain.[DAYS DATE]) BETWEEN
[Start Date: (mm/dd/yy)] And [Stop Date: (mm/dd/yy)]))
GROUP BY tblMain.[EMPLOYEE NAME];
Let's break it down:
SELECT tblMain.[EMPLOYEE NAME]
,[EMPLOYEE TIME]-[TotalDT] AS TotalUT
Right here, you subtract [TotalDT] from [EMPLOYEE TIME].
TotalDT gets created further below as a column alias. You can't refer
to TotalDT here.
You're also declaring that the result of this expression will be a
column alias named TotalUT.
,Sum(tblMain.[EMPLOYEE TIME]) AS [SumOfEMPLOYEE TIME]
,[DT Reason 1]+[DT MAINTENANCE]+[DT Reason 2] AS TotalDT
Right here, you're declaring that the result of this express will be a
column alias named TotalDT.
,Sum(tblMain.[DT Reason 1]) AS [SumOfDT Reason 1]
,Sum(tblMain.[DT Reason 2]) AS [SumOfDT Reason 2]
,Sum(tblMain.[DT MAINTENANCE]) AS [SumOfDT MAINTENANCE]
,Sum([FOOTAGE])/(Sum([TotalUT])*8) AS Average
TotalUT gets created further above as a column alias. You can't refer
to TotalUT here.
FROM tblMain
WHERE (((tblMain.[DAYS DATE]) BETWEEN
[Start Date: (mm/dd/yy)] And [Stop Date: (mm/dd/yy)]))
GROUP BY tblMain.[EMPLOYEE NAME];
Other Comments.
If you have control of your database schema, consider removing all
spaces (and any special characters) from every table, query, form,
report, macro, and/or module name. To start with, it will get rid of
all those hideous [] characters.
If you have control of your database schema, consider table names that
describe the entity in question. "tblMain" describes nothing in
particular.
If you have control of your own query design, consider using table
aliases in all queries. They will clarify all of your queries. Many
queries are not possible without using table aliases (all types of
correlated subqueries are impossible without table aliases).
Sincerely,
Chris O.