Writing a SPARQL query that constructs new triples with result from COUNT in an aggregate

Solution for Writing a SPARQL query that constructs new triples with result from COUNT in an aggregate
is Given Below:

I’m working with the Snap SPARQL tool in Protege so to add data into the ontology I have to use CONSTRUCT because it doesn’t support INSERT (the tool gives the option to assert the new triples constructed back into the ontology). I want to count values with a specific value and assert the count of those values back into the ontology. I created a little test ontology regarding students and grades to help me figure this out. I have the following query which works:

SELECT ?student (COUNT(?test) AS ?tcount)
WHERE {?student test:tookTest ?test.
       ?test test:hasGrade test:A.}
GROUP BY ?student

This gives me a table with each student in one column and their number of A grades in the next. What I want to do next is to use the ?tcount to assert the data back into the ontology. I’ve tried various things like replacing the SELECT with a CONSTRUCT or using an embedded query:

CONSTRUCT {?student test:hasACount ?tcount.}
WHERE {
    SELECT ?student (COUNT(?test) AS ?tcount)
    WHERE {?student test:tookTest ?test.
           ?test test:hasGrade test:A.}
    GROUP BY ?student}

I think the problem with this is that ?tcount isn’t in scope of the surrounding query. I’ve tried several different options like using BIND to BIND ?tcount or grouping by ?tcount rather than ?student but no luck.