Sql 在查询的Select子句中使用Distinct with Top
在SQL Server中,我可以创建在Select子句中同时使用Top和Distinct的查询,例如: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个不同的程序名。 您的
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