Output:
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 :
execute sp_executesql @query;
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 :
The Problem :with cte as(select count( p.ProfileId) as xx ,year(b.createDate) as yrfrom profile pINNER JOIN (SELECT ProfileID, cast(MIN(CreateDate) as date) as createDatefrom dbo.Profile_DocsGROUP BY ProfileID) AS BON B.ProfileID = p.ProfileIDwhere p.FormID= 2551group by year(b.createDate))select *from ctepivot(max(xx)for yr in ([2014],[2015],[2016],[2017])
) px
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 '
No comments:
Post a Comment