【问题标题】:SQL using CASE with STRING functionSQL 使用 CASE 和 STRING 函数
【发布时间】:2014-11-04 20:15:55
【问题描述】:

我正在尝试编写一个同时使用 CASE 和 STRING 的 SQL 查询,但出现错误。 这就是我正在努力的方向。任何帮助是极大的赞赏。 我也尝试添加 STRING 函数,但也不起作用。

SELECT Case
         when sn.1_code =  1 then 'Attended  -- ' ,  
                                  sn.mult_1 , 
                                  , 'and'  ,  
                                  sn.dict_2 ,  
                                  ' also acted with ' ,  
                                  sn.dict_3 , 
                                  '.' , 
         when sn.1_code =  3 then 'left because ' ,
                                  sn.mult_2 ,
                                  '.' ,
         when sn.dict_1 =  2 then 'Went home' ,
         when sn.dict_1 = 24 then 'Canceled' AS 'Attendance'
FROM db.sn

【问题讨论】:

  • 你用的是什么关系型数据库?
  • RDBMS 类型是 Inter systems Cache 系统。但是使用 SQL 来编写查询。

标签: sql string case intersystems-cache


【解决方案1】:

您似乎正在尝试连接字符串。实际操作员可能会因您的服务器软件而异,但想法是:

SELECT
Case 
  when sn.1_code  = 1 
    then  'Attended  -- ' + sn.mult_1 + 'and' + sn.dict_2 + ' also acted with ' + sn.dict_3 + '.' 
  when sn.1_code  = 3 
    then 'left because ' + sn.mult_2 + '.' , 
  when  sn.dict_1 = 2 
    then 'Went home'  ,
  when sn.dict_1 = 24 
    then 'Canceled'   
End AS 'Attendance'
FROM db.sn

【讨论】:

    【解决方案2】:

    正如其他答案所指出的,您需要将字符串值连接在一起。从我对 Intersystems Cache SQL 的了解非常非常少(我刚刚查了一下),您将 need to use || 连接值(您也可以使用 CONCAT() 函数来执行此操作,但它只允许两个参数) :

    SELECT Case
         when sn.1_code =  1 then 'Attended  -- ' || 
                                  sn.mult_1 || 
                                  'and'  ||  
                                  sn.dict_2 ||  
                                  ' also acted with ' ||  
                                  sn.dict_3 || 
                                  '.'  
         when sn.1_code =  3 then 'left because ' ||
                                  sn.mult_2 ||
                                  '.' 
         when sn.dict_1 =  2 then 'Went home' 
         when sn.dict_1 = 24 then 'Canceled' END AS 'Attendance'
    FROM db.sn
    

    你还有一些额外的逗号,以及CASE statement末尾缺少的END

    【讨论】:

    • 我试过了,也得到了一个错误的 SQL 语句)预期,,发现
    • 我想知道这是版本问题,还是我缺少其他东西。不熟悉 Intersystems Cache,很难说。使用 String() 函数查看 Gordon 的答案。也许这样就可以解决问题。
    【解决方案3】:

    Intersystems Cache 似乎支持两个参数concat() 函数(ala Oracle)。它还支持string()。这应该做你想做的事:

    SELECT (Case when sn.1_code =  1
                then string('Attended  -- ', sn.mult_1, 'and',  sn.dict_2 ,  ' also acted with ', sn.dict_3 ,  '.' ) 
                when sn.1_code =  3
                then string('left because ', sn.mult_2 , '.' )
                when sn.dict_1 =  2
                then 'Went home'
                when sn.dict_1 = 24
                then 'Canceled'
            end) AS 'Attendance'
    FROM db.sn;
    

    case 语句还需要一个end

    【讨论】:

    • 我试了一下,得到一个错误的 SQL 语句 IDENTIFIER 预期,?找到
    【解决方案4】:

    如果您尝试连接,在我看来,您可以使用 + 字符,而不是逗号:

    SELECT
    Case when sn.1_code  = 1 then  'Attended  -- ' +  
    sn.mult_1 +
    'and' +
    sn.dict_2 +  
    ' also acted with ' +
    sn.dict_3 +
    '.' 
    when sn.1_code  = 3 
    then 'left because ' + sn.mult_2 + '.'  
    when  sn.dict_1 = 2 then 'Went home'  
    when sn.dict_1 = 24 then 'Canceled' 
    END  AS 'Attendance'
    FROM db.sn
    

    没有看到您使用 String 函数的示例,我不知道您想用它做什么。

    【讨论】:

    • 试过了,但没有成功得到错误提示“ROW SQL 语句中的错误:需要标识符,?找到了
    • 好的,我的回答是假设 SQL Server,但我不知道您正在使用的 RDBMS。试试 JNevill 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多