1

我正在尝试在 where 语句中创建以下内容。

    where Registration_Date >=@StartDate and Registration_Date < dateadd(day,1,@EndDate) 
        and if @affiliateid is null begin Channel in (select Value from dbo.Split(',',@Channel)) end 
             else Affiliate_Id in (select Value from dbo.Split(',', @AffiliateId))
        and Registration_Channel in (select Value from dbo.Split(',', @Channel))

所以,我想说的是,如果@AffiliateId 为空,则对所有会员ID 使用@Channel 输入,如果不是,则使用特定的@AffiliateId,而与渠道无关。

有什么办法可以使这项工作?

4

1 回答 1

1
SELECT  *
FROM    mytable
WHERE   DATE(registration_date) BETWEEN @startDate AND @endDate
        AND EXISTS
        (
        SELECT  NULL
        FROM    dbo.split(',', COALESCE(@affiliateId, @channel))
        WHERE   value = CASE WHEN @affiliateId IS NULL THEN channel ELSE affiliateId END
        )
        AND EXISTS
        (
        SELECT  NULL
        FROM    dbo.split(',', @channel)
        WHERE   value = registration_channel
        )

SQL Server 2008,DATE(column)是一个 sargable 表达式。

于 2013-04-25T10:57:58.197 回答