我有一些SQL,其中一部分是with语句的一部分 并且我一直收到一个错误,“NEWID”在使用sqlstate 42703的上下文中无效。
更新:错误一直来自group by子句,使用的函数我没有放入原始代码,因为我认为这不是问题。所以我更新了代码以显示完整版本
是否有人知道该陈述的问题是什么?
HATSTABLE1 (HATId, NewID) as (
select HA.HATId as "ID",
round(
cast(
sum(
case when HA.ID = 4 or
HA.ID < 0
then 1 else 0 end
) AS FLOAT
) / count(*) * 100,
2
) AS NewID
from Hats T
join Heads HD on
T.ID=HD.HatID
group by T.ID
having NewID >1
答案 0 :(得分:0)
试试吧
with tmp as (
select T.HATId as "ID",
sum(case when T.ID = 4 or HA.ID < 0 then 1 else 0 end) as sum1,
count(*) as nb
from Hats T
group by T.HATId
)
select HATId, round(cast(sum1 as decimal)/ nb * 100, 2) NewID
from tmp