【问题标题】:Getiing multiple rows as output using case statement in sql在sql中使用case语句获取多行作为输出
【发布时间】:2021-02-10 14:32:32
【问题描述】:

我有一张桌子,

id category column1 column2 column3
1   1        val1a   val1b   val1c   
2   2        val2a   val2b   val2c
3   3        val3a   val3b   val3c

我需要根据多个条件从中选择列,如下所示。

SELECT id, category, column1, column2, column3
            FROM table
            WHERE id = @id
                AND category IN (
                select case when (@input1='Yes' AND @input2='Yes') then (select category from table where category in ('1','2'))
                     when (@input1='Yes' AND @input2='No') then (select category from table where category ='1')
                     when (@input1='No' AND @input2='Yes') then (select category from table where category ='2')
                     else ''
                     end as category)
            END

输入值@input1 和@input2 是从另一个表中抓取的,需要根据上述条件选择并输出类别在('1','2') 中的行。

我需要什么-

如果 input1=Yes 且 input2=Yes 输出类别在 ('1','2') 中的行

如果 input1=Yes 且 input2=No 输出类别在 ('1') 中的行

如果 input1=No 且 input2=Yes 输出类别在 ('2') 中的行

如果 input1=No 和 input2=No 输出行('')中的类别

case 语句是否输出多个值?需要帮助。

【问题讨论】:

  • 仅供参考,它是一个案例表达式而不是一个语句。
  • “case 语句是否输出多个值?” 要回答这个问题,CASE 表达式会返回一个标量值。
  • 我们可以在这里使用替代案例吗?
  • @EERS2 。 . .在您的示例数据中,id 是唯一的,因此无需对 category 进行第二次比较。

标签: sql sql-server asp.net-mvc tsql case


【解决方案1】:

也许是这样的:

SELECT id, category, column1, column2, column3
FROM table
WHERE id = @id
AND category IN 
(
    select category 
    from table 
    where 
    (
        category = '1'
        AND
        @input1='Yes'
    )
    OR
    (
        category = '2'
        AND
        @input1='Yes'
    )
)

或者这个:

SELECT id, category, column1, column2, column3
FROM table
WHERE id = @id
AND
(
    (
        @input1='Yes' AND @input2='Yes' AND category IN  (select category from table where category in ('1','2')
    )
    OR
    (
        @input1='Yes' AND @input2='No' AND category IN (select category from table where category ='1')
    )
    OR
    (
        @input1='No' AND @input2='Yes' AND category IN (select category from table where category ='2')
    )
)

【讨论】:

    【解决方案2】:

    我建议您形成一个映射表并在 SELECT 查询中利用它。它将导致简化的代码和更清晰的方法。

    ;WITH CTE_MappingTable AS
    (
    SELECT *
    FROM
    (
    values
    ('Yes','Yes',1),
    ('Yes','Yes',2),
    ('Yes','No',1),
    ('No','Yes',2)
    ) as t(input1,input2,category)
    )
    SELECT id, category, column1, column2, column3
                FROM table as t
                INNER JOIN CTE_MappingTable  as c
                ON c.Category = t.Category 
                WHERE id = @id AND c.input1 = @input1 AND c.input2 = @input2
    

    【讨论】:

      【解决方案3】:

      查看下面的代码,您不需要使用 CASE WHEN,只需使用 where 条件组合即可。

      create table [Test] ( id int, category nvarchar(10));
      
      insert [Test] values(1,'1');
      insert [Test] values(2,'2');
      insert [Test] values(3,'1');
      insert [Test] values(4,'2');
      
      
      declare @input1 varchar(10) = 'Yes'
      declare @input2 varchar(10) = 'No'
      
      SELECT id, category
      FROM [Test]
      WHERE 
          ( 
              ( @input1 = 'Yes' and  category ='1')
              or 
              ( @input2 = 'Yes' and  category ='2')
          )
      

      【讨论】:

        【解决方案4】:

        如果你想使用in,你可以使用:

        where id = @id and
              category in ( (case when @input1 = 'Yes' then 1 end),
                            (case when @input2 = 'Yes' then 2 end)
                          )
        

        case 的默认值为NULL,这将导致任何比较失败。

        另外,请注意,我删除了 '1''2' 周围的单引号。这些看起来像数字,所以我假设category 是一个数字。引号只能用于字符串和日期常量。

        【讨论】:

          猜你喜欢
          • 2020-08-06
          • 2017-08-15
          • 1970-01-01
          • 2017-02-17
          • 2021-03-06
          • 1970-01-01
          • 2014-12-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多