我如何得到多个"那么"案例表达的结果?

时间:2016-06-28 17:54:33

标签: sql case

首先,我认为我不能做我原先要做的事情,所以如果有任何建议偏离案例表达,我愿意​​接受它。

就像我能做到的那样简单,这就是我想要它的样子。

SELECT *
FROM Table_CRR
WHERE (QTR in ('1st Quarter', '2nd Quarter'))

但是,我想从单个参数提供过滤器,以便在我选择:

"1st Quarter" it gives nothing, 
"2nd Quarter" returns "1st Quarter", 
"3rd Quarter" returns both "1st Quarter" and "2nd Quarter" and 
"4th Quarter" returns "3rd Quarter", "2nd Quarter", and "1st Quarter".

这就是我所拥有的。

@QuarterSelect VarChar(15)

AS

SET @QuarterSelect = Case 
                    WHEN @QuarterSelect = '4th Quarter' THEN '3rd Quarter'
                    WHEN @QuarterSelect = '4th Quarter' THEN '2nd Quarter'
                    WHEN @QuarterSelect = '4th Quarter' THEN '1st Quarter'
                    WHEN @QuarterSelect = '3rd Quarter' THEN '2nd Quarter'
                    WHEN @QuarterSelect = '3rd Quarter' THEN '1st Quarter'
                    WHEN @QuarterSelect = '2nd Quarter' THEN '1st Quarter'
                    WHEN @QuarterSelect = '1st Quarter' THEN ''
                    ELSE '' 
                  END

SELECT *
FROM Table_CRR
WHERE (QTR in (@QuarterSelect))

它显然终止了,只给了我第二季度#39;我确信有一种优雅的方法可以做到这一点,我只是很难搞清楚。

非常感谢任何帮助或建议!

我想看到的示例:当我将@QuarterSelect参数设置为' 3rd Quarter'

|Key    |Qtr        |
---------------------
|772384 |1st Quarter|
|407038 |1st Quarter|
|790033 |1st Quarter|
|188914 |1st Quarter|
|1607947|2nd Quarter|
|1683954|2nd Quarter|
|1607937|2nd Quarter|
|1948397|2nd Quarter|
...

4 个答案:

答案 0 :(得分:3)

我可能过度简化,但如果你的字符串是一致的,那么为什么不只是做一个比较少的比较。

SELECT *
   FROM Table_CRR
   WHERE 
      QTR < '1st Quarter'   -- returns nothing, unless you have something else   

SELECT *
   FROM Table_CRR
   WHERE 
      QTR < '2st Quarter'   -- returns 1st since 1 is less than 2 string compare

SELECT *
   FROM Table_CRR
   WHERE 
      QTR < '3rd Quarter'   -- returns 1st and 2nd since 1 & 2 are less than 3 string compare

SELECT *
   FROM Table_CRR
   WHERE 
      QTR < '4th Quarter'   -- returns 1st, 2nd and 3rd since 1, 2 & 3 are less than 4 string compare

答案 1 :(得分:2)

这有点粗糙,但如果您的所有QTR标签都具有相同的结构,则可以使用第一个数字,以便您的查询如下:

declare @QuarterSelect int = 3

SELECT *
FROM Table_CRR
WHERE (cast(left(QTR,1) as int) < (@QuarterSelect))

答案 2 :(得分:1)

我认为你对变量如何运作感到困惑。 farable只能有一个值,因此尝试将其设置为两个值是没有意义的。我你想要的东西:

SELECT *
FROM Table_CRR
WHERE (@QuarterSelect = '3rd Quarter' AND QTR in ('1st Quarter', '2nd Quarter')) OR
      (@QuarterSelect = {next option} AND QTR in ({other options}))

或者创建一个内存映射表并join

DECLARE @OptionsMap TABLE
(
    OPTION VARCHAR(15),
    QTR    VARCHAR(15)
)

INSERT INTO @OptionsMap
VALUES ('4th Quarter' , '3rd Quarter')
INSERT INTO @OptionsMap
VALUES ('4th Quarter' ,'2nd Quarter')
INSERT INTO @OptionsMap
VALUES ('4th Quarter' ,'1st Quarter')
INSERT INTO @OptionsMap
VALUES ('3rd Quarter' ,'2nd Quarter')
INSERT INTO @OptionsMap
VALUES ('3rd Quarter' ,'1st Quarter')
INSERT INTO @OptionsMap
VALUES ('2nd Quarter' ,'1st Quarter')
INSERT INTO @OptionsMap
VALUES ('1st Quarter' , '')

SELECT *
FROM Table_CRR
WHERE QTR IN (SELECT QTR FROM @OptionsMap WHERE Option = @QuarterSelect)

答案 3 :(得分:1)

我看到解决方案已被接受,但对于问题的背景,以下内容也相当可观。

SET @QuarterSelect=
Case 
                    WHEN @QuarterSelect = '4th Quarter' THEN '''3rd Quarter'',''2nd Quarter'',''1st Quarter'''

                    WHEN @QuarterSelect = '3rd Quarter' THEN '''2nd Quarter'',''1st Quarter'''

                    WHEN @QuarterSelect = '2nd Quarter' THEN '''1st Quarter'''

                    ELSE ''

                  END

                  declare @sql nvarchar(max);
set @sql = 'SELECT * FROM Table_CRR WHERE (QTR in (' + @QuarterSelect + ')';
exec sp_executesql @sql