Probably you need this if you've only single row returned in result set:-DECLARE @CtrRef AS Int,@CountValue as intSELECT @CtrRef = BaseRef,@CountValue=COUNT(DocEntry)FROM(SELECT dbo.RIN1.BaseRef,dbo.RIN1.DocEntryFROMdbo.RIN1GROUP BYdbo.RIN1.BaseRef,dbo.RIN1.DocEntry) CountBaseRefGROUP BYBaseRef
or put it in a temporary table if you've more than 1 row in result setDECLARE @temp table(CtrRef int,CountValue int)INSERT INTO @tempSELECT BaseRef,COUNT(DocEntry)FROM(SELECT dbo.RIN1.BaseRef,dbo.RIN1.DocEntryFROMdbo.RIN1GROUP BYdbo.RIN1.BaseRef,dbo.RIN1.DocEntry) CountBaseRefGROUP BYBaseRef