What I am doing is a method of standardization commonly
referred to as "area normalization"
Eventually I need to get to
RFC(i)= Known(i)/avg_area(i)
Cnst(i) = RFC(i)/RFC(0)
Result = (avg_area(i)*Cnst(i)) / sum(area(i)*Cnst(i))
where (i) is the constituent and (0) is the reference point in the analysis. avg_area is the average of the raw data point over several analysis.
So in the workbook I calculate the Cnst(i) that is used in the instrument, or workbook, to calculate results.
So I have the database normalized
PK = Primary Key; FK = Foreign Key to indicated table;
IK = Internal Key to the table (self join)
[T_Constituent]
[PK][Constituent_Name]
[1][A]
[2][b]
[3][C]
etc...
[T_System]
[PK][System_Name]
[1][X]
[2][Y]
[3][Z]
etc...
[T_Stanards]
[PK][Stanards_name][IK_Sample]
[1][ReferenceStd][1]
[2][SecondaryStd][1]
etc...
(the [IK] is used to relate the raw data back to the reference standard ([PK]=[IK])...)
[T_KnownValue]
[PK][FK_Sample][FK_Constituent][KnownValue_Value]
[1][1][1][1.05]
[2][1][2][2.50]
[3][1][3][5.00]
etc...
(Not sure if I should put this in a different table; however, these are not directly related to the results)
[T_Injections]
[PK][Inj_DateTime][FK_Sample][FK_System][Injection_use]
[1][2017-05-03 10:18:27][1][1][1]
[2][2017-05-03 11:18:27][1][1][1]
[3][2017-05-03 12:18:27][1][1][1]
[4][2017-05-03 10:18:27][1][2][1]
[5][2017-05-03 11:18:27][1][2][1]
[6][2017-05-03 12:18:27][1][2][1]
[7][2017-05-04 10:18:27][1][1][1]
[8][2017-05-04 11:18:27][1][1][1]
[9][2017-05-04 12:18:27][1][1][1]
[10][2017-05-04 10:18:27][1][2][1]
[11][2017-05-04 11:18:27][1][2][1]
[12][2017-05-04 12:18:27][1][2][1]
[T_InjectionResult] (these are raw results %area)
[PK][FK_Injection][FK_Constituent][Injection_Value]
[1] [1] [1] [0.98] 1
[2] [1] [2] [0.17] 1
[3] [1] [3] [1.05] 1
[4] [1] [4] [3.20] 1
[5] [1] [6] [40.38] 1
[6] [1] [8] [54.22] 1
[7] [2] [1] [1.09] 1
[8] [2] [2] [0.05] 1
[9] [2] [3] [1.04] 1
[10] [2] [4] [3.16] 1
[11] [2] [6] [40.41] 1
[12] [2] [8] [54.25] 1
[13] [3] [1] [1.02] 1
[14] [3] [3] [1.04] 1
[15] [3] [4] [3.13] 1
[16] [3] [6] [40.5] 1
[17] [3] [8] [54.31] 1
[18] [4] [1] [1.02] 1
[19] [4] [3] [1.04] 1
[20] [4] [4] [3.13] 1
[21] [4] [6] [40.54] 1
[22] [4] [8] [54.28] 1
I've crosstab-queried this and we have a nice table with the injection results for each system as rows and the constituents as columns.
The first step is the get the average of the areas for the day (each day is a batch run against the primary reference)
That would be easy enough; however, I need to sum a few of the constituents first such as "A" and "B":
Expand|Select|Wrap|Line Numbers
- sum(iif([FK_Constituent]=1 or [FK_Constituent]=2,[Injection_Value],0))
so for
[T_InjectionResult].[pk]=1 and 2
0.98+0.17 = 1.15
[T_InjectionResult].[pk]=7 and 8
1.09+0.05 = 1.14
In the cross tab, so long as I have the injections as individual rows (records) this works very nicely. Even in an aggregate I can get this to work so long as these are individual injections
Then average the two AVG(1.15+1.14)= 1.145
This is within the day and per system (the calibration is different for each system) and this is where things break.
I can get the correct sum in the Cross tab or in an aggregate if I do the individual injections, as soon as I group on date/system... the sum adds all of the values togeither (0.98+0.17+1.09+0.05) if by hand I divide by the injection count (in this case 2) this works
Of course I get an error
Expand|Select|Wrap|Line Numbers
- avg(sum(iif([FK_Constituent]=1 or [FK_Constituent]=2,[Injection_Value],0)))
Count
Expand|Select|Wrap|Line Numbers
- SELECT Format([t_injection].[Injection_Date],"yyyy-mm-dd") AS InjDay, t_Injection.FK_Standard
- , t_Injection.FK_System
- , Count(t_Injection.PK_Injections) AS CountOfPK_Injections
- FROM t_Injection
- WHERE (((t_Injection.Injection_use)=1))
- GROUP BY Format([t_injection].[Injection_Date],"yyyy-mm-dd")
- , t_Injection.FK_Standard, t_Injection.FK_System;
Expand|Select|Wrap|Line Numbers
- SELECT Format([t_injection].[Injection_Date],"yyyy-mm-dd") AS InjDay, t_Injection.FK_Standard
- , t_Injection.FK_System, Sum(IIf([t_Injection_result].[FK_Constituent]=1 Or [t_Injection_result].[FK_Constituent]=2,[t_injection_result].[Injection_Value],0)) AS SumAB
- FROM t_Injection LEFT JOIN t_Injection_result ON t_Injection.PK_Injections = t_Injection_result.FK_Injection
- WHERE (((t_Injection.Injection_use)=1))
- GROUP BY Format([t_injection].[Injection_Date],"yyyy-mm-dd"), t_Injection.FK_Standard, t_Injection.FK_System;
If I create a third query to bring both Q_sum and Q_count in, join on the fk_standard use the equation
Expand|Select|Wrap|Line Numbers
- AvgAB: [zq_sum].[SumAB]/[zq_countinjperstandardbyday].[CountOfPK_Injections]
"Your query does not include the specified 'AvgAB' as part of an aggregate function"
Ok, running on fumes and out of time for the day...
-Z