View Full Version : Get a grand total from SQL Query within SQL Query


aldeb
10-27-2008, 06:36 AM
Below is the sql I have for a query. This works good. What
I would like to add is another Column for Grand Total that
would equal the total of each of the items I am querying
in the query. How can I accomplish this?

SELECT Sum(IIf(([ObserversSecondTbl].[PPEEyeProSafe]=True),1,0)) AS Eye, Sum(IIf(([ObserversSecondTbl].[PPEFaceProSafe]=True),1,0)) AS Face, Sum(IIf(([ObserversSecondTbl].[PPEHeadProSafe]=True),1,0)) AS Head, Sum(IIf(([ObserversSecondTbl].[PPEEarProSafe]=True),1,0)) AS Ear, Sum(IIf(([ObserversSecondTbl].[PPERespiratoryProSafe]=True),1,0)) AS Respiratory, Sum(IIf(([ObserversSecondTbl].[PPEHandProSafe]=True),1,0)) AS Hand, Sum(IIf(([ObserversSecondTbl].[PPEBodyProSafe]=True),1,0)) AS Body, Sum(IIf(([ObserversSecondTbl].[PPEFootProSafe]=True),1,0)) AS Foot
FROM ObserversMainResultsTbl INNER JOIN ObserversSecondTbl ON ObserversMainResultsTbl.FormNbr = ObserversSecondTbl.FormNbr
WHERE (((ObserversMainResultsTbl.Date) Between [Forms]![LiftsQueriesReports]![StartDateTxt] And [Forms]![LiftsQueriesReports]![EndDateTxt]));

georgedwilkinson
10-27-2008, 05:00 PM
Sum(IIf(([ObserversSecondTbl].[PPEEyeProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEFaceProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEHeadProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEEarProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPERespiratoryProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEHandProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEBodyProSafe]=True),1,0)) + Sum(IIf(([ObserversSecondTbl].[PPEFootProSafe]=True),1,0))


Interesting approach.

DCrake
10-29-2008, 04:17 AM
Your grand total column would be GT:Face+Hand+Eye+Etc

redneckgeek
10-29-2008, 08:31 AM
I would do this without all the IIF statements. Since you're dealing with Yes/No (or True/False) fields, you can treat the value as a number.
0 = False (or No), -1 = True (or Yes).
Therefore:
Sum(IIf(([ObserversSecondTbl].[PPEEyeProSafe]=True),1,0))

Can be stated as
SUM([ObserversSecondTbl].[PPEEyeProSafe]*-1)

And your total would then be
SUM([ObserversSecondTbl].[PPEEyeProSafe]*-1+[ObserversSecondTbl].[PPEFaceProSafe]*-1+...)