How to make aggregate count based on function name ?
I need to resume my query below or add code to my code below
in result below I need to aggregate by function Name
on first column CountDoneCode is make count to 1 based on function name
on second column CountNotDoneCode is make count to 0 based on function name
Final result I need is
FunctionName CountDoneCode CountNotDoneCode
Lifecycle 3 0
Rohs 0 3
Reach 3 0
FMD 1 2
Parametric 1 2
Package 3 0
IntroductionDate 2 1
MFG 2 1
Qualification 2 1
my code below
create table #Donecode
(
FunctionId int,
FunctionName nvarchar(50)
)
insert into #Donecode
values
(1,'Lifecycle'),
(2,'Rohs'),
(3,'Reach'),
(4,'FMD'),
(5,'Parametric'),
(6,'Package'),
(7,'IntroductionDate'),
(8,'MFG'),
(9,'Qualification')
create table #filedetails
(
FileID int,
DoneCode nvarchar(50)
)
insert into #filedetails (FileID,DoneCode)
values
(3301,'101011111'),
(3301,'101101111'),
(3301,'101001000')
select substring (Donecode,1,1)as Lifecycle,substring (Donecode,2,1)as Rohs,substring (Donecode,3,1)as Reach,substring (Donecode,4,1)as FMD,substring (Donecode,5,1)as Parametric,substring (Donecode,6,1)as Package,substring (Donecode,7,1)as IntroductionDate,substring (Donecode,8,1)as MFG,substring (Donecode,9,1)as Qualification
into #FunctionsDiv from #filedetails where DoneCode is not NULL and fileid=3301
declare @SeletColumnComma varchar(max)
select @SeletColumnComma = coalesce(@SeletColumnComma + ',','') + coalesce('substring(DoneCode ,' +cast (FunctionId as nvarchar(20)) + ',1) as ' + FunctionName + '','') from #Donecode
select @SeletColumnComma
DECLARE @query nvarchar(max)
SET @query='select ' + @SeletColumnComma + ' from #filedetails where DoneCode is not null '
exec(@query)
drop table #filedetails
drop table #Donecode
drop table #FunctionsDiv