I am using SQL Server 2012.
I am using the following query to create a custom table used to import into another program. This query is working fine but I need to use a case statement to complete it.
Purpose is the problematic column
The current, working query.
WITH Data AS
( SELECT b.ReportHeading1,
p.DisplayOrder,
p.MemberCode,
m.PortfolioGroupCode as DistGroup,
m.MemberCode as Packgroup,
g.purpose,
gg.Purpose as purpose2,
p.PortfolioGroupID as pid,
m.PortfolioGroupID as mid,
convert(varchar(max),lb.value) as repset,
convert(varchar(max),lb2.value) as periods,
RowNumber = ROW_NUMBER() OVER(PARTITION BY p.portfoliogroupid ORDER BY p.DisplayOrder)
FROM [APXFirm].[AdvApp].[vPortfolioGroupMemberFlattened] p
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBase] b
ON b.PortfolioBaseID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroupMember] m
on m.MemberID = p.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroup] g
ON g.PortfolioGroupID = m.PortfolioGroupID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioGroup] gg
ON gg.PortfolioGroupID = m.MemberCode
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBaseLabels] lb
on p.MemberID = lb.PortfolioBaseID
LEFT OUTER JOIN [APXFirm].[AdvApp].[vPortfolioBaseLabels] lb2
on p.MemberID = lb2.PortfolioBaseID
WHERE m.PortfolioGroupCode like '%_Dist%'
and (g.Purpose like '%_ind' or g.Purpose like '%group')
and lb.Label = '$repset'
and lb2.Label = '$periods'
)
SELECT
t.ContentSetName,
d.periods as 'DistributionDesc',
CONCAT(d.pid,'_',d.DisplayOrder,'_',t.ContentSetName,'_',d.ReportHeading1) as DistributionName,
'False' as IsForFunctionalGroup,
'True' as IsLandscapePageNum,
1 as NumOfCopies,
d.purpose as RecipientCode,
d.ReportHeading1 as RecipientFullName,
d.MemberCode as ReportingEntityCode,
'Daily' as RunEvent
FROM Data d
CROSS APPLY
( VALUES
('Cover_SSRS'),
('Separator_docx'),
(d.repset)
) t (ContentSetName)
WHERE d.RowNumber = 1
OR t.ContentSetNAme != 'Cover_SSRS'
This produces a result set like this:
ContentSetName DistributionDesc DistributionName IsForFunctionalGroup IsLandscapePageNum NumOfCopies RecipientCode RecipientFullName ReportingEntityCode RunEvent
Cover_SSRS 135 11221_5178_Cover_SSRS_John Smith Trusts FALSE TRUE 1 SmithJohn_ind John Smith Trusts fial3gen Daily
Separator_docx 135 11221_5178_Separator_docx_John Smith Trusts FALSE TRUE 1 SmithJohn_ind John Smith Trusts fial3gen Daily
Report_DVAStandardardAnnSSRS 135 11221_5178_Report_DVAStandardardAnnSSRS_John Smith Trusts FALSE TRUE 1 SmithJohn_ind John Smith Trusts fial3gen Daily
I need to add one more column that uses the following CASE statement
CASE WHEN d.purpose = d.purpose2 then d.purpose ELSE '' END as DistributionCustom
I can get this case statement to work in a smaller query, but in this large query when placing the CASE Statement after this line
d.periods as 'DistributionDesc',
I get this error.
Conversion failed when converting the nvarchar value 'JohnSmith_purpose' to data type int.
I tried this CASE Statement and got the same error result
CASE WHEN convert(nvarchar(max),d.purpose) = convert(nvarchar(max),d.purpose2) then convert(nvarchar(max),d.purpose2) ELSE convert(nvarchar(max),'') END as DistributionCustom
Thank you in advance