complex filter and calculations in access

G

Guest

I don't really think this is that complex. My medical database includes
mixed data, text, numbers, yes/No's etc Specifically, patients that have
undergone a specific operation by a specific surgeon, some have certain risk
factors and some have had certain complications. I can certainly filter to
get all of the patients who have had an appendectomy/by Dr Kildare/who have
diabetes/and got and infection/ But how do I calculate Dr Kildare's overall
infection rate? How do I make the query count Just all of Dr Kildares
operations, and use that as a denominator. How can I compare his infection
rate in diabetcs vs non diabetics?

Please help, my local exper says I need to export it all to excel and do it
there.
Thanks, Tom
 
M

Marshall Barton

DrTominRI said:
I don't really think this is that complex. My medical database includes
mixed data, text, numbers, yes/No's etc Specifically, patients that have
undergone a specific operation by a specific surgeon, some have certain risk
factors and some have had certain complications. I can certainly filter to
get all of the patients who have had an appendectomy/by Dr Kildare/who have
diabetes/and got and infection/ But how do I calculate Dr Kildare's overall
infection rate? How do I make the query count Just all of Dr Kildares
operations, and use that as a denominator. How can I compare his infection
rate in diabetcs vs non diabetics?

Please help, my local exper says I need to export it all to excel and do it
there.


You will probably want to use queries of queries or
subqueries for some of that, but, as Karl said, we need to
know your table structure.

If your "expert" thinks Excel is the way to do this, s/he's
probably an "expert" in Excel, not in Access/SQL.
 
M

Marshall Barton

Not the contents of a table, the **structure** of the tables
involved in your question. I.E. just the table names along
with their significant fields, for example:

Customers:
CustID AutoNumber primary key
CName Text

Contacts:
ContID AutoNumber primary key
CustID Long foreign key to customer
ContName Text
ContPhone Text

table c:
. . .

If you can restate you problem in terms of the tables and
fields in the structure, that would also be a big help.
 
G

Guest

Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Thanks

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


Marshall Barton said:
Not the contents of a table, the **structure** of the tables
involved in your question. I.E. just the table names along
with their significant fields, for example:

Customers:
CustID AutoNumber primary key
CName Text

Contacts:
ContID AutoNumber primary key
CustID Long foreign key to customer
ContName Text
ContPhone Text

table c:
. . .

If you can restate you problem in terms of the tables and
fields in the structure, that would also be a big help.
--
Marsh
MVP [MS Access]

I would love to post my table to you all. how do I do that?
 
M

Marshall Barton

DrTominRI said:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
G

Guest

I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help

Marshall Barton said:
DrTominRI said:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
M

Marshall Barton

We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?
--
Marsh
MVP [MS Access]

I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help

Marshall Barton said:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
G

Guest

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

Marshall Barton said:
We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?
--
Marsh
MVP [MS Access]

I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help

DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer
Marshall Barton said:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
V

Vincent Johns

I agree with Marshall Barton's comments, but I kind of like to use
concrete (even if imaginary) examples in my explanations, when I have
time to do so. So, hoping not to muddy the waters, my suggestion (based
on Marshall's) is to set up (at least) the following three Tables.

First, you already said that you'd have a Table containing information
on surgeons, so I set up an example containing two records.

[Surgeons] Table Datasheet View:

Surgeons_ID name
----------- -------
-1124293098 Kildare
240216869 Welby

Also, thinking that one operation may be performed more than once, and
you included two fields that appeared to be related to that, I set up a
Table to contain that type of information. (Actually, I know that such
information already exists, so you could probably just import this from
a government or insurance-company Web site.) For my example, this Table
contains only a numeric code and a verbal description, but I strongly
urge you to use the standard codes.

[Operations] Table Datasheet View:

operation code operation performed
-------------- -------------------
123 Brain transplant
666 Liposuction
855 Cardiectomy

Now, for the Table containing patient information, I included fields
linking it to the other Tables. For example, in the first record,
instead of specifying a surgeon's name, I used the number, 240216869,
from the [Surgeons_ID] field for that surgeon's record in the [Surgeons]
Table. This is called a "foreign key", being a reference to the primary
key in some other Table. Same for the [operation code] values. Since
each one may be used more than once, it avoids duplicating information
such as the surgeon's name that would otherwise take extra space in the
[Patients] Table, and even worse, cause confusion in case the surgeon's
name were misspelled in some [Patients] records.

[Patients] Table Datasheet View:

Patients_ID last name ... operation Surgeons_ID ...
code
----------- ----------- --------- -----------
-1419127 Doe ... 123 240216869 ...
287390171 Interruptus ... 855 240216869 ...
1896333185 Piggy ... 666 -1124293098 ...
2060653442 Stiltskin ... 666 -1124293098 ...
1284631936 Tenshun ... 855 -1124293098 ...

However, these foreign keys may be hard to read, so I almost always
define a Lookup property (in Table Design View) for the foreign keys.
This causes the datasheet to display some more meaningful information,
in my case the name of the surgeon or of the procedure. Some people
dislike using Lookup properties; the most compelling reason I've seen
for that is that the underlying value is still that key number, and it's
easy to forget that the name you're seeing is not the actual value
stored in the Table. If this bothers you, then don't use Lookup
properties. But for now, the rest of my example will use Lookups in
order to make the datasheets easier to read.

So, with Lookups defined on the two foreign keys, and with the (long)
records in the [Patients] Table split into 3 parts to make them easier
to read, my example [Patients] Table looks like this:

[Patients] Table Datasheet View:

Patients_ID last name first name Medical operation code
record
Number
----------- ----------- ---------- ------- ----------------
-1419127 Doe John 1872 Brain transplant
287390171 Interruptus Curtis 1933 Cardiectomy
1896333185 Piggy Miss 1777 Liposuction
2060653442 Stiltskin Rumple 1980 Liposuction
1284631936 Tenshun Hooper 1922 Cardiectomy

Surgeons_ID Diabetes pre-op high steroids
infections blood
pressure
----------- -------- ---------- -------- --------
Welby No No No No
Welby No No No No
Kildare No No No No
Kildare No No No No
Kildare No No No No


infection heart death how long units of
attack the opera- blood
tion took transfused
--------- ------ ----- ---------- ----------
Yes No No 3:25 17
Yes Yes Yes 0:20 1
No No No 0:45 3
Yes No Yes 1:30 1
No No Yes 2:35 22

Now that we have example Tables on which to base some Queries, my
versions of Marshall's Queries follow.

First, I need to determine how many operations are recorded for each
surgeon; this number will be used later.

[Q_TotalOperations] SQL:

SELECT Surgeons.Surgeons_ID, Surgeons.name,
Count(Patients.Patients_ID) AS CountOfOperations
FROM Surgeons INNER JOIN Patients
ON Surgeons.Surgeons_ID = Patients.Surgeons_ID
GROUP BY Surgeons.Surgeons_ID, Surgeons.name;

[Q_TotalOperations] Query Datasheet View:

Surgeons_ID name CountOfOperations
----------- ------- -----------------
-1124293098 Kildare 3
240216869 Welby 2

Next, I count the number of post-op infections on record for patients
linked to each surgeon, and I calculate relevant infection rates (based
partly on the values returned by the [Q_TotalOperations] Query).

[Q_Infections] SQL:

SELECT Surgeons.name,
Count(Patients.Patients_ID) AS CountOfInfections,
[CountOfInfections]/[Q_TotalOperations]![CountOfOperations]
AS InfectionRate
FROM (Surgeons INNER JOIN Q_TotalOperations
ON Surgeons.Surgeons_ID = Q_TotalOperations.Surgeons_ID)
INNER JOIN Patients
ON Surgeons.Surgeons_ID = Patients.Surgeons_ID
WHERE (((Patients.infection)<>False))
GROUP BY Surgeons.name,
Q_TotalOperations.CountOfOperations
ORDER BY Count(Patients.Patients_ID) DESC;

In this Query Datasheet, I specified a "percentage" format with 2
decimal places for the last field. You can specify a format by opening
the Query in Query Design View, right-clicking at the top of the field's
column, choosing Properties, and specifying the format you want.

[Q_Infections] Query Datasheet View:

name CountOfInfections InfectionRate
-------- ----------------- -------------
Welby 2 100.00%
Kildare 1 33.33%

Here's another Query showing some averages:

[Q_AvgTime&Amount] SQL:

SELECT Surgeons.name,
Avg(Patients.[how long the operation took])
AS [AvgOfhow long the operation took],
Avg(Patients.[units of blood transfused])
AS [AvgOfunits of blood transfused]
FROM Surgeons INNER JOIN Patients
ON Surgeons.Surgeons_ID = Patients.Surgeons_ID
GROUP BY Surgeons.name
ORDER BY Surgeons.name;

Again, I specified appropriate formats for the numeric fields in this Query.

[Q_AvgTime&Amount] Query Datasheet View:

name AvgOfhow long AvgOfunits of
the operation took blood transfused
------- ------------------ ----------------
Kildare 1:36 8.67
Welby 1:52 9.00


-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.


Marshall said:
We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?
DrTominRI said:
I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help

:

DrTominRI wrote:


Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
M

Marshall Barton

Let's try this query as an example:
_____________________________
SELECT M.[{Surgeon Name}],
Count(*) As CountOf Operations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
______________________________

First create a new query without selecting a table and
without entering anything, switch to SQL view (View Menu).

Then Copy and Paste the above SQL statement over the top of
whatever is in the SQL window. The names you need to
replace with your names are contained in { }. Remove the
{}s but leave the []s.

Once you are sure you have the correct names, try to run the
query by switching to datasheet view (View menu), if you get
an error message, click OK and it will usually highlight
something at or near the error in the query. If you are
prompted to enter a value, take careful note of the message
and fix the spelling of whatever it is promting for.
Hopefully, sooner, rather than later, the will run and you
will see the data in sheet view. When you are done looking
at the query's results, switch to Design view. It might be
easier for you to follow the graphical representation than
it is for you to decipher the cryptic SQL view.

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.
--
Marsh
MVP [MS Access]

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

Marshall Barton said:
We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?

I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help


DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
G

Guest

It worked perfectly, Thank you!

Now, as you said, I can translate one field into another, replace infection
with deaths.
I think what I need to learn more about is the correct commands and syntax.
I actually used to write code in Fortran in th 80's when I was in college.
where can I find our more about the "M." and "Me." parts? (I got the Me.
from the other question I was working on - which is solved now) what do they
mean?

I will attempt to send you the DB but I am affraid outlook blocks sending
DBs in emails.

Thanks again,

Tom

Marshall Barton said:
Let's try this query as an example:
_____________________________
SELECT M.[{Surgeon Name}],
Count(*) As CountOf Operations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
______________________________

First create a new query without selecting a table and
without entering anything, switch to SQL view (View Menu).

Then Copy and Paste the above SQL statement over the top of
whatever is in the SQL window. The names you need to
replace with your names are contained in { }. Remove the
{}s but leave the []s.

Once you are sure you have the correct names, try to run the
query by switching to datasheet view (View menu), if you get
an error message, click OK and it will usually highlight
something at or near the error in the query. If you are
prompted to enter a value, take careful note of the message
and fix the spelling of whatever it is promting for.
Hopefully, sooner, rather than later, the will run and you
will see the data in sheet view. When you are done looking
at the query's results, switch to Design view. It might be
easier for you to follow the graphical representation than
it is for you to decipher the cryptic SQL view.

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.
--
Marsh
MVP [MS Access]

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

Marshall Barton said:
We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?


DrTominRI wrote:
I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help


DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
G

Guest

My education continues - I get the feeling Access doesn't like circular
references.
In the query you gave me I can count total operations and infections. But
if I create another expression in the query that divides one result
(countofinfections) by the other (countofoperations) to get the rate of
infections, I get a box asking me to fill in the information?

Do I need to create another query to make calculations on the results of
this query?

Tom

Marshall Barton said:
Let's try this query as an example:
_____________________________
SELECT M.[{Surgeon Name}],
Count(*) As CountOf Operations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
______________________________

First create a new query without selecting a table and
without entering anything, switch to SQL view (View Menu).

Then Copy and Paste the above SQL statement over the top of
whatever is in the SQL window. The names you need to
replace with your names are contained in { }. Remove the
{}s but leave the []s.

Once you are sure you have the correct names, try to run the
query by switching to datasheet view (View menu), if you get
an error message, click OK and it will usually highlight
something at or near the error in the query. If you are
prompted to enter a value, take careful note of the message
and fix the spelling of whatever it is promting for.
Hopefully, sooner, rather than later, the will run and you
will see the data in sheet view. When you are done looking
at the query's results, switch to Design view. It might be
easier for you to follow the graphical representation than
it is for you to decipher the cryptic SQL view.

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.
--
Marsh
MVP [MS Access]

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

Marshall Barton said:
We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?


DrTominRI wrote:
I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help


DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
G

Guest

Marsh, I am trying to send you my database but outlook blocks it. I have
tried changing the extension, or saving it as something else but no luck.
Any suggestions

Tom

:

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.
 
M

Marshall Barton

I hope you didn't think I had abandoned you, but I've been
out of town for the past week.

The M in M.fieldname is just an alias for the full table
name as specified by the "As M" in the FROM clause. Aliases
are often used to shorten the query's SQL statement and make
it easier to read. Although you're not at this point yet,
when using a subquery on the same table as the main query,
then at least one of the FROM clauses must use an alias.

As you have already found, Me is only used with VBA code in
a Class Module to refer to the module's class object (e.g. a
Form).
--
Marsh
MVP [MS Access]

It worked perfectly, Thank you!

Now, as you said, I can translate one field into another, replace infection
with deaths.
I think what I need to learn more about is the correct commands and syntax.
I actually used to write code in Fortran in th 80's when I was in college.
where can I find our more about the "M." and "Me." parts? (I got the Me.
from the other question I was working on - which is solved now) what do they
mean?


Marshall Barton said:
Let's try this query as an example:
_____________________________
SELECT M.[{Surgeon Name}],
Count(*) As CountOf Operations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
______________________________

First create a new query without selecting a table and
without entering anything, switch to SQL view (View Menu).

Then Copy and Paste the above SQL statement over the top of
whatever is in the SQL window. The names you need to
replace with your names are contained in { }. Remove the
{}s but leave the []s.

Once you are sure you have the correct names, try to run the
query by switching to datasheet view (View menu), if you get
an error message, click OK and it will usually highlight
something at or near the error in the query. If you are
prompted to enter a value, take careful note of the message
and fix the spelling of whatever it is promting for.
Hopefully, sooner, rather than later, the will run and you
will see the data in sheet view. When you are done looking
at the query's results, switch to Design view. It might be
easier for you to follow the graphical representation than
it is for you to decipher the cryptic SQL view.

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

:

We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?


DrTominRI wrote:
I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help


DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
M

Marshall Barton

When you have a question about a query, please post a
Copy/Paste of the query's SQL statement so we can see what
you're asking about.

I don't think what you're aking here is really a circular
reference. It sounds more like you are just trying to refer
to a field by its alias. If so, then this is one of those
things that usually works, but sometimes(?) doesn't. The
way to do the calculation is to use the entire expression
instead of the alias name:

SELECT M.[{Surgeon Name}],
Count(*) As CountOfOperations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections,
Sum(IIf(M.{Infection}, 1, Null)) / Count(*) As PctInfect
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
--
Marsh
MVP [MS Access]


My education continues - I get the feeling Access doesn't like circular
references.
In the query you gave me I can count total operations and infections. But
if I create another expression in the query that divides one result
(countofinfections) by the other (countofoperations) to get the rate of
infections, I get a box asking me to fill in the information?

Do I need to create another query to make calculations on the results of
this query?


Marshall Barton said:
Let's try this query as an example:
_____________________________
SELECT M.[{Surgeon Name}],
Count(*) As CountOf Operations,
Sum(IIf(M.{Infection}, 1, Null)) As CountOfInfections
FROM {MainTable} As M
GROUP BY M.[{Surgeon Name}]
______________________________

First create a new query without selecting a table and
without entering anything, switch to SQL view (View Menu).

Then Copy and Paste the above SQL statement over the top of
whatever is in the SQL window. The names you need to
replace with your names are contained in { }. Remove the
{}s but leave the []s.

Once you are sure you have the correct names, try to run the
query by switching to datasheet view (View menu), if you get
an error message, click OK and it will usually highlight
something at or near the error in the query. If you are
prompted to enter a value, take careful note of the message
and fix the spelling of whatever it is promting for.
Hopefully, sooner, rather than later, the will run and you
will see the data in sheet view. When you are done looking
at the query's results, switch to Design view. It might be
easier for you to follow the graphical representation than
it is for you to decipher the cryptic SQL view.

I must be in a good mood today, so go ahead and send me a
compacted copy (Tools - Database Utilities - Compact menu)
of your database using the from address in this post.

My problem is that I don't speak enough "access", or at least not fluently
I do have a fair idea about what your describing but I am not sure what and
where to type it. What part of what your writing should be copied verbatum
and what is meant to represent something else?

where we stand right now is that I still have no clue as to what to do.
I have been trying to create little test databases to no avail

Is there away to create this relationship through a wizard or the event
builder?
Is there a way I can send you a piece of the DB or a screenshot or something?

:

We get that here every day too ;-))

You're welcome, but we're not done here until you get a
query to work. Where do things stand now?


DrTominRI wrote:
I really appreciate the help I was given. I know it is impossible to guess
how to help me if I barely know what I am doing. Your analogy is more
perfect than you know. Unfortunately, I actually get that eveery day.
Thanks for the help


DrTominRI wrote:
Well, I am not really sophisticated enough to use more than one table, sort
of, but here goes.

Main table

Patient last name Text
Patient first name Text
Medical record Number number
operation performed text
operation code number
surgeon name text this is returned to the form from a combo
box from another table named surgeons containing about 20 names

then there is a series of yes/no check boxes
Diabetes yes/no
pre-op infections yes/no
high blood pressure yes/no
steroids yes/no

then the complications
infection yes/no
heart attack yes/no
death yes/no

there are some other fields scattered around like how long and operation took
and nunber of units of blood transfused.

to repeat my question how do I create a query that will alow me to do
calculations on a subset of my data from many fields such as counting the
number of infections one surgeon had compared to his total number of
operations. Or what is each surgeons average blood transfusion rate for a
given operation.

Tom

PS I posted another question to anothr part of the forum as to how to get my
table to look up one piece of data on another table and return that piece of
information and the another bit associated with it in the same table - but I
didn't really get an answer


:
The basic query for aggregating (Count, Sum, etc) data such
as you're looking for will be along these lines:

SELECT M.[Surgeon Name],
Count(*) As CountOf Operations,
Sum(IIf(M.Infection, 1, Null)) As CountOfInfections
FROM MainTable As M
GROUP BY M.[Surgeon Name]

or

SELECT M.[Surgeon Name],
Avg(M.OperationTime) As AvgOperationTime,
Avg(M.TransFusionAmt) As AvgTransFusionAmt
FROM MainTable As M
GROUP BY M.[Surgeon Name]

I am uncertain whether you will see the surgeon's name or an
ID number because you did not provide details about that
combo box lookup field.

If you take one of those queries and Paste into a new
query's SQL view, fix the names to the ones you are using,
and then switch to design view, you should be able to see
how to create other similar queries.

On your other request for assistance, I want to warn you
that your problem is poorly defined and the critical
information is not complete. In addition, the fact that you
have most all of your data in a single table (which violates
the Normalization rules of relational database theory) means
that there are only a few knowledgable people with
experience in working with an ill designed table structure.
So, getting answers means that one of those few has to see
it, decipher what you're talking about, and have the time
and wherewithal to come up with a good answer. I understand
that you are not, and have no interest in becoming, a
database programmer, but getting someone that is capable in
this arena to help you means that you should do some
homework and provide a clear problem description. It's kind
of analogous to someone coming to you and saying "hey Doc,
I don't feel good, make me better" and then you have to
perform your diagnostic procedures not only using email,but
also in a language you are only vaguely familiar with. ;-)
 
M

Marshall Barton

Sorry, Tom, but I don't use OutLook so I don't know the why
you can't send an attachment. I strongly suspect that it's
a Security setting or maybe some Option setting that
blocking it, but I don't know the settings might be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top