2016-11-23 72 views
3

我创建了一个报告,但我已经停止了对你来说可能很简单的障碍,但我想不出任何方法可以让我解决我的问题。有条件的地方根据参数

我已经创建了一个应该由像被参数基于SQL查询(具有多个CTE)的报告:

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

,它的工作至今。但是我想要创建另一个有3个值的参数。根据这些值,我想获得所有符合条件的记录。有点儿像那个:

  1. 当参数值= 1,那么我想每一条记录
  2. 当参数值= 2,那么我希望每一个记录,其中列1 <> COLUMN2
  3. 当参数值= 3,那么我想获得每个记录 column1 = column2

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。

回答

3

添加到您的WHERE条款:

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

它的工作!感谢您牺牲的阅读和解决问题的帮助和时间;] – M3How