Not sure what you are after here (your revised code does the same job, so I would have the same question about that too )
COUNT(xxx) will count the number of rows in the resultset that are not-NULL for the "xxx" column/expression.
SUM(xxx) will total the "xxx" column / expression
Depends on whether you are using the side effect of (not) counting NULLs in your result? I'm guessing, given that you have a SUBSTRING in there, that you are intending something else. SUBSTRING is only going to be NULL if the whole [ac.Code] column is NULL, hence I reckon you were intending to do some "work"on the first 5 characters (digits??) of [ac.Code]
If you literally just want the total (SUM) count of the non-NULL values in [ac.Code] then replace
SUM(COUNT(SUBSTRING(ac.Code,1,5))) AS Count
with
COUNT(ac.Code) AS Count
Your second, successful, code looks like it is going around the houses more than it needs to (I worry about that, because either you intended it to but are not using the detail results, or you did not intend it to (in which case there is a smarter, more efficient, way to write the code)
As it stands the next person who needs to take a look at your code might ask "Is this meant to do A? ... or B?".
SELECT COUNT(Code) AS Count1
FROM TableA
INNER JOIN (SELECT COUNT(*) AS total FROM TableA) AS t ON (1=1)
GROUP BY Code, t.total
This will produce a resultset (with multiple rows unless all rows have the same, single, value in the [Code] column) with a COUNT of the number of rows where TableA.Code is non-NULL.
It will "GROUP BY" Code and t.total
t.total is the total number of rows in TableA. This total is (Cartesian) joined to every row in the outer query.
I can't see that GROUP BY on t.total is doing anything as it is the same on every row?
So basically you are taking a COUNT(Code) GROUP BY Code and then SUM()ing them. I can't see what that is intended to achieve that
SELECT COUNT(Code) AS Count1
FROM TableA
would not do (more efficiently and without ambiguity) ...
But maybe I am missing something crucial?