相同的SQL查询无法在SQL Server中运行,但在Azure SQL转换中可以很好地运行

问题描述 投票:0回答:1

我已经编写了附加查询,并且在SQL Server查询过程中,SELECT几乎在GROUP BY之后运行。

我使用CASE-WHEN在SELECT中创建了一个新列AGE_GRP,并在同一单选查询中使用GROUP BY AGE_GRP,即使先执行GR​​OUP BY然后它仍然有效请紧接着SELECT并应抛出错误,因为尚未找到AGE_GRP!实际上,当我在SQL Management Studio中运行相同的代码时,它给出了错误消息“无效的列名'AGE_GRP'。

但是在使用SQL转换的Azure ML Studio中,它运行得很好。有什么想法吗?enter image description here

sql azure transformation
1个回答
0
投票

由于它们以不同的顺序运行,因此可以在sql-server中使用子查询,然后对其进行分组。

with cte as (
    select 18 Age, 'User01' Name union select 35 , 'User02' union select 75 , 'User02'
)
select 
    AGE_GRP,count(*) as NoTrans
from (
    select 
        (case  when Age < 30  then 'Young' when Age<50 then 'Middle' else 'Senior' end ) as AGE_GRP
        ,*
    from cte
) T
group by AGE_GRP

结果:

AGE_GRP | NoTrans:------ | ------:中| 1个高级1个年轻| 1个

SQL Server 2017 | db<>fiddle

© www.soinside.com 2019 - 2024. All rights reserved.