条件WHERE根据参数

时间:2016-11-23 12:40:21

标签: sql sql-server reporting-services parameters reportbuilder

我创建了一份报告,但我已经停止了对您来说可能非常简单的障碍,但我无法考虑任何能让我解决问题的方法。

我创建了一个基于SQL查询(带有多个CTE)的报告,应该通过以下方式进行参数化:

where
deliverydate between @FromDate and @ToDate
and
carrierid = @Carrier

到目前为止它还在运作。但我想创建另一个具有3个值的参数。根据这些值,我想获得符合条件的所有记录。像那样排序:

  1. 当参数值= 1时,我想得到每条记录
  2. 当参数值= 2时,我想要每个记录,其中column1<> 列2
  3. 当参数值= 3时,我想得到每条记录 column1 = column2
  4. Column1和column2是我创建的报告中的列。 最终的选择看起来像这样:

    SELECT  SALESID ,
            CARRIERID ,
            dlvmodeid ,
            SUM(totalweight) AS totalweight ,
            SUM(totalcharges) AS totalcharges ,
            TOTALCHARGESCURRENCY ,
            SUM(HowManyPackagesForThisSO) AS HowManyPackagesForThisSO ,
            SUM(HowManyPackagesForThisSO_st) AS HowManyPackagesForThisSO_st ,
            SUM(InHowManyDays) AS InHowManyDays ,
            SUM(cspjnumber) AS HowManyPackingSlip
    FROM    countingcte
    WHERE   deliverydate BETWEEN @FromDate AND @ToDate
            AND carrierid = @Carrier
    GROUP BY SALESID ,
            CARRIERID ,
            dlvmodeid ,
            TOTALCHARGESCURRENCY
    

    有没有人知道我的问题的解决方案?

    我想到的确切列是HowManyPackagesForThisSO和HowManyPackagesForThisSO_st。

1 个答案:

答案 0 :(得分:3)

将此添加到您的WHERE子句:

AND ((@Param = 1) OR
     (@Param = 2 AND col1 <> col2) OR
     (@Param = 3 AND col1 = col2))