Question

I have this table

SELECT ProductName, Grade, COUNT(Grade) AS count
FROM   Inspection
WHERE (Branch = @branchno) AND (Date BETWEEN @DateFrom AND DATEADD(day, 1, @DateTo))
          AND (Grade <> '')
GROUP BY ProductName, Grade
ORDER BY ProductName

and the report matrix has subtotals that i would like to divide with count of grade

                         | =Fields!grade.Value       |Total
========================= =========================== ======
=Fields!ProductName.Value|=Sum(Fields!count.Value)   |

but ive been trying to get the fields count value to divide by the total value but having trouble with that cause what i need the table to look like is this

       Grade 1 | Grade 2 | Grade 3 |  bad
======|========|=========|=========|=======
orange| 17.65% |  11.76% |  58.82% | 11.76%
------ -------- --------- ---------|--------    
banana| 13.33% |  13.33% |  53.33% | 20.00%  

which we would get by doing the following

       Grade 1 | Grade 2 | Grade 3 |  bad  | Total
======|========|=========|=========|=======|====== 
orange|  3/17  |  2/17   |  10/17  |  2/17 | 17
------ -------- --------- --------- ------- ------
banana|  2/15  |  2/15   |  8/15   |  3/15 | 15

Thanks for your help

Was it helpful?

Solution 2

Thanks to Maciej from http://www.codeproject.com/ we got this to work

SELECT ProductName, Grade, COUNT(Grade) AS CountOfGrades, (SELECT COUNT(Grade) FROM Inspection WHERE (Branch = @branchno) AND (Date BETWEEN @DateFrom AND DATEADD(day, 1, @DateTo))
          AND (Grade<>'') AND (productname=i.productname) AS TotalOfGrades
FROM   Inspection as i.inspection
WHERE (Branch = @branchno) AND (Date BETWEEN @DateFrom AND DATEADD(day, 1, @DateTo))
          AND (Grade <>'')
GROUP BY ProductName, Grade
ORDER BY ProductName

OTHER TIPS

You can sum over the data in a particular grouping or data region. See this example from MSDN where the sum is over the "Order" grouping:

=Sum(Fields!LineTotal.Value, "Order")

You will also want to protect against division-by-zero errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top