help in calculation

G

Guest

Hi
I need help in some calculation. It is easy in Excel, which I used to do.
I have a table with multiple fields including stimulation (text),
replication (text) and value (numeric) fields.
Stimulation field has the values (Aux, Env1, Env2, Gag, NS ….), Replication
field has values (Rep1, Rep2, Rep3 …Rep6).

Id Group Week AssayType Stimulation Replication AvgOfValue
RSi A Pre Ble Elispot Aux Rep1 7
RSi A Pre Ble Elispot Aux Rep2 8
RSi A Pre Ble Elispot Env1 Rep1 5
RSi A Pre Ble Elispot Env1 Rep2 6
RSi A Pre Ble Elispot Env2 Rep1 7
RSi A Pre Ble Elispot Env2 Rep2 4
RSi A Pre Ble Elispot Gag Rep1 3
RSi A Pre Ble Elispot Gag Rep2 9
RSi A Pre Ble Elispot NS Rep1 3
RSi A Pre Ble Elispot NS Rep2 2
RSi A Pre Ble Elispot NS Rep3 3
RSi A Pre Ble Elispot NS Rep4 2
RSi A Pre Ble Elispot NS Rep5 1
RSi A Pre Ble Elispot NS Rep6 2


I need to calculate average of values (group all other fields) to use in the
following formula.
Avg (value) – Avg (value where stimulation = NS). i.e. Calculate average
value for each stimulation and then subtract the average NS Stimulation value
of that group.
For each group ( Id, Group, Week, Assay type, replication ) and different
stimulations the NS is value is same.
Can some body help me in forming the query?
 
G

Guest

I am sorry about the formatting problems.
Please let me try again.
I have a table with multiple fields including stimulation (text),
replication (text) and value (numeric) fields.
Stimulation field has the values (Aux, Env1, Env2, Gag, NS etc), Replication
field has values (Rep1, Rep2, Rep3 etc.).

Id, Group, Week, Assay, Stimulation, Replication, result
RSi, A, Pre, Elispot, Aux, Rep1, 7
RSi, A, Pre, Elispot, Aux, Rep2, 8
RSi, A, Pre, Elispot, Env1, Rep1, 5
RSi, A, Pre, Elispot, Env1, Rep2, 6
RSi, A, Pre, Elispot, Env2, Rep1, 7
RSi, A, Pre, Elispot, Env2, Rep2, 4
RSi, A, Pre, Elispot, Gag, Rep1, 3
RSi, A, Pre, Elispot, Gag, Rep2, 9
RSi, A, Pre, Elispot, NS, Rep1, 3
RSi, A, Pre, Elispot, NS, Rep2, 2
RSi, A, Pre, Elispot, NS, Rep3, 3
RSi, A, Pre, Elispot, NS, Rep4, 2
RSi, A, Pre, Elispot, NS, Rep5, 1
RSi, A, Pre, Elispot, NS, Rep6, 2


I need to calculate average of “result†field ( when grouping all other
fields, except Replication) to use in the following formula.
Avg (result) – Avg (result where stimulation = NS). i.e. Calculate average
result for each stimulation and then subtract the average NS Stimulation
result of that group ( each ID, Group, week and Assay fields combined group).
For example (average result for Aux) – (average result for NS)
(average result for Env1) – (average result for NS)
(average result for Env2) – (average result for NS)
(average result for Gag) – (average result for NS)
So on.
 
C

Chris2

sheela said:
Hi
I need help in some calculation. It is easy in Excel, which I used to do.
I have a table with multiple fields including stimulation (text),
replication (text) and value (numeric) fields.
Stimulation field has the values (Aux, Env1, Env2, Gag, NS ….), Replication
field has values (Rep1, Rep2, Rep3 …Rep6).

Id Group Week AssayType Stimulation Replication AvgOfValue
RSi A Pre Ble Elispot Aux Rep1 7
RSi A Pre Ble Elispot Aux Rep2 8
RSi A Pre Ble Elispot Env1 Rep1 5
RSi A Pre Ble Elispot Env1 Rep2 6
RSi A Pre Ble Elispot Env2 Rep1 7
RSi A Pre Ble Elispot Env2 Rep2 4
RSi A Pre Ble Elispot Gag Rep1 3
RSi A Pre Ble Elispot Gag Rep2 9
RSi A Pre Ble Elispot NS Rep1 3
RSi A Pre Ble Elispot NS Rep2 2
RSi A Pre Ble Elispot NS Rep3 3
RSi A Pre Ble Elispot NS Rep4 2
RSi A Pre Ble Elispot NS Rep5 1
RSi A Pre Ble Elispot NS Rep6 2

sheela,

In this space-delimited data, there are seven column names, and
eight columns of data.

What is the name of the eighth column?

I need to calculate average of values (group all other fields) to use in the
following formula.

The average of values on which column (when grouping on all the
"other" columns)?

Avg (value) – Avg (value where stimulation = NS). i.e.

There is no "value" column, so what column is the average being
derived from?

What does – mean (three strange looking characters, as my
newsreader displays)?

Also, as far as I can tell, the values under Stimulation are always
"Elispot". The only "NS" values occur under Replication.

Calculate average value for each stimulation and then subtract the
average NS Stimulation value of that group.

I am getting the feeling that the column names aren't properly
aligned with the sample data below the column names.

Can you please provide the data in comma delimited format instead of
space delimited format?

For each group ( Id, Group, Week, Assay type, replication ) and different
stimulations the NS is value is same.

Can you rephrase that?

Can some body help me in forming the query?


More information on how you can improve your chances of getting your
question answered:

-------------------------------

Formatting:

Please use a monospace font (Courier New, etc.) when writing out
your examples (all descriptions, charts, SQL, etc.).

-------------------------------

Process Description:

Please only include the shortest possible narrative of what is going
on with the query. (Include all that is necessary, and nothing
more.)

When parts of your query make calculations, show the exact code or
nearest readable plain-text math formula you can create.

When you are done with this section, re-read it several times before
posting to assure yourself that you are accurately describing the
situation in way you believe others will understand.

-------------------------------

Table Structures/Description:

Post a description of your table structures.

Although it can be a source of information, please do not copy and
paste information directly from MS Access' Documenter. It is
virtually unreadable. Please distill down and legibly format only
the relevant table information.

If reading the information in MS Access' Documenter is too
intimidating (I know what its output says, myself, and I still
dislike going over its output listings), open your table in Design
View, view the column names and data types in it, and then type out
the column names and data types *that are necessary* (do not include
columns that are not absolutely necessary for the query). Use the
Index dialog box (you can get at it by clicking on the "key" icon on
the toolbar) to locate information on primary and foreign keys and
other indexes and type out that information, as well.

Note: For table descriptions (or DDL) lining up the column names,
data type names, and key/index information in neat columns is quite
helpful.

Note: If you know how to write DDL SQL (CREATE TABLE), please post
that (including constraints) instead of text descriptions. (Please
post only the portion of the DDL that is relevant.)


Example (text description):

MyTableOne
MyTableOneID AUTOINCREMENT PK
ColTwo INTEGER NOT NULL
ColThree TEXT(10)

MyTableTwo
MyTableTwoID AUTOINCREMENT PK\
MyTableOneID INTEGER PK/-- Composite Primary Key
ColThree INTEGER FK -- MyTableOne ColOne
ColFour DATETIME
ColFive CURRENCY
ColSix BIT
ColSeven TEXT(1)
ColEight TEXT(1)

etc., etc., etc.


Example (DDL SQL/CREATE TABLE):

CREATE TABLE MyTableOne
(MyTableOneID AUTOINCREMENT
,ColTwo INTEGER NOT NULL
,ColThree TEXT(10)
,CONSTRAINT pk_MyTableOne
PRIMARY KEY (MyTableOneID)
)

CREATE TABLE MyTableTwo
(MyTableTwoID AUTOINCREMENT
,MyTableOneID INTEGER
,ColThree INTEGER
,ColFour DATETIME
,ColFive CURRENCY
,ColSix BIT
,ColSeven TEXT(1)
,ColEight TEXT(1)
,CONSTRAINT pk_MyTableTwo
PRIMARY KEY (MyTableTwoID)
,CONSTRAINT fk_MyTableTwo_MyTableOne_MyTableOneID
FOREIGN KEY (MyTableOneID)
REFERENCES MyTableOne (MyTableOneID)
)

The Primary Key and Foreign Key notes (or constraints in the DDL
SQL) are *critical*.

-------------------------------

Sample Data (using comma delimited lists):

Note: If your sample data is "wide" across the screen, and you can't
trim out any columns because they are needed, make *two* (or more)
charts, and then clearly note that the second chart is the
continuation of the first chart for the same table. It is far
easier to convert a comma delimited chart into a table in MS Word or
import it directly into MS Excel (where the data can be copied and
pasted into a new table in MS Access) or even MS Access than it is
to manually undo the line-break on *every* row of a line-wrapped
chart (in fact, manually undoing the line-breaks caused by newsgroup
posting is a huge pain in the neck).

Note: In a comma delimited list, it is not absolutely necessary
(although it is nice) to have the data in the columns lined straight
up and down, like I have in my examples below. When the data is
finally imported into MS Access, a quick glance at the table in
datasheet view will show things lined up straight. It is not
necessary to expend extra effort on your chart here. (The right
data does have to be in the right position of each row of the chart,
of course.)

Note: Use the real table and column names whenever possible. Use
invented table names and column names (like I use below in my
example) only when you absolutely have to.

Note: When naming the columns on this chart, use the same column
names as is the table structures above. Using shortened names (or
completely different names, for whatever reason) may save space and
prevent line-wraps, but it can be severely confusing. If the chart
gets too "wide", make two (or more) charts if you have to, as noted
above in Table Structures.

Note: Please include just enough rows of sample data so that
sufficient tests of the various possibilities ("test cases") can be
made.

Note: Please do not attempt to post endless rows of data. 3-5 rows
are probably the minimum, and 10-20 row are probably the normal
maximum. (Post only what is necessary, and no more.)

Note: Please try and use real data when possible. However, real
people's personal information, or private information (banking,
proprietary, etc.), should never be posted. When you have
information that cannot be posted, you will have to invent test data
that can produce results similar to what the real data would
produce.


MyTableOne
MyTableOneID, ColTwo, ColThree
1, 2, a
3, 4, b
5, 6, c

MyTableTwo (Part One)
MyTableTwoID, MyTableOneID, ColThree, ColFour, ColFive
1, 5, 1, 01/01/06, 1.01


MyTableTwo (Part Two)
ColSix, ColSeven, ColEight
-1 g, h

-------------------------------

Desired Results

.. . . <whatever it is you want your query to produce; "the right
stuff", if you will forgive the pun>

(Same chart style as found in the Sample Data section.)

-------------------------------

Query:

Your SQL query code attempts to date. (If "SQL code" throws you for
a loop, open your Query in Design View, and then use the menus, View
SQL View, to switch to a window that will show the SQL code. Copy
and paste that into your new post to the newsgroup.)

Note: There is a huge temptation to merely copy and past the SQL
code. Usually, this is completely unreadable, and whoever reads it
must re-align the code in order to make heads or tails of it (yes,
there are a few out there who can read endless unbroken streams of
code packed together, but I am not one of them). If you know how,
spend some time straightening out and aligning the SQL before
posting it.

Note: In some situations, of course, you will have no query or SQL
code at all.

-------------------------------

Current Results:

.. . . <the incorrect results the current query(s) is producing>

(Same chart style as found in the Sample Data section.)

-------------------------------

Lots Of Work:

Does all this sound like a lot of work?

Remember, whatever work you haven't done will have to be done by
whoever tries to answer your question.

Any information that is not included may have to be asked for,
necessitating additional posts (sometimes many) before someone can
begin answering your question.

Time spent doing these things is time spent not answering your
question.

-------------------------------

I hope that the above can be of assistance in helping you receive an
answer to your various MS Access questions.


Sincerely,

Chris O.
 
C

Chris2

sheela said:
I am sorry about the formatting problems.
Please let me try again.
I have a table with multiple fields including stimulation (text),
replication (text) and value (numeric) fields.
Stimulation field has the values (Aux, Env1, Env2, Gag, NS etc), Replication
field has values (Rep1, Rep2, Rep3 etc.).

Id, Group, Week, Assay, Stimulation, Replication, result
RSi, A, Pre, Elispot, Aux, Rep1, 7
RSi, A, Pre, Elispot, Aux, Rep2, 8
RSi, A, Pre, Elispot, Env1, Rep1, 5
RSi, A, Pre, Elispot, Env1, Rep2, 6
RSi, A, Pre, Elispot, Env2, Rep1, 7
RSi, A, Pre, Elispot, Env2, Rep2, 4
RSi, A, Pre, Elispot, Gag, Rep1, 3
RSi, A, Pre, Elispot, Gag, Rep2, 9
RSi, A, Pre, Elispot, NS, Rep1, 3
RSi, A, Pre, Elispot, NS, Rep2, 2
RSi, A, Pre, Elispot, NS, Rep3, 3
RSi, A, Pre, Elispot, NS, Rep4, 2
RSi, A, Pre, Elispot, NS, Rep5, 1
RSi, A, Pre, Elispot, NS, Rep6, 2

Thank you for providing a comma delimited list of data.
I need to calculate average of “result†field ( when grouping all other
fields, except Replication) to use in the following formula.
Avg (result) – Avg (result where stimulation = NS). i.e.
Calculate average

Your "minus" sign is showing up as three strange characters in my
newsreader, and not - (the word _result_ above is also enclosed in
strange characters).

result for each stimulation and then subtract the average NS Stimulation
result of that group ( each ID, Group, week and Assay fields combined group).
For example (average result for Aux) – (average result for NS)
(average result for Env1) – (average result for NS)
(average result for Env2) – (average result for NS)
(average result for Gag) – (average result for NS)
So on.

sheela,

Table:

Note: I could not successfully determine what the primary key of the
table was (other than the entire row), so I added one.

Note: I was unable to determine an appropriate table name, so I
assigned one (please forgive the date appended to the table name).


CREATE TABLE Unknown_20051223_1
(UnknownID AUTOINCREMENT
,Id TEXT(3)
,Group TEXT(1)
,Week TEXT(3)
,Assay TEXT(7)
,Stimulation TEXT(4)
,Replication TEXT(4)
,result INTEGER
,CONSTRAINT pk_Unknown_20051223_1
PRIMARY KEY (UnknownID)
)


Sample Data:

As per the comma delimited list provided.


Query:

SELECT U1.Id
,U1.Group
,U1.Week
,U1.Assay
,U1.Stimulation
,AVG(U1.result) -
(SELECT AVG(U01.result)
FROM Unknown_20051223_1 AS U01
WHERE U01.Stimulation = "NS"
GROUP BY U01.Id
,U01.Group
,U01.Week
,U01.Assay
,U01.Stimulation)
AS Difference
FROM Unknown_20051223_1 AS U1
WHERE U1.Stimulation <> "NS"
GROUP BY U1.Id
,U1.Group
,U1.Week
,U1.Assay
,U1.Stimulation

Results:

Id, Group, Week, Assay, Stimulation, Difference
RSi, A, Pre, Elispot, Aux, 5.33333333333333
RSi, A, Pre, Elispot, Env1, 3.33333333333333
RSi, A, Pre, Elispot, Env2, 3.33333333333333
RSi, A, Pre, Elispot, Gag, 3.83333333333333



As nearly as I can tell, that is what you wanted.


Sincerely,

Chris O.
 
G

Guest

chris:

I am sorry for my late reply. I tried your querry, but it gives the
following error.
" At most one record can be returned by this subquery".

I have been trying other methods aby creating a new field with aggregated
fields in two tables and joining them, but still it is giving the same error.
I have tried exact same syntax as you wrote.
Could you please write where the problem is. Thank you very much for all
your help.
 

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

Similar Threads


Top