Q:

How to do division from select statements in sql server?

I am trying to create a select statement that can return a table of 1 column.
Basically, I am looping through all the records of NewHire, and then using its id, to select stuff from another table. I then do a count to get the number of rows, and then divide to get a percentage value, and then I want to insert it to this temp table t. Then select t. But this isn't working. I am getting syntax errors. 
Does anyone know how to fix this?
declare @t table (Percentage int)
DECLARE @acc INT 
SET @acc = 1
DECLARE @max INT 
select @max = max(HireID) from NewHire
WHILE (@acc <= @max)
BEGIN
  IF (@acc in (select HireID from NewHire))
  BEGIN
    insert into @t  
      (select COUNT(*) from Hire_Response WHERE HireID = @acc) /
      (select COUNT(*) from Hire_Response WHERE HireID = @acc 
       AND (HireResponse = 0 OR HireResponse = 1))
  END
  set @acc = @acc + 1
END
select * from @t

A:

You need an additional select just after your insert.
declare @t table (Percentage int)
DECLARE @acc INT 
SET @acc = 1
DECLARE @max INT 
select @max = max(HireID) from NewHire
WHILE (@acc <= @max)
    BEGIN
        IF (@acc in (select HireID from NewHire))
            BEGIN try
                insert into @t  
                select
                    (select COUNT(*) from Hire_Response WHERE HireID = @acc) /
                    (select COUNT(*) from Hire_Response WHERE HireID = @acc AND (HireResponse = 0 OR HireResponse = 1))
            END try
            begin catch
                insert into @t 
                select 0
            end catch
        set @acc = @acc + 1
    END
select * from @t

There is more than one way to do this though, but the above is your quickest fix to the provided query

