I tried debugging it but could not find a solution as to why one employee information is missing from this code.
Select UserName, EmpNumber, Product, SubProduct, [1] as 'January', [2] as 'February', [3] as 'March', [4] as 'April', [5] as 'May', [6] as 'June'
, [7] as 'July', [8] as 'August', [9] as 'September', [10] as 'October', [11] as 'November', [12] as 'December'
from (
SELECT
Userdata.chUserName as UserName
,Month(a.dtInsertDate) as [Month2]
, datename(mm,a.dtInsertDate) as [Month]
,Year(a.dtInsertDate) as [Year]
,chRefReferredBy as EmpNumber
,case when Prod.vchParameterDesc like '%Insurance' and Prod.vchParameterDesc <> 'Auto Insurance' then 'Other Insurance'
when Prod.vchParameterDesc = 'Auto Insurance' then 'Auto Insurance' when Prod.vchParameterDesc = 'Travel' then 'Travel' end as Product
,SubProd.vchParameterDesc as SubProduct
--,count(iCustomerId) as Total
, iCustomerId
FROM [CRM_OED].[dbo].[CSuProductReferrals] a (nolock)
Left Outer Join [CRM_OED].[dbo].[ReferenceParameters] b(nolock)
on b.iParameterId = a.iRefType
Left Outer Join [CRM_OED].[dbo].[ReferenceParameters] as Prod(nolock)
on Prod.iParameterId = a.iRefProduct
Left Outer Join [CRM_OED].[dbo].[ReferenceParameters] as SubProd(nolock)
on SubProd.iParameterId = a.iRefSubProduct
Left Outer Join [CRM_OED].[dbo].[ReferenceParameters] as AccptDec(nolock)
on AccptDec.iParameterId = a.iStatus
Left Outer Join [CRM_OED].[dbo].[Users] Userdata(nolock)
on Userdata.chUserId = a.chRefReferredBy
Inner Join [CRM_OED].[dbo].[csuEmployeeAttributes] as Attributes(nolock)
on Attributes.chUserID = chRefReferredBy
Inner Join [CRM_OED].[dbo].[UDT_GroupsRegion] as Region(nolock)
on Region.chGroupID = 'RE' + cast(Attributes.iRegioncode as CHAR(10))
where Year(a.dtInsertDate) = @Year
--and chRefReferredBy in ('643247')
and AccptDec.vchParameterDesc = 'Accepted'
and a.tiRecordStatus = 1
and( Region.chGroupDesc in (@Region) or ' All' = @Region)
and Prod.vchParameterDesc not in ('Life Insurance', 'Annuities')
and (cast(Attributes.iClubCode as CHAR(10)) in (@Club) or ' All' = @Club)
and (Prod.vchParameterDesc like '%Insurance' or Prod.vchParameterDesc = 'Travel')
) P
Pivot
(
count(iCustomerId)
For [Month2] in
([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) as pvt
Order by
UserName, Product, SubProduct
Any help would really be appreciated.