没有Group By [英] Having without Group By

查看:99
本文介绍了没有Group By的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在从Sybase转换为SQL Server,我们的代码中有几个实例,它们使用了不带分组的实例,并且在运行代码时我们在运行时遇到错误。


示例


SELECT Col1,Col2


  FROM table_name


  WHERE ProcessedSw ='N'


具有Col1 = Min(Col1)


任何帮助将不胜感激。



谢谢





解决方案

如果您发布了错误消息,那会有所帮助。你说运行时错误,但我不希望你发布的代码甚至编译。


如果我要抓住你想要实现的目标,我会夸大这一点: / p>

;与编号AS

    SELECT col1,col2,row_number()OVER(ORDER BY Col1)AS rowno

    FROM   table_name

    WHERE  ProcessedSw ='N'



SELECT col1,col2

FROM   table_name

WHERE  rowno = 1


We are converting from Sybase to SQL Server and we have several instances in our code that use Having without a group by and we are getting errors at runtime when we run the code.

Example

SELECT Col1, Col2

  FROM table_name

 WHERE ProcessedSw = 'N'

HAVING Col1 = Min(Col1)

Any help would be appreciated.

Thanks


解决方案

It would have helped if you had posted the error message. You say run-time error, but I would not expect the code you posted to even compile.

If I am to take stab of what you want to achieve, I would suuggest this:

; WITH numbering AS
    SELECT col1, col2, row_number() OVER(ORDER BY Col1) AS rowno
    FROM   table_name
    WHERE  ProcessedSw = 'N'
)
SELECT col1, col2
FROM   table_name
WHERE  rowno = 1


这篇关于没有Group By的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆