Below is the query and the results shown in SQL Analyzer. Only the first two lines (in bold) from the results export into Excel. I am new to SQL so please do not hesitate to ask questions. I appreciate any assistance offered. Thanks in advance.
set nocount onFacility Manually Created Total Created Percent of Deficiencies created Manually
declare @facility char(10),@mcount float, @tcount float
declare countManDef cursor for
select facility_code from cabinet..facility_file
open countManDef
fetch next from countManDef into @facility
while @@fetch_status <> -1
begin
Select @mcount =
(select count(distinct Id) from cabinet.dbo.follow_up f (nolock)
join audit.dbo.audit_trail a (nolock) on folder = encounter
and f.facility = a.facility
and a.occurred = f.created and a.action = 'C'
where f.facility = @facility)*1.0
Select @tcount =
((Select count(*) from cabinet.dbo.follow_up f where f.facility = @facility)*1.0)
If @tcount <> 0 and @tcount is not null
Select @facility as Facility, @mcount as [Manually Created], @tcount as [Total Created], 100*(@mcount/@tcount) as [Percent of Deficiencies created Manually]
Else
Select @facility as Facility, @mcount as [Manually Created], @tcount as [Total Created],0 as [Percent of Deficiencies created Manually]
fetch next from countManDef into @facility
end
close countManDef
deallocate countManDef
---------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------
0234 0.0 0.0 0
Facility Manually Created Total Created Percent of Deficiencies created Manually
---------- ----------------------------------------------------- ----------------------------------------------------- -----------------------------------------------------
A 14.0 23.0 60.869565217391312
Facility Manually Created Total Created Percent of Deficiencies created Manually
---------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------
B 0.0 0.0 0
Facility Manually Created Total Created Percent of Deficiencies created Manually
---------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------
C 0.0 0.0 0