>syntax error, if this run, I will understand
I'm sorry, if you can post table creation/insert, etc. statements I may play with it a bit with SQL Server, otherwise you may want to remove having part for now. I believe it should work without it.
You may move having condition into where condition for the whole query though using it as a having should be quicker, AFAIK.
Also you may want to add Category = 'A' in the inner select.
If it's not broken, fix it until it is.
My Blog