怪物查询中的CASE语句

时间:2014-01-12 00:33:03

标签: sql sql-server case

我正在使用SQL Server 2012。

我使用以下查询来创建用于导入另一个程序的自定义表。此查询工作正常,但我需要使用case语句来完成它。

目的是有问题的专栏

view

当前有效的查询。

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'

这会生成如下结果集:

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

我需要再添加一个使用以下CASE语句的列

CASE WHEN d.purpose = d.purpose2 then d.purpose ELSE  '' END as DistributionCustom

我可以将此case语句用于较小的查询,但在此行之后放置CASE语句时在此大型查询中

d.periods as 'DistributionDesc',

我收到此错误。

将nvarchar值'JohnSmith_purpose'转换为数据类型int时转换失败。

我尝试了这个CASE语句并得到了相同的错误结果

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

提前谢谢

1 个答案:

答案 0 :(得分:1)

由于你有LEFT OUTER JOINS,肯定会有NULLS。在SQL Server中,如果临时列在任何字母数字值之前为NULL,则为其提供INT数据类型 -

您可以尝试更改此类Data字段 -

SELECT b.ReportHeading1, 
            p.DisplayOrder, 
            p.MemberCode,
            m.PortfolioGroupCode as DistGroup,
            m.MemberCode as Packgroup,
            ISNULL(g.purpose,'') AS purpose,
            ISNULL(gg.Purpose,'') as purpose2,
...

更新1 - 尝试将您的案例陈述更改为此 -

CASE    WHEN ISNULL(d.purpose,'.') = ISNULL(d.purpose2,'.')
    THEN CAST(d.purpose2 AS NVARCHAR(MAX))
    ELSE '' END AS DistributionCustom