Wednesday, August 9, 2017

sql server pivot example by year dynamically from table relates pivot

Output:
Desciption : 
i need to get count for number of archived documents from table called Profile but i have archived documents in other table called Profile_Docs , so i need to count every Profile one time and get the first create date for first document relates profile then count it and display number of archived profiles per year.

Main Query :

with cte as(
              select   count( p.ProfileId) as xx ,year(b.createDate) as yr
              from profile p
              INNER JOIN (
                           SELECT ProfileID
                                  , cast(MIN(CreateDate) as date) as createDate
                           from dbo.Profile_Docs 
                           GROUP BY ProfileID
                          ) AS B
              ON B.ProfileID  = p.ProfileID
              where p.FormID= 2551
              group by  year(b.createDate)
)
 select * 
 from cte
 pivot
(
 max(xx)
for yr in ([2014],[2015],[2016],[2017])

) px
The Problem :
the query years is static so if any year greater than 2017 the query won't do it, so i need to get all years available from Profile_Docs Table and pivot them dynamically to avoid missing data from the query .
the complete Solution :

DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(year(createDate))
                    from Profile_Docs
                    group by year(createDate)
                                  order by  year(createDate) desc
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)')
        ,1,1,'')
              print @cols

set @query = 'with cte as(
              select   count( p.ProfileId) as xx ,year(b.createDate) as yr
                     from profile p
                     INNER JOIN (SELECT ProfileID, cast(MIN(CreateDate) as date) as createDate
                                         from dbo.Profile_Docs  GROUP BY ProfileID) AS B
                     ON B.ProfileID  = p.ProfileID
                     where p.FormID= 2551
                     group by  year(b.createDate)
)
 select * 
 from cte
                       pivot
            (
              max(xx)
                for yr in ('+ @cols +')
            ) px '

execute sp_executesql @query; 

No comments:

Post a Comment