Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在查询的Select子句中使用Distinct with Top_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 在查询的Select子句中使用Distinct with Top

Sql 在查询的Select子句中使用Distinct with Top,sql,sql-server,tsql,Sql,Sql Server,Tsql,在SQL Server中,我可以创建在Select子句中同时使用Top和Distinct的查询,例如: Select Distinct Top 10 program_name From sampleTable 数据库将返回前10个结果中的不同值,还是返回前10个不同值的结果?这种行为在SQL中是一致的还是依赖于数据库?使用 Select Top 10 program_name From sampleTable group by program_name; 它将返回前10个不同的程序名。 您的

在SQL Server中,我可以创建在Select子句中同时使用Top和Distinct的查询,例如:

Select Distinct Top 10 program_name
From sampleTable
数据库将返回前10个结果中的不同值,还是返回前10个不同值的结果?这种行为在SQL中是一致的还是依赖于数据库?

使用

Select Top 10 program_name
From sampleTable group by program_name;
它将返回前10个不同的程序名。

您的查询还将返回不同的10程序名。

使用

Select Top 10 program_name
From sampleTable group by program_name;
它将返回前10个不同的程序名。


您的查询还将返回不同的10程序名。

TOP
最后执行,因此您的
distinct
首先运行,然后运行
TOP


TOP
最后执行,因此您的
DISTINCT
首先运行,然后运行
TOP

试试这个:

select distinct top 10 c from
(
    select 1 c union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 2
) as T
order by c
将该结果与以下查询进行比较:

select distinct c from (
    select top 10 c from
    (
        select 1 c union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 2
    ) as T
    order by c
) as T2

select top 10 c from (
    select distinct c from
    (
        select 1 c union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 2
    ) as T
) as T2
order by c
试试这个:

select distinct top 10 c from
(
    select 1 c union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 1   union all
    select 2
) as T
order by c
将该结果与以下查询进行比较:

select distinct c from (
    select top 10 c from
    (
        select 1 c union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 2
    ) as T
    order by c
) as T2

select top 10 c from (
    select distinct c from
    (
        select 1 c union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 1   union all
        select 2
    ) as T
) as T2
order by c