首页 > 解决方案 > SQL 选择语句语法问题

问题描述

下面的代码出现错误,抱歉,这可能是一个非常简单的问题,但我真的不明白为什么它不起作用。

我收到“第 9 行,第 1 列'from' 附近的语法不正确”错误。

select distinct
b.Identifier,
(b.Low+ b.High) / 2 as bAverage,
m.Average as mAverage,
coalesce(
case when mAverage not null then ((((b.Low + b.High) / 2) + m.Average) / 2) else null end,
case when mAverage is null then ((b.Low + b.High) / 2)) end as TotalAvg,
(b.Volume + m.Qty) as TotalVolume
from table_b_data b
full outer join table_m_data m on b.Identifier=m.Identifier

标签: sql

解决方案


coalesce()没有关闭的paren。我可以这样写:

coalesce( m.average / 4 + (b.Low + b.High) / 4,
          (b.Low + b.High) / 2
        ) as TotalAvg,

case不是必需的,因为算术表达式将返回NULL。我只是改写了算术(希望是正确的!)以减少括号的数量。


推荐阅读