【问题标题】:Is there a way to possibly put a case statement in the from clause?有没有办法在 from 子句中添加 case 语句?
【发布时间】:2014-05-07 18:34:44
【问题描述】:

我正在尝试根据某个方面从不同的位置进行选择。是否可以在FROM 子句中加入CASE 语句?

这就是我想要做的事情

FROM (Case WHEN @location = 'location A' THEN stockA 
WHEN @location = 'location B' then stockB end) ss

如果我不选择多个位置,我会从中提取 StockA。 SS 是别名。

【问题讨论】:

    标签: sql postgresql case


    【解决方案1】:

    你不能这样做。这是一种关闭方法:

    select ab.*
    from ((select a.*
           from stockA a
           where @location = 'location A'
          ) union all
          (select b.*
           from stockB b
           where @location = 'location B'
          )
         ) ab
    

    【讨论】:

    • 这部分where @location = 'location A' 对我来说有点奇怪。它真的会起作用吗?我的意思是它会将参数解析为实际的列名吗?没有把握。 AFAIK,where col = @param
    • @Rahul 。 . .你似乎很困惑。 where 子句将变量与文字值进行比较。就这些。此处不涉及列。
    猜你喜欢
    • 2022-11-08
    • 2023-03-26
    • 1970-01-01
    • 2021-10-26
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多