/ / Utilizzo di Distinct con Top in Seleziona clausola di query - sql, sql-server, tsql

Usando Distinct con Top in Select Clause of Query - sql, sql-server, tsql

In SQL Server sono in grado di creare una query che utilizza sia Top che Distinct nella clausola Select, come questa:

Select Distinct Top 10 program_name
From sampleTable

Il database restituirà i valori distinti dai primi 10 risultati o restituirà i primi 10 risultati dei valori distinti? Questo comportamento è coerente in SQL o dipende dal database?

risposte:

4 per risposta № 1

TOP viene eseguito per ultimo, quindi il tuo DISTINCT viene eseguito prima quindi il TOP

http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/


0 per risposta № 2

Uso

Select Top 10 program_name
From sampleTable group by program_name;

Ti restituirà la cima 10 nome_programma distinto.

La tua query restituirà anche il 10 nome_programma distinto.


0 per risposta № 3

Prova questo:

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

Confronta questo risultato con queste query:

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