我一直收到以下错误:
Msg 156,第15级,状态1,第4行关键字“ as”附近的语法不正确。
Msg 156,第15级,状态1,第10行关键字'between'附近的语法不正确。
我看不到问题。
use leads;
select *
(select max(HighTrw)
from (values (trw1), (trw2), (trw21)) as Value (HighTrw)) as [high_trw]
from
dbo.spi s
Where
(select max(HighTrw)
from (values (trw1), (trw2), (trw21)) As updatedate (HighTrw)) between '600' and '625'
您的代码看起来像SQL Server代码,所以我将使用该语法。
大概,您打算做这样的事情:
select s.*, ss.high_trw
from dbo.spi s outer apply
(select max(Value.HighTrw) as high_trw
from (values (s.trw1), (s.trw2), (s.trw21)
) as Value(HighTrw)
) ss
where ss.high_trw between 600 and 625;
注意:
from
将子查询放入apply
子句。600
和625
]看起来像数字,而不是字符串。如果是这样,则常量不应使用引号。除了不提供某些数据外,还有一些我关心的代码问题:
use leads;
-- There's no comma "," after the "*"
select*
-- This look like it belongs in the "from" section as a subquery, eg "(select ...) as sub"
-- You can then reference all of its contents in the main select using "sub.*"
(select max(HighTrw) from (values (trw1), (trw2), (trw21)) as Value(HighTrw)) as [high_trw]
from dbo.spi s
Where (select max(HighTrw)
-- Haven't seen this way of specifying a table before, is it valid?
-- Why not explicitly define a temporary table above with the data you want, _before_ starting the query? - makes the code cleaner/clearer, IMO
from (values (trw1), (trw2), (trw21)) As updatedate (HighTrw)) between '600' and '625'
-- There's no final ";" to terminate the query, might not be absolutely necessary, but good habit,
-- because at some point, this code's going to dovetail into some other code, and the compiler will probably throw a wobbly