Hello,
The requirement is suppose there are category A, B and C in database so i want to display amount for category A, B and C column wise, if anyone adds new category D then my query automatically takes category D as 4th column in query output. I prepared the SQL where i have taken category values from database in a variable and passed that into PIVOT, but now i am unable to convert it into NSQL.
Output should be displayed as
SQL Code:-
DECLARE @cols AS VARCHAR(1000)='';
DECLARE @query AS VARCHAR(1000)='';
SELECT @cols = @cols + QUOTENAME(PRCATEGORY) + ',' FROM (select distinct PRCATEGORY from PRJ_RESOURCES ) as tmp
set @query = ,
N'SELECT * from
(
select ptt.PRPROJECTID,prj_resources.PRCATEGORY , PAA.ACTCOST_SUM
from Niku.PrTask PTT inner Join niku.prassignment PAA ON PAA.prTaskId=PTT.prid inner join niku.prj_resources on PAA.PRRESOURCEID = prj_resources.prid
inner join niku.srm_resources on PAA.PRRESOURCEID = srm_resources.id
where ptt.PRISTASK=1 and prj_resources.PRCATEGORY is NOT NULL and srm_resources.RESOURCE_TYPE in (0,1,2,3)
) src
pivot
(
SUM(PAA.ACTCOST_SUM) for PAA.PRCATEGORY in ( ' + @cols + ')
) piv'
execute(@query)
Any help would be much appreciated.