【问题标题】:SQL Oracle, combine and then caseSQL Oracle,合并然后大小写
【发布时间】:2016-10-18 07:13:31
【问题描述】:

我期待

1) CONCAT 2 字段
2) 使用 case 语句让我的连接字段只留下相关结果

例子

Colour Item  Quantity
Blue   Socks 1
Brown  Shoes 2
Black  Tie   3

我在找

Concat(Colour,Item)    Quantity
BlueSocks              1
Others                 5

我可以使用CASECONCAT,但无法让CASE 引用CONCAT 字段。

【问题讨论】:

  • 你能再次确认这与mysql无关吗?标题是 Oracle,但您标记了两种不同的数据库产品。
  • 看看你能不能把问题说得更清楚些。特别是关于您的预期结果。目前,这对我来说没有多大意义。

标签: mysql oracle case concat


【解决方案1】:

尚不清楚为什么应连接 blue socks 而不是其他行,但要在您的示例中获得结果,您可以这样做:

select 
    case 
       when colour = 'Blue' and item = 'Socks' then concat(colour, item) 
       else 'Other' 
    end as Item, 
    sum(quantity) as Quantity
from t -- your table here
group by 
    case 
       when colour = 'Blue' and item = 'Socks' then concat(colour, item) 
       else 'Other' 
    end

如果您不想在 select 和 group by 子句中重复 case 表达式,可以将其包装在派生表或公用表表达式中。

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2012-03-17
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多