SQL Server TOP(1)有区别 [英] SQL Server TOP(1) with distinct

查看:818
本文介绍了SQL Server TOP(1)有区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取第一行,我得到的结果通过 i_version_id 。如果我不使用 TOP(2),我的查询工作作为预期ans返回所有结果按i_version_id排序。但是当我添加 TOP(2)(如下所示)时,它说在 distinct 。请让我知道我在做什么。

I am trying to extract the first row I get after ordering the result by i_version_id. If I do not use TOP(2), my query works as expected ans returns all results sorted by i_version_id. But when I add the TOP(2) (as shown below), it says that there is a syntax error near distinct. Please let me know what I am doing wrong.

SELECT TOP(2) 
    distinct(i_version_id) 
FROM 
    [PaymentGateway_2006].[dbo].[merchant] 
WHERE 
    dt_updated_datetime > '2013-11-11'
GROUP BY
    i_version_id 
ORDER BY 
    i_version_id;


推荐答案

如果你只得到 TOP 1 ,那么 distinct 是不相关的。这也是不相关的,因为按列分组将给你不同的值,

If you're only getting the TOP 1 then distinct is irrelevant. It's also irrelevant since grouping by the column will give you distinct values,

但是,如果你想要多个只删除括号:

However, If you want more than one just remove the parentheses:

SELECT DISTINCT TOP(2) 
    i_version_id
FROM 
    [PaymentGateway_2006].[dbo].[merchant] 
WHERE 
    dt_updated_datetime > '2013-11-11'
GROUP BY
    i_version_id 
ORDER BY 
    i_version_id;

这篇关于SQL Server TOP(1)有区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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