473,486 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Summing Calculated Field in a query

88 New Member
Hi

I have a query which calculates the duration of each activity undertaken by a patient by simply deducting the [Start Time] from the [End Time] - I have called this field within the query [Duration]

What I now need is to Total the [Duration] field for the week so I can see the number of hours of activities a patient does in a week

I have used Between Date1 & Date2 in the Date Field Criteria but have no idea what the new formula or expression would be

The query currently returns 3 'records' / lines (as the patient did three activites between the dates selected) but I imagine it has to somehow aggregate these so that each patient only has 1 'record / line

Hope that makes sense (i'm a novice at queries)
Sep 16 '14 #1
5 1151
zmbd
5,501 Recognized Expert Moderator Expert
More information than what you need now; however, you should bookmark for future reference:
Sum data by using a query


You'll also want: Aggregate Query Woes


and a handy reference:
Basic SQL Syntax for Access Queries


If you are still stuck please post your SQL
Open the Query in design mode
Right click in the table area
Select SQL View
IN a post here, click on the [CODE/] button in the toolbar
Now Cut and Paste the SQL between the two [code][/code] tags.
Sep 16 '14 #2
Hargo
88 New Member
Thanks again zmbd I'll have a read and see how I get on
Sep 16 '14 #3
Hargo
88 New Member
I have managed most of what I wanted using a Crosstab Query of a Total Query, both shown below:

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_25_Hours.[Patient ID], tbl_25_Hours.[Activity Date], tbl_25_Hours.[Start Time], tbl_25_Hours.[End Time], tbl_25_Hours.Activity, tbl_Patient_Information.Surname, tbl_Patient_Information.[Forename(s)], Sum([End Time]-[Start Time]) AS Duration
  2. FROM tbl_Patient_Information INNER JOIN (tbl_Admissions INNER JOIN tbl_25_Hours ON tbl_Admissions.[Patient ID] = tbl_25_Hours.[Patient ID]) ON tbl_Patient_Information.[NHS Number] = tbl_Admissions.[NHS Number]
  3. GROUP BY tbl_25_Hours.[Patient ID], tbl_25_Hours.[Activity Date], tbl_25_Hours.[Start Time], tbl_25_Hours.[End Time], tbl_25_Hours.Activity, tbl_Patient_Information.Surname, tbl_Patient_Information.[Forename(s)]
  4. HAVING (((tbl_25_Hours.[Activity Date]) Between #6/10/2014# And #6/17/2014#));
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum([25 Hour Activity Duration Calculation].Duration) AS SumOfDuration
  2. SELECT [25 Hour Activity Duration Calculation].[Patient ID], Sum([25 Hour Activity Duration Calculation].Duration) AS [Total Of Duration]
  3. FROM [25 Hour Activity Duration Calculation]
  4. GROUP BY [25 Hour Activity Duration Calculation].[Patient ID]
  5. PIVOT [25 Hour Activity Duration Calculation].Activity;
  6.  
What I'd like is for the date part (bold) to be input by the user

I tried to make the Total Query a Parameter query using [Activity Date] BUT got the following message:

"This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables"


Any advice?
Sep 17 '14 #4
twinnyfo
3,653 Recognized Expert Moderator Specialist
Hargo,

Concerning your first question, change line 4 of your first block of code to:

Expand|Select|Wrap|Line Numbers
  1. HAVING (((tbl_25_Hours.[Activity Date]) Between [Start Date?] And [Stop Date?]));
You just have to make sure that the user enters a valid date format (e.g. m/d/yy). You can put that into your input string, too.

Expand|Select|Wrap|Line Numbers
  1. HAVING (((tbl_25_Hours.[Activity Date]) Between [Start Date (m/d/yy)?] And [Stop Date (m/d/yy)?]));
This can become challenging if this is a raw SQL query. If you were building your string in VBA, you could validate a date.

Hope this hepps!
Sep 17 '14 #5
Hargo
88 New Member
Hi Twinnyfo

Just checking through the workings of Db and a problem has popped up with this crosstab query

Since Patient ID became a lookup of AdmisisonsKey I am getting:

Type mismatch in expression message when I try to run the query

It appears to NOT like PatientID as a row heading, presumably because it is a number now rather than text?

Any suggestions on a fix?



Here's the code

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [Start Date] DateTime, [End Date] DateTime;
  2. TRANSFORM Sum([qry_Activity_Duration_&_Dates_Select].Duration) AS SumOfDuration
  3. SELECT [qry_Activity_Duration_&_Dates_Select].PatientID, [qry_Activity_Duration_&_Dates_Select].Forenames, [qry_Activity_Duration_&_Dates_Select].Surname, Sum([qry_Activity_Duration_&_Dates_Select].Duration) AS TotalActivityDuration
  4. FROM [qry_Activity_Duration_&_Dates_Select]
  5. WHERE ((([qry_Activity_Duration_&_Dates_Select].ActivityDate)>=[Start Date] And ([qry_Activity_Duration_&_Dates_Select].ActivityDate)<=[End Date]) AND (([qry_Activity_Duration_&_Dates_Select].Outcome)="Accepted"))
  6. GROUP BY [qry_Activity_Duration_&_Dates_Select].PatientID, [qry_Activity_Duration_&_Dates_Select].Forenames, [qry_Activity_Duration_&_Dates_Select].Surname
  7. PIVOT [qry_Activity_Duration_&_Dates_Select].Activity;
  8.  
Oct 1 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2312
by: Manfred | last post by:
Hi Would like to add a new Field to Table which is 75 % (or another Percentage) higher than the Value in another Field in same Table.Is this possible with Expression or would I have to enter the...
5
2913
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this post. I need to create a metrics (form or report - I don't care which) to display calculated fields about the database (A2002 front end to SQL Server 2K) 1) I...
2
5335
by: hhathome | last post by:
In my report I have a calculated DateDiff Field, I also have a calculated count field. I'm trying to get a total of the DateDiff field and dividing it by the count field and I'm having problems --...
2
5911
by: John | last post by:
I am using Access 2000. One table in my database has a field called RankName. Values inlcude: Officer, Sergeant, Lieutenant. I need create a report that groups these three RankNames into two...
2
3598
by: ey.markov | last post by:
Greetings, in A2K VBA, I set the following recordset: Set rsGPValue = dbs.OpenRecordset("SELECT *, DateSerial(Year(),Month()+4,0) FROM tblGPValue AS OurDate, dbOpenSnapshot) and then I try...
5
5872
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
9
7790
by: Haas C | last post by:
Hi all! Is there anyway I can override a value in a calculated field on a form? For example: I have a form which displays the following fields based on a query: Premium Due field has the...
3
1596
by: fperri | last post by:
Hello, I have a calculated field in my query that uses a function to come up with the value. The function has various fields from the table used in the query passed into it as parameters. For some...
3
5989
by: myemail.an | last post by:
I use Access 2007 and create queries with a number of calculated fields/expressions (I'm still a novice so please forgive me if my wording is imprecise...), like: MyCalculation = Field1 - Field2. ...
2
4268
by: mkbrady | last post by:
I have a query that includes calculated fields that generate numeric results. I have wrapped conversion functions CLng() and CDdl() around the calculated fields to ensure the data types are...
0
6964
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
7126
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,...
1
6842
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
7330
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...
0
5434
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,...
1
4865
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...
0
4559
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...
0
3070
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.