SQL查询从sybase转换为SQL Server

时间:2016-02-19 18:50:33

标签: sql-server tsql sybase

select a.dda_pk
from direct_table a      
where a.dda_type = 'B'  
  and a.dda_status = 'D'        
  and a.dda_location = '01'      
group by a.dda_emp_idno       
having  dda_pk <  max(a.dda_pk) 
    and a.dda_status = 'D'  
    and a.dda_location = '01'
  

列'direct_deposit_audit.dda_pk'在HAVING子句中无效   因为它不包含在聚合函数或   GROUP BY子句。

3 个答案:

答案 0 :(得分:0)

这是基于SQL返回每个dda_emp_idno的行的假设 其中dda_pk小于该dda_emp_idno的最大dda_pk。

这应该在SQL Server中做同样的事情:

select dda_pk 
from (
    select 
      dda_pk, 
      dense_rank() over (partition by dda_emp_idno order by dda_pk desc) as RN  
    from 
      direct_table a
    where 
      a.dda_type = 'B'
      and a.dda_status = 'D'
      and a.dda_location = '01'
) X
where RN > 1

答案 1 :(得分:-1)

group by中的所有元素必须位于select子句中,以便您可以尝试:

select a.dda_pk
from direct_table a      
where a.dda_type = 'B'  
and a.dda_status = 'D'        
and a.dda_location = '01'      
group by a.dda_pk       
having  a.dda_pk <  max(a.dda_pk) 
    and a.dda_status = 'D'  
    and a.dda_location = '01'

答案 2 :(得分:-1)

在没有深入研究此查询的情况下,请注意,Sybase ASE在GROUP BY周围的语义比大多数其他数据库都要松散。这意味着使用运行iN ASE的GROUP BY的查询可能会在其他数据库中引发错误。 有关详细信息,请参阅http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.help.ase.15.7/title.htm